UNPKG

@serafin/pipeline

Version:

CRUD data access library with a functional approach

110 lines (109 loc) 6.73 kB
import { SchemaBuilder } from "@serafin/schema-builder"; import { IdentityInterface } from "./IdentityInterface"; import { SchemaBuildersInterface } from "./SchemaBuildersInterface"; import { Pipe } from "./PipeInterface"; import { Relation } from "./Relation"; import { ResultsInterface } from "./ResultsInterface"; import { PipelineCreateFunction, PipelineDeleteFunction, PipelineInterface, PipelinePatchFunction, PipelineReadFunction, ReadOnlyPipelineInterface } from "./PipelineInterface"; import { RelationType } from "./RelationType"; export interface PipelineAbstractOptions { validationEnabled?: boolean; name?: string; } export declare const defaultPipelineAbstractOptions: PipelineAbstractOptions; export declare abstract class PipelineAbstract<M extends IdentityInterface = IdentityInterface, CV = any, CO = any, RQ = any, PQ = any, PV = any, DQ = any, CM = any, RM = any, PM = any, DM = any, CTX = any, R extends Record<string, Relation<IdentityInterface, string, IdentityInterface, any, any, RelationType>> = {}> implements PipelineInterface<M, CV, CO, RQ, PQ, PV, DQ, CM, RM, PM, DM, CTX> { schemaBuilders: SchemaBuildersInterface<M, CV, CO, RQ, PQ, PV, DQ, CM, RM, PM, DM, CTX>; relations: R; private pipes; private options; constructor(schemaBuilders: SchemaBuildersInterface<M, CV, CO, RQ, PQ, PV, DQ, CM, RM, PM, DM, CTX>, options?: PipelineAbstractOptions); get modelSchemaBuilder(): SchemaBuilder<M>; pipe<M2 extends IdentityInterface = M, CV2 = CV, CO2 = CO, RQ2 = RQ, PQ2 = PQ, PV2 = PV, DQ2 = DQ, CM2 = CM, RM2 = RM, PM2 = PM, DM2 = DM, CTX2 = CTX>(pipe: Pipe<M, CV, CO, RQ, PQ, PV, DQ, CM, RM, PM, DM, CTX, M2, CV2, CO2, RQ2, PQ2, PV2, DQ2, CM2, RM2, PM2, DM2, CTX2>): PipelineAbstract<M2, CV2, CO2, RQ2, PQ2, PV2, DQ2, CM2, RM2, PM2, DM2, CTX2, R>; /** * Build a recursive function that will call all the pipes for a CRUD method */ private pipeChain; /** * Add a many relation to the pipeline. * A query parameter is added to the all actions to conditionally fetch the related entities. */ addRelationWithMany<NameKey extends string, RelationModel extends IdentityInterface, ReadQuery, ReadMeta>(name: NameKey, pipeline: ReadOnlyPipelineInterface<RelationModel, ReadQuery, ReadMeta>, query: Partial<ReadQuery>): PipelineAbstract<{ [P_1 in keyof (M & { [P in NameKey]?: RelationModel[] | undefined; })]: (M & { [P in NameKey]?: RelationModel[] | undefined; })[P_1]; }, CV, CO, { [P_3 in keyof (RQ & { [P_2 in `with${Capitalize<NameKey>}`]?: boolean | undefined; })]: (RQ & { [P_2 in `with${Capitalize<NameKey>}`]?: boolean | undefined; })[P_3]; }, PQ, PV, DQ, CM, RM, PM, DM, CTX, R & { [key in NameKey]: Relation<M, NameKey, RelationModel, ReadQuery, ReadMeta, RelationType.many>; }>; /** * Add a one relation to the pipeline. * A query parameter is added to the all actions to conditionally fetch the related entity. */ addRelationWithOne<NameKey extends string, RelationModel extends IdentityInterface, ReadQuery, ReadMeta>(name: NameKey, pipeline: ReadOnlyPipelineInterface<RelationModel, ReadQuery, ReadMeta>, query: Partial<ReadQuery>): PipelineAbstract<{ [P_1 in keyof (M & { [P in NameKey]?: RelationModel | undefined; })]: (M & { [P in NameKey]?: RelationModel | undefined; })[P_1]; }, CV, CO, { [P_3 in keyof (RQ & { [P_2 in `with${Capitalize<NameKey>}`]?: boolean | undefined; })]: (RQ & { [P_2 in `with${Capitalize<NameKey>}`]?: boolean | undefined; })[P_3]; }, PQ, PV, DQ, CM, RM, PM, DM, CTX, R & { [key in NameKey]: Relation<M, NameKey, RelationModel, ReadQuery, ReadMeta, RelationType.one>; }>; /** * Create new resources based on `resources` input array. * * @param resources An array of partial resources to be created * @param options Options that can alter the action behavior * @param context Context object */ create(resources: CV[], options?: CO, context?: CTX): Promise<ResultsInterface<M, CM>>; /** * Create action placeholder * It should be overridden by child pipeline class */ protected _create(resources: CV[], options: CO, context: CTX): Promise<ResultsInterface<M, CM>>; /** * Extract a standalone create function. * It can be used as a parameter for pipes to isolate dependencies and ease constraints definition */ getCreateFunction(): PipelineCreateFunction<M, CV, CO, CM, CTX>; /** * Read resources from the underlying source according to the given `query`. * * @param query The query filter to be used for fetching the data * @param context Context object */ read(query: RQ, context?: CTX): Promise<ResultsInterface<M, RM>>; /** * Read action placeholder * It should be overridden by child pipeline class */ protected _read(query: RQ, context: CTX): Promise<ResultsInterface<M, RM>>; /** * Extract a standalone read function. * It can be used as a parameter for pipes to isolate dependencies and ease constraints definition */ getReadFunction(): PipelineReadFunction<M, RQ, RM, CTX>; /** * Patch resources according to the given query and values. * The `query` will select a subset of the underlying data source and `values` are updated on it. * This method should follow the JSON merge patch standard if possible. @see https://tools.ietf.org/html/rfc7396 * * @param query * @param values * @param context Context object */ patch(query: PQ, values: PV, context?: CTX): Promise<ResultsInterface<M, PM>>; /** * Patch action placeholder * It should be overridden by child pipeline class */ protected _patch(query: PQ, values: PV, context: CTX): Promise<ResultsInterface<M, PM>>; /** * Extract a standalone patch function. * It can be used as a parameter for pipes to isolate dependencies and ease constraints definition */ getPatchFunction(): PipelinePatchFunction<M, PQ, PV, PM, CTX>; /** * Delete resources that match th given Query. * @param query The query filter to be used for selecting resources to delete * @param context Context object */ delete(query: DQ, context?: CTX): Promise<ResultsInterface<M, DM>>; /** * Delete action placeholder * It should be overridden by child pipeline class */ protected _delete(query: DQ, context: CTX): Promise<ResultsInterface<M, DM>>; /** * Extract a standalone delete function. * It can be used as a parameter for pipes to isolate dependencies and ease constraints definition */ getDeleteFunction(): PipelineDeleteFunction<M, DQ, DM, CTX>; private handleValidationOfData; clone(): this; }