import { Filter, FilterExcludingWhere, Where } from '@loopback/filter'; import { AnyObject, Command, Count, DataObject, NamedParameters, Options, PositionalParameters } from '../common-types'; import { DataSource } from '../datasource'; import { Entity, Model, ValueObject } from '../model'; import { InclusionResolver } from '../relations/relation.types'; import { IsolationLevel, Transaction } from '../transaction'; export interface Repository { } export interface ExecutableRepository extends Repository { /** * Execute a query with the given parameter object or an array of parameters * @param command - The query string or command object * @param parameters - The object with name/value pairs or an array of parameter * values * @param options - Options */ execute(command: Command, parameters: NamedParameters | PositionalParameters, options?: Options): Promise; } /** * A type for CRUD repositories that are backed by IDs and support * Transactions */ export declare type TransactionalEntityRepository = TransactionalRepository & EntityCrudRepository; /** * Repository Interface for Repositories that support Transactions * * @typeParam T Generic type for the Entity */ export interface TransactionalRepository extends Repository { /** * Begin a new Transaction * @param options - Options for the operations * @returns Promise Promise that resolves to a new Transaction * object */ beginTransaction(options?: IsolationLevel | Options): Promise; } /** * Basic CRUD operations for ValueObject and Entity. No ID is required. */ export interface CrudRepository extends Repository { /** * Create a new record * @param dataObject - The data to be created * @param options - Options for the operations * @returns A promise of record created */ create(dataObject: DataObject, options?: Options): Promise; /** * Create all records * @param dataObjects - An array of data to be created * @param options - Options for the operations * @returns A promise of an array of records created */ createAll(dataObjects: DataObject[], options?: Options): Promise; /** * Find matching records * @param filter - Query filter * @param options - Options for the operations * @returns A promise of an array of records found */ find(filter?: Filter, options?: Options): Promise<(T & Relations)[]>; /** * Updating matching records with attributes from the data object * @param dataObject - The data to be updated * @param where - Matching criteria * @param options - Options for the operations * @returns A promise of number of records updated */ updateAll(dataObject: DataObject, where?: Where, options?: Options): Promise; /** * Delete matching records * @param where - Matching criteria * @param options - Options for the operations * @returns A promise of number of records deleted */ deleteAll(where?: Where, options?: Options): Promise; /** * Count matching records * @param where - Matching criteria * @param options - Options for the operations * @returns A promise of number of records matched */ count(where?: Where, options?: Options): Promise; } /** * Base interface for a repository of entities */ export interface EntityRepository extends ExecutableRepository { } /** * CRUD operations for a repository of entities */ export interface EntityCrudRepository extends EntityRepository, CrudRepository { entityClass: typeof Entity & { prototype: T; }; inclusionResolvers: Map>; /** * Save an entity. If no id is present, create a new entity * @param entity - Entity to be saved * @param options - Options for the operations * @returns A promise that will be resolve if the operation succeeded or will * be rejected if the entity was not found. */ save(entity: DataObject, options?: Options): Promise; /** * Update an entity * @param entity - Entity to be updated * @param options - Options for the operations * @returns A promise that will be resolve if the operation succeeded or will * be rejected if the entity was not found. */ update(entity: DataObject, options?: Options): Promise; /** * Delete an entity * @param entity - Entity to be deleted * @param options - Options for the operations * @returns A promise that will be resolve if the operation succeeded or will * be rejected if the entity was not found. */ delete(entity: DataObject, options?: Options): Promise; /** * Find an entity by id, return a rejected promise if not found. * * @remarks * * The rationale behind findById is to find an instance by its primary key * (id). No other search criteria than id should be used. If a client wants * to use a `where` clause beyond id, use `find` or `findOne` instead. * * @param id - Value for the entity id * @param filter - Additional query options. E.g. `filter.include` configures * which related models to fetch as part of the database query (or queries). * @param options - Options for the operations * @returns A promise of an entity found for the id */ findById(id: ID, filter?: FilterExcludingWhere, options?: Options): Promise; /** * Update an entity by id with property/value pairs in the data object * @param id - Value for the entity id * @param data - Data attributes to be updated * @param options - Options for the operations * @returns A promise that will be resolve if the operation succeeded or will * be rejected if the entity was not found. */ updateById(id: ID, data: DataObject, options?: Options): Promise; /** * Replace an entity by id * @param id - Value for the entity id * @param data - Data attributes to be replaced * @param options - Options for the operations * @returns A promise that will be resolve if the operation succeeded or will * be rejected if the entity was not found. */ replaceById(id: ID, data: DataObject, options?: Options): Promise; /** * Delete an entity by id * @param id - Value for the entity id * @param options - Options for the operations * @returns A promise that will be resolve if the operation succeeded or will * be rejected if the entity was not found. */ deleteById(id: ID, options?: Options): Promise; /** * Check if an entity exists for the given id * @param id - Value for the entity id * @param options - Options for the operations * @returns Promise if an entity exists for the id, otherwise * Promise */ exists(id: ID, options?: Options): Promise; } /** * Repository implementation * * @example * * User can import `CrudRepositoryImpl` and call its functions like: * `CrudRepositoryImpl.find(somefilters, someoptions)` * * Or extend class `CrudRepositoryImpl` and override its functions: * ```ts * export class TestRepository extends CrudRepositoryImpl { * constructor(dataSource: DataSource, model: Test) { * super(dataSource, Customer); * } * * // Override `deleteAll` to disable the operation * deleteAll(where?: Where, options?: Options) { * return Promise.reject(new Error('deleteAll is disabled')); * } * } * ``` */ export declare class CrudRepositoryImpl implements EntityCrudRepository { dataSource: DataSource; entityClass: typeof Entity & { prototype: T; }; private connector; readonly inclusionResolvers: Map>; constructor(dataSource: DataSource, entityClass: typeof Entity & { prototype: T; }); private toModels; private toModel; create(entity: DataObject, options?: Options): Promise; createAll(entities: DataObject[], options?: Options): Promise; save(entity: DataObject, options?: Options): Promise; find(filter?: Filter, options?: Options): Promise; findById(id: ID, filter?: FilterExcludingWhere, options?: Options): Promise; update(entity: DataObject, options?: Options): Promise; delete(entity: DataObject, options?: Options): Promise; updateAll(data: DataObject, where?: Where, options?: Options): Promise; updateById(id: ID, data: DataObject, options?: Options): Promise; replaceById(id: ID, data: DataObject, options?: Options): Promise; deleteAll(where?: Where, options?: Options): Promise; deleteById(id: ID, options?: Options): Promise; count(where?: Where, options?: Options): Promise; exists(id: ID, options?: Options): Promise; execute(command: Command, parameters: NamedParameters | PositionalParameters, options?: Options): Promise; }