import { Getter } from '@loopback/core'; import { Filter, FilterExcludingWhere, InclusionFilter, Where } from '@loopback/filter'; import legacy from 'loopback-datasource-juggler'; import { AnyObject, Command, Count, DataObject, NamedParameters, Options, PositionalParameters } from '../common-types'; import { Entity, Model } from '../model'; import { BelongsToAccessor, HasManyRepositoryFactory, HasManyThroughRepositoryFactory, HasOneRepositoryFactory, InclusionResolver, ReferencesManyAccessor } from '../relations'; import { IsolationLevel, Transaction } from '../transaction'; import { EntityCrudRepository, TransactionalEntityRepository } from './repository'; export declare namespace juggler { export import DataSource = legacy.DataSource; export import ModelBase = legacy.ModelBase; export import ModelBaseClass = legacy.ModelBaseClass; export import PersistedModel = legacy.PersistedModel; export import KeyValueModel = legacy.KeyValueModel; export import PersistedModelClass = legacy.PersistedModelClass; export import Transaction = legacy.Transaction; export import IsolationLevel = legacy.IsolationLevel; } /** * This is a bridge to the legacy DAO class. The function mixes DAO methods * into a model class and attach it to a given data source * @param modelClass - Model class * @param ds - Data source * @returns {} The new model class with DAO (CRUD) operations */ export declare function bindModel(modelClass: T, ds: juggler.DataSource): T; /** * Ensure the value is a promise * @param p - Promise or void */ export declare function ensurePromise(p: legacy.PromiseOrVoid): Promise; /** * Default implementation of CRUD repository using legacy juggler model * and data source */ export declare class DefaultCrudRepository implements EntityCrudRepository { entityClass: typeof Entity & { prototype: T; }; dataSource: juggler.DataSource; modelClass: juggler.PersistedModelClass; readonly inclusionResolvers: Map>; /** * Constructor of DefaultCrudRepository * @param entityClass - LoopBack 4 entity class * @param dataSource - Legacy juggler data source */ constructor(entityClass: typeof Entity & { prototype: T; }, dataSource: juggler.DataSource); private ensurePersistedModel; /** * Creates a legacy persisted model class, attaches it to the datasource and * returns it. This method can be overridden in sub-classes to acess methods * and properties in the generated model class. * @param entityClass - LB4 Entity constructor */ protected definePersistedModel(entityClass: typeof Model): typeof juggler.PersistedModel; private resolvePropertyType; /** * @deprecated * Function to create a constrained relation repository factory * * Use `this.createHasManyRepositoryFactoryFor()` instead * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance */ protected _createHasManyRepositoryFactoryFor(relationName: string, targetRepositoryGetter: Getter>): HasManyRepositoryFactory; /** * Function to create a constrained relation repository factory * * @example * ```ts * class CustomerRepository extends DefaultCrudRepository< * Customer, * typeof Customer.prototype.id, * CustomerRelations * > { * public readonly orders: HasManyRepositoryFactory; * * constructor( * protected db: juggler.DataSource, * orderRepository: EntityCrudRepository, * ) { * super(Customer, db); * this.orders = this._createHasManyRepositoryFactoryFor( * 'orders', * orderRepository, * ); * } * } * ``` * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance */ protected createHasManyRepositoryFactoryFor(relationName: string, targetRepositoryGetter: Getter>): HasManyRepositoryFactory; /** * Function to create a constrained hasManyThrough relation repository factory * * @example * ```ts * class CustomerRepository extends DefaultCrudRepository< * Customer, * typeof Customer.prototype.id, * CustomerRelations * > { * public readonly cartItems: HasManyRepositoryFactory; * * constructor( * protected db: juggler.DataSource, * cartItemRepository: EntityCrudRepository, * throughRepository: EntityCrudRepository, * ) { * super(Customer, db); * this.cartItems = this.createHasManyThroughRepositoryFactoryFor( * 'cartItems', * cartItemRepository, * ); * } * } * ``` * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance * @param throughRepo - Through repository instance */ protected createHasManyThroughRepositoryFactoryFor(relationName: string, targetRepositoryGetter: Getter> | { [repoType: string]: Getter>; }, throughRepositoryGetter: Getter>): HasManyThroughRepositoryFactory; /** * @deprecated * Function to create a belongs to accessor * * Use `this.createBelongsToAccessorFor()` instead * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance */ protected _createBelongsToAccessorFor(relationName: string, targetRepositoryGetter: Getter> | { [repoType: string]: Getter>; }): BelongsToAccessor; /** * Function to create a belongs to accessor * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance */ protected createBelongsToAccessorFor(relationName: string, targetRepositoryGetter: Getter> | { [repoType: string]: Getter>; }): BelongsToAccessor; /** * @deprecated * Function to create a constrained hasOne relation repository factory * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance */ protected _createHasOneRepositoryFactoryFor(relationName: string, targetRepositoryGetter: Getter> | { [repoType: string]: Getter>; }): HasOneRepositoryFactory; /** * Function to create a constrained hasOne relation repository factory * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance */ protected createHasOneRepositoryFactoryFor(relationName: string, targetRepositoryGetter: Getter> | { [repoType: string]: Getter>; }): HasOneRepositoryFactory; /** * @deprecated * Function to create a references many accessor * * Use `this.createReferencesManyAccessorFor()` instead * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance */ protected _createReferencesManyAccessorFor(relationName: string, targetRepoGetter: Getter>): ReferencesManyAccessor; /** * Function to create a references many accessor * * @param relationName - Name of the relation defined on the source model * @param targetRepo - Target repository instance */ protected createReferencesManyAccessorFor(relationName: string, targetRepoGetter: Getter>): ReferencesManyAccessor; create(entity: DataObject, options?: Options): Promise; createAll(entities: DataObject[], options?: Options): Promise; save(entity: T, options?: Options): Promise; find(filter?: Filter, options?: Options): Promise<(T & Relations)[]>; findOne(filter?: Filter, options?: Options): Promise<(T & Relations) | null>; findById(id: ID, filter?: FilterExcludingWhere, options?: Options): Promise; update(entity: T, options?: Options): Promise; delete(entity: T, 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 a SQL command. * * **WARNING:** In general, it is always better to perform database actions * through repository methods. Directly executing SQL may lead to unexpected * results, corrupted data, security vulnerabilities and other issues. * * @example * * ```ts * // MySQL * const result = await repo.execute( * 'SELECT * FROM Products WHERE size > ?', * [42] * ); * * // PostgreSQL * const result = await repo.execute( * 'SELECT * FROM Products WHERE size > $1', * [42] * ); * ``` * * @param command A parameterized SQL command or query. * Check your database documentation for information on which characters to * use as parameter placeholders. * @param parameters List of parameter values to use. * @param options Additional options, for example `transaction`. * @returns A promise which resolves to the command output as returned by the * database driver. The output type (data structure) is database specific and * often depends on the command executed. */ execute(command: Command, parameters: NamedParameters | PositionalParameters, options?: Options): Promise; /** * Execute a MongoDB command. * * **WARNING:** In general, it is always better to perform database actions * through repository methods. Directly executing MongoDB commands may lead * to unexpected results and other issues. * * @example * * ```ts * const result = await repo.execute('MyCollection', 'aggregate', [ * {$lookup: { * // ... * }}, * {$unwind: '$data'}, * {$out: 'tempData'} * ]); * ``` * * @param collectionName The name of the collection to execute the command on. * @param command The command name. See * [Collection API docs](http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html) * for the list of commands supported by the MongoDB client. * @param parameters Command parameters (arguments), as described in MongoDB API * docs for individual collection methods. * @returns A promise which resolves to the command output as returned by the * database driver. */ execute(collectionName: string, command: string, ...parameters: PositionalParameters): Promise; /** * Execute a raw database command using a connector that's not described * by LoopBack's `execute` API yet. * * **WARNING:** In general, it is always better to perform database actions * through repository methods. Directly executing database commands may lead * to unexpected results and other issues. * * @param args Command and parameters, please consult your connector's * documentation to learn about supported commands and their parameters. * @returns A promise which resolves to the command output as returned by the * database driver. */ execute(...args: PositionalParameters): Promise; protected toEntity(model: juggler.PersistedModel): R; protected toEntities(models: juggler.PersistedModel[]): R[]; /** * Register an inclusion resolver for the related model name. * * @param relationName - Name of the relation defined on the source model * @param resolver - Resolver function for getting related model entities */ registerInclusionResolver(relationName: string, resolver: InclusionResolver): void; /** * Returns model instances that include related models of this repository * that have a registered resolver. * * @param entities - An array of entity instances or data * @param include -Inclusion filter * @param options - Options for the operations */ protected includeRelatedModels(entities: T[], include?: InclusionFilter[], options?: Options): Promise<(T & Relations)[]>; /** * This function works as a persist hook. * It converts an entity from the CRUD operations' caller * to a persistable data that can will be stored in the * back-end database. * * User can extend `DefaultCrudRepository` then override this * function to execute custom persist hook. * @param entity The entity passed from CRUD operations' caller. * @param options */ protected entityToData(entity: R | DataObject, options?: {}): legacy.ModelData; /** Converts an entity object to a JSON object to check if it contains navigational property. * Throws an error if `entity` contains navigational property. * * @param entity The entity passed from CRUD operations' caller. * @param options */ protected ensurePersistable(entity: R | DataObject, options?: {}): legacy.ModelData; /** * Removes juggler's "include" filter as it does not apply to LoopBack 4 * relations. * * @param filter - Query filter */ protected normalizeFilter(filter?: Filter): legacy.Filter | undefined; } /** * Default implementation of CRUD repository using legacy juggler model * and data source with beginTransaction() method for connectors which * support Transactions */ export declare class DefaultTransactionalRepository extends DefaultCrudRepository implements TransactionalEntityRepository { beginTransaction(options?: IsolationLevel | Options): Promise; }