UNPKG

lakutata

Version:

An IoC-based universal application framework.

1,069 lines (1,058 loc) 46 kB
import { ObjectLiteral, EntityTarget, DeepPartial, PickKeysByType, ObjectType } from './TypeDef.internal.36.js'; import { MongoFindManyOptions, MongoFindOneOptions } from './TypeDef.internal.82.js'; import { MongoEntityManager, EntityManager } from './TypeDef.internal.35.js'; import { QueryRunner } from './TypeDef.internal.40.js'; import { SelectQueryBuilder, InsertOrUpdateOptions, QueryDeepPartialEntity } from './TypeDef.internal.38.js'; import { FindManyOptions, FindOneOptions, FindTreeOptions, FindOptionsWhere } from './TypeDef.internal.74.js'; import { FilterOperators, ObjectId, Filter, FindCursor, AggregateOptions, AggregationCursor, AnyBulkWriteOperation, BulkWriteOptions, BulkWriteResult, CountOptions, CountDocumentsOptions, CreateIndexesOptions, IndexDescription, DeleteOptions, DeleteResult, CommandOperationOptions, FindOneAndDeleteOptions, Document, FindOneAndReplaceOptions, FindOneAndUpdateOptions, OrderedBulkOperation, UnorderedBulkOperation, InsertManyResult, InsertOneOptions, InsertOneResult, ListIndexesOptions, ListIndexesCursor, Collection, ReplaceOptions, UpdateResult, CollStatsOptions, CollStats, UpdateFilter, UpdateOptions } from './TypeDef.internal.55.js'; import './TypeDef.internal.30.js'; import { EntityMetadata } from './TypeDef.internal.47.js'; import { InsertResult, UpdateResult as UpdateResult$1, DeleteResult as DeleteResult$1 } from './TypeDef.internal.81.js'; import { UpsertType } from './TypeDef.internal.44.js'; import { DataSource } from './TypeDef.internal.33.js'; /** * Special options passed to Repository#save, Repository#insert and Repository#update methods. */ interface SaveOptions { /** * Additional data to be passed with persist method. * This data can be used in subscribers then. */ data?: any; /** * Indicates if listeners and subscribers are called for this operation. * By default they are enabled, you can disable them by setting { listeners: false } in save/remove options. */ listeners?: boolean; /** * By default transactions are enabled and all queries in persistence operation are wrapped into the transaction. * You can disable this behaviour by setting { transaction: false } in the persistence options. */ transaction?: boolean; /** * Breaks save execution into chunks of a given size. * For example, if you want to save 100,000 objects but you have issues with saving them, * you can break them into 10 groups of 10,000 objects (by setting { chunk: 10000 }) and save each group separately. * This option is needed to perform very big insertions when you have issues with underlying driver parameter number limitation. */ chunk?: number; /** * Flag to determine whether the entity that is being persisted * should be reloaded during the persistence operation. * * It will work only on databases which does not support RETURNING / OUTPUT statement. * Enabled by default. */ reload?: boolean; } /** * Special options passed to Repository#remove and Repository#delete methods. */ interface RemoveOptions { /** * Additional data to be passed with remove method. * This data can be used in subscribers then. */ data?: any; /** * Indicates if listeners and subscribers are called for this operation. * By default they are enabled, you can disable them by setting { listeners: false } in save/remove options. */ listeners?: boolean; /** * By default transactions are enabled and all queries in persistence operation are wrapped into the transaction. * You can disable this behaviour by setting { transaction: false } in the persistence options. */ transaction?: boolean; /** * Breaks save execution into given number of chunks. * For example, if you want to save 100,000 objects but you have issues with saving them, * you can break them into 10 groups of 10,000 objects (by setting { chunk: 10000 }) and save each group separately. * This option is needed to perform very big insertions when you have issues with underlying driver parameter number limitation. */ chunk?: number; } /** * Repository used to manage mongodb documents of a single entity type. */ declare class MongoRepository<Entity extends ObjectLiteral> extends Repository<Entity> { /** * Entity Manager used by this repository. */ readonly manager: MongoEntityManager; /** * Raw SQL query execution is not supported by MongoDB. * Calling this method will return an error. */ query(query: string, parameters?: any[]): Promise<any>; /** * Using Query Builder with MongoDB is not supported yet. * Calling this method will return an error. */ createQueryBuilder(alias: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>; /** * Finds entities that match given find options or conditions. */ find(options?: FindManyOptions<Entity> | Partial<Entity> | FilterOperators<Entity>): Promise<Entity[]>; /** * Finds entities that match given find options or conditions. */ findBy(where: any): Promise<Entity[]>; /** * Finds entities that match given find options or conditions. * Also counts all entities that match given conditions, * but ignores pagination settings (from and take options). */ findAndCount(options?: MongoFindManyOptions<Entity>): Promise<[Entity[], number]>; /** * Finds entities that match given find options or conditions. * Also counts all entities that match given conditions, * but ignores pagination settings (from and take options). */ findAndCountBy(where: any): Promise<[Entity[], number]>; /** * Finds entities by ids. * Optionally find options can be applied. * * @deprecated use `findBy` method instead in conjunction with `In` operator, for example: * * .findBy({ * id: In([1, 2, 3]) * }) */ findByIds(ids: any[], options?: any): Promise<Entity[]>; /** * Finds first entity that matches given find options. */ findOne(options: MongoFindOneOptions<Entity>): Promise<Entity | null>; /** * Finds first entity that matches given WHERE conditions. */ findOneBy(where: any): Promise<Entity | null>; /** * Finds entity that matches given id. * * @deprecated use `findOneBy` method instead in conjunction with `In` operator, for example: * * .findOneBy({ * id: 1 // where "id" is your primary column name * }) */ findOneById(id: string | number | Date | ObjectId): Promise<Entity | null>; /** * Finds first entity by a given find options. * If entity was not found in the database - rejects with error. */ findOneOrFail(options: FindOneOptions<Entity>): Promise<Entity>; /** * Finds first entity that matches given where condition. * If entity was not found in the database - rejects with error. */ findOneByOrFail(where: any): Promise<Entity>; /** * Creates a cursor for a query that can be used to iterate over results from MongoDB. */ createCursor<T = any>(query?: Filter<Entity>): FindCursor<T>; /** * Creates a cursor for a query that can be used to iterate over results from MongoDB. * This returns modified version of cursor that transforms each result into Entity model. */ createEntityCursor(query?: Filter<Entity>): FindCursor<Entity>; /** * Execute an aggregation framework pipeline against the collection. */ aggregate<R = any>(pipeline: ObjectLiteral[], options?: AggregateOptions): AggregationCursor<R>; /** * Execute an aggregation framework pipeline against the collection. * This returns modified version of cursor that transforms each result into Entity model. */ aggregateEntity(pipeline: ObjectLiteral[], options?: AggregateOptions): AggregationCursor<Entity>; /** * Perform a bulkWrite operation without a fluent API. */ bulkWrite(operations: AnyBulkWriteOperation[], options?: BulkWriteOptions): Promise<BulkWriteResult>; /** * Count number of matching documents in the db to a query. */ count(query?: ObjectLiteral, options?: CountOptions): Promise<number>; /** * Count number of matching documents in the db to a query. */ countDocuments(query?: ObjectLiteral, options?: CountDocumentsOptions): Promise<number>; /** * Count number of matching documents in the db to a query. */ countBy(query?: ObjectLiteral, options?: CountOptions): Promise<number>; /** * Creates an index on the db and collection. */ createCollectionIndex(fieldOrSpec: string | any, options?: CreateIndexesOptions): Promise<string>; /** * Creates multiple indexes in the collection, this method is only supported for MongoDB 2.6 or higher. * Earlier version of MongoDB will throw a command not supported error. * Index specifications are defined at http://docs.mongodb.org/manual/reference/command/createIndexes/. */ createCollectionIndexes(indexSpecs: IndexDescription[]): Promise<string[]>; /** * Delete multiple documents on MongoDB. */ deleteMany(query: ObjectLiteral, options?: DeleteOptions): Promise<DeleteResult>; /** * Delete a document on MongoDB. */ deleteOne(query: ObjectLiteral, options?: DeleteOptions): Promise<DeleteResult>; /** * The distinct command returns returns a list of distinct values for the given key across a collection. */ distinct(key: string, query: ObjectLiteral, options?: CommandOperationOptions): Promise<any>; /** * Drops an index from this collection. */ dropCollectionIndex(indexName: string, options?: CommandOperationOptions): Promise<any>; /** * Drops all indexes from the collection. */ dropCollectionIndexes(): Promise<any>; /** * Find a document and delete it in one atomic operation, requires a write lock for the duration of the operation. */ findOneAndDelete(query: ObjectLiteral, options?: FindOneAndDeleteOptions): Promise<Document | null>; /** * Find a document and replace it in one atomic operation, requires a write lock for the duration of the operation. */ findOneAndReplace(query: ObjectLiteral, replacement: Object, options?: FindOneAndReplaceOptions): Promise<Document | null>; /** * Find a document and update it in one atomic operation, requires a write lock for the duration of the operation. */ findOneAndUpdate(query: ObjectLiteral, update: Object, options?: FindOneAndUpdateOptions): Promise<Document | null>; /** * Retrieve all the indexes on the collection. */ collectionIndexes(): Promise<any>; /** * Retrieve all the indexes on the collection. */ collectionIndexExists(indexes: string | string[]): Promise<boolean>; /** * Retrieves this collections index info. */ collectionIndexInformation(options?: { full: boolean; }): Promise<any>; /** * Initiate an In order bulk write operation, operations will be serially executed in the order they are added, creating a new operation for each switch in types. */ initializeOrderedBulkOp(options?: BulkWriteOptions): OrderedBulkOperation; /** * Initiate a Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order. */ initializeUnorderedBulkOp(options?: BulkWriteOptions): UnorderedBulkOperation; /** * Inserts an array of documents into MongoDB. */ insertMany(docs: ObjectLiteral[], options?: BulkWriteOptions): Promise<InsertManyResult<Document>>; /** * Inserts a single document into MongoDB. */ insertOne(doc: ObjectLiteral, options?: InsertOneOptions): Promise<InsertOneResult>; /** * Returns if the collection is a capped collection. */ isCapped(): Promise<any>; /** * Get the list of all indexes information for the collection. */ listCollectionIndexes(options?: ListIndexesOptions): ListIndexesCursor; /** * Reindex all indexes on the collection Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections. */ rename(newName: string, options?: { dropTarget?: boolean; }): Promise<Collection<Document>>; /** * Replace a document on MongoDB. */ replaceOne(query: ObjectLiteral, doc: ObjectLiteral, options?: ReplaceOptions): Promise<Document | UpdateResult>; /** * Get all the collection statistics. */ stats(options?: CollStatsOptions): Promise<CollStats>; /** * Update multiple documents on MongoDB. */ updateMany(query: ObjectLiteral, update: UpdateFilter<Document>, options?: UpdateOptions): Promise<Document | UpdateResult>; /** * Update a single document on MongoDB. */ updateOne(query: ObjectLiteral, update: UpdateFilter<Document>, options?: UpdateOptions): Promise<Document | UpdateResult>; } /** * Repository with additional functions to work with trees. * * @see Repository */ declare class TreeRepository<Entity extends ObjectLiteral> extends Repository<Entity> { /** * Gets complete trees for all roots in the table. */ findTrees(options?: FindTreeOptions): Promise<Entity[]>; /** * Roots are entities that have no ancestors. Finds them all. */ findRoots(options?: FindTreeOptions): Promise<Entity[]>; /** * Gets all children (descendants) of the given entity. Returns them all in a flat array. */ findDescendants(entity: Entity, options?: FindTreeOptions): Promise<Entity[]>; /** * Gets all children (descendants) of the given entity. Returns them in a tree - nested into each other. */ findDescendantsTree(entity: Entity, options?: FindTreeOptions): Promise<Entity>; /** * Gets number of descendants of the entity. */ countDescendants(entity: Entity): Promise<number>; /** * Creates a query builder used to get descendants of the entities in a tree. */ createDescendantsQueryBuilder(alias: string, closureTableAlias: string, entity: Entity): SelectQueryBuilder<Entity>; /** * Gets all parents (ancestors) of the given entity. Returns them all in a flat array. */ findAncestors(entity: Entity, options?: FindTreeOptions): Promise<Entity[]>; /** * Gets all parents (ancestors) of the given entity. Returns them in a tree - nested into each other. */ findAncestorsTree(entity: Entity, options?: FindTreeOptions): Promise<Entity>; /** * Gets number of ancestors of the entity. */ countAncestors(entity: Entity): Promise<number>; /** * Creates a query builder used to get ancestors of the entities in the tree. */ createAncestorsQueryBuilder(alias: string, closureTableAlias: string, entity: Entity): SelectQueryBuilder<Entity>; } /** * Special options passed to Repository#upsert */ interface UpsertOptions<Entity> extends InsertOrUpdateOptions { conflictPaths: string[] | { [P in keyof Entity]?: true; }; /** * If true, postgres will skip the update if no values would be changed (reduces writes) */ skipUpdateIfNoValuesChanged?: boolean; /** * Define the type of upsert to use (currently, CockroachDB only). * * If none provided, it will use the default for the database (first one in the list) */ upsertType?: UpsertType; } /** * Repository is supposed to work with your entity objects. Find entities, insert, update, delete, etc. */ declare class Repository<Entity extends ObjectLiteral> { /** * Entity target that is managed by this repository. * If this repository manages entity from schema, * then it returns a name of that schema instead. */ readonly target: EntityTarget<Entity>; /** * Entity Manager used by this repository. */ readonly manager: EntityManager; /** * Query runner provider used for this repository. */ readonly queryRunner?: QueryRunner; /** * Entity metadata of the entity current repository manages. */ get metadata(): EntityMetadata; constructor(target: EntityTarget<Entity>, manager: EntityManager, queryRunner?: QueryRunner); /** * Creates a new query builder that can be used to build a SQL query. */ createQueryBuilder(alias?: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>; /** * Checks if entity has an id. * If entity composite compose ids, it will check them all. */ hasId(entity: Entity): boolean; /** * Gets entity mixed id. */ getId(entity: Entity): any; /** * Creates a new entity instance. */ create(): Entity; /** * Creates new entities and copies all entity properties from given objects into their new entities. * Note that it copies only properties that are present in entity schema. */ create(entityLikeArray: DeepPartial<Entity>[]): Entity[]; /** * Creates a new entity instance and copies all entity properties from this object into a new entity. * Note that it copies only properties that are present in entity schema. */ create(entityLike: DeepPartial<Entity>): Entity; /** * Merges multiple entities (or entity-like objects) into a given entity. */ merge(mergeIntoEntity: Entity, ...entityLikes: DeepPartial<Entity>[]): Entity; /** * Creates a new entity from the given plain javascript object. If entity already exist in the database, then * it loads it (and everything related to it), replaces all values with the new ones from the given object * and returns this new entity. This new entity is actually a loaded from the db entity with all properties * replaced from the new object. * * Note that given entity-like object must have an entity id / primary key to find entity by. * Returns undefined if entity with given id was not found. */ preload(entityLike: DeepPartial<Entity>): Promise<Entity | undefined>; /** * Saves all given entities in the database. * If entities do not exist in the database then inserts, otherwise updates. */ save<T extends DeepPartial<Entity>>(entities: T[], options: SaveOptions & { reload: false; }): Promise<T[]>; /** * Saves all given entities in the database. * If entities do not exist in the database then inserts, otherwise updates. */ save<T extends DeepPartial<Entity>>(entities: T[], options?: SaveOptions): Promise<(T & Entity)[]>; /** * Saves a given entity in the database. * If entity does not exist in the database then inserts, otherwise updates. */ save<T extends DeepPartial<Entity>>(entity: T, options: SaveOptions & { reload: false; }): Promise<T>; /** * Saves a given entity in the database. * If entity does not exist in the database then inserts, otherwise updates. */ save<T extends DeepPartial<Entity>>(entity: T, options?: SaveOptions): Promise<T & Entity>; /** * Removes a given entities from the database. */ remove(entities: Entity[], options?: RemoveOptions): Promise<Entity[]>; /** * Removes a given entity from the database. */ remove(entity: Entity, options?: RemoveOptions): Promise<Entity>; /** * Records the delete date of all given entities. */ softRemove<T extends DeepPartial<Entity>>(entities: T[], options: SaveOptions & { reload: false; }): Promise<T[]>; /** * Records the delete date of all given entities. */ softRemove<T extends DeepPartial<Entity>>(entities: T[], options?: SaveOptions): Promise<(T & Entity)[]>; /** * Records the delete date of a given entity. */ softRemove<T extends DeepPartial<Entity>>(entity: T, options: SaveOptions & { reload: false; }): Promise<T>; /** * Records the delete date of a given entity. */ softRemove<T extends DeepPartial<Entity>>(entity: T, options?: SaveOptions): Promise<T & Entity>; /** * Recovers all given entities in the database. */ recover<T extends DeepPartial<Entity>>(entities: T[], options: SaveOptions & { reload: false; }): Promise<T[]>; /** * Recovers all given entities in the database. */ recover<T extends DeepPartial<Entity>>(entities: T[], options?: SaveOptions): Promise<(T & Entity)[]>; /** * Recovers a given entity in the database. */ recover<T extends DeepPartial<Entity>>(entity: T, options: SaveOptions & { reload: false; }): Promise<T>; /** * Recovers a given entity in the database. */ recover<T extends DeepPartial<Entity>>(entity: T, options?: SaveOptions): Promise<T & Entity>; /** * Inserts a given entity into the database. * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient INSERT query. * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted. */ insert(entity: QueryDeepPartialEntity<Entity> | QueryDeepPartialEntity<Entity>[]): Promise<InsertResult>; /** * Updates entity partially. Entity can be found by a given conditions. * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient UPDATE query. * Does not check if entity exist in the database. */ update(criteria: string | string[] | number | number[] | Date | Date[] | ObjectId | ObjectId[] | FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[], partialEntity: QueryDeepPartialEntity<Entity>): Promise<UpdateResult$1>; /** * Updates all entities of target type, setting fields from supplied partial entity. * This is a primitive operation without cascades, relations or other operations included. * Executes fast and efficient UPDATE query without WHERE clause. * * WARNING! This method updates ALL rows in the target table. */ updateAll(partialEntity: QueryDeepPartialEntity<Entity>): Promise<UpdateResult$1>; /** * Inserts a given entity into the database, unless a unique constraint conflicts then updates the entity * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query. */ upsert(entityOrEntities: QueryDeepPartialEntity<Entity> | QueryDeepPartialEntity<Entity>[], conflictPathsOrOptions: string[] | UpsertOptions<Entity>): Promise<InsertResult>; /** * Deletes entities by a given criteria. * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient DELETE query. * Does not check if entity exist in the database. */ delete(criteria: string | string[] | number | number[] | Date | Date[] | ObjectId | ObjectId[] | FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<DeleteResult$1>; /** * Deletes all entities of target type. * This is a primitive operation without cascades, relations or other operations included. * Executes fast and efficient DELETE query without WHERE clause. * * WARNING! This method deletes ALL rows in the target table. */ deleteAll(): Promise<DeleteResult$1>; /** * Records the delete date of entities by a given criteria. * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient SOFT-DELETE query. * Does not check if entity exist in the database. */ softDelete(criteria: string | string[] | number | number[] | Date | Date[] | ObjectId | ObjectId[] | FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<UpdateResult$1>; /** * Restores entities by a given criteria. * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient SOFT-DELETE query. * Does not check if entity exist in the database. */ restore(criteria: string | string[] | number | number[] | Date | Date[] | ObjectId | ObjectId[] | FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<UpdateResult$1>; /** * Checks whether any entity exists that matches the given options. * * @deprecated use `exists` method instead, for example: * * .exists() */ exist(options?: FindManyOptions<Entity>): Promise<boolean>; /** * Checks whether any entity exists that matches the given options. */ exists(options?: FindManyOptions<Entity>): Promise<boolean>; /** * Checks whether any entity exists that matches the given conditions. */ existsBy(where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<boolean>; /** * Counts entities that match given options. * Useful for pagination. */ count(options?: FindManyOptions<Entity>): Promise<number>; /** * Counts entities that match given conditions. * Useful for pagination. */ countBy(where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<number>; /** * Return the SUM of a column */ sum(columnName: PickKeysByType<Entity, number>, where?: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<number | null>; /** * Return the AVG of a column */ average(columnName: PickKeysByType<Entity, number>, where?: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<number | null>; /** * Return the MIN of a column */ minimum(columnName: PickKeysByType<Entity, number>, where?: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<number | null>; /** * Return the MAX of a column */ maximum(columnName: PickKeysByType<Entity, number>, where?: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<number | null>; /** * Finds entities that match given find options. */ find(options?: FindManyOptions<Entity>): Promise<Entity[]>; /** * Finds entities that match given find options. */ findBy(where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<Entity[]>; /** * Finds entities that match given find options. * Also counts all entities that match given conditions, * but ignores pagination settings (from and take options). */ findAndCount(options?: FindManyOptions<Entity>): Promise<[Entity[], number]>; /** * Finds entities that match given WHERE conditions. * Also counts all entities that match given conditions, * but ignores pagination settings (from and take options). */ findAndCountBy(where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<[Entity[], number]>; /** * Finds entities with ids. * Optionally find options or conditions can be applied. * * @deprecated use `findBy` method instead in conjunction with `In` operator, for example: * * .findBy({ * id: In([1, 2, 3]) * }) */ findByIds(ids: any[]): Promise<Entity[]>; /** * Finds first entity by a given find options. * If entity was not found in the database - returns null. */ findOne(options: FindOneOptions<Entity>): Promise<Entity | null>; /** * Finds first entity that matches given where condition. * If entity was not found in the database - returns null. */ findOneBy(where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<Entity | null>; /** * Finds first entity that matches given id. * If entity was not found in the database - returns null. * * @deprecated use `findOneBy` method instead in conjunction with `In` operator, for example: * * .findOneBy({ * id: 1 // where "id" is your primary column name * }) */ findOneById(id: number | string | Date | ObjectId): Promise<Entity | null>; /** * Finds first entity by a given find options. * If entity was not found in the database - rejects with error. */ findOneOrFail(options: FindOneOptions<Entity>): Promise<Entity>; /** * Finds first entity that matches given where condition. * If entity was not found in the database - rejects with error. */ findOneByOrFail(where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[]): Promise<Entity>; /** * Executes a raw SQL query and returns a raw database results. * Raw query execution is supported only by relational databases (MongoDB is not supported). * * @see [Official docs](https://typeorm.io/repository-api) for examples. */ query<T = any>(query: string, parameters?: any[]): Promise<T>; /** * Tagged template function that executes raw SQL query and returns raw database results. * Template expressions are automatically transformed into database parameters. * Raw query execution is supported only by relational databases (MongoDB is not supported). * Note: Don't call this as a regular function, it is meant to be used with backticks to tag a template literal. * Example: repository.sql`SELECT * FROM table_name WHERE id = ${id}` */ sql<T = any>(strings: TemplateStringsArray, ...values: unknown[]): Promise<T>; /** * Clears all the data from the given table/collection (truncates/drops it). * * Note: this method uses TRUNCATE and may not work as you expect in transactions on some platforms. * @see https://stackoverflow.com/a/5972738/925151 */ clear(): Promise<void>; /** * Increments some column by provided value of the entities matched given conditions. */ increment(conditions: FindOptionsWhere<Entity>, propertyPath: string, value: number | string): Promise<UpdateResult$1>; /** * Decrements some column by provided value of the entities matched given conditions. */ decrement(conditions: FindOptionsWhere<Entity>, propertyPath: string, value: number | string): Promise<UpdateResult$1>; /** * Extends repository with provided functions. */ extend<CustomRepository>(customs: CustomRepository & ThisType<this & CustomRepository>): this & CustomRepository; } /** * Provides abstract class for custom repositories that do not inherit from original orm Repository. * Contains all most-necessary methods to simplify code in the custom repository. * All methods are protected thus not exposed and it allows to create encapsulated custom repository. * * @deprecated use Repository.extend function to create a custom repository */ declare class AbstractRepository<Entity extends ObjectLiteral> { /** * Gets entity manager that allows to perform repository operations with any entity. */ protected manager: EntityManager; /** * Gets the original ORM repository for the entity that is managed by this repository. * If current repository does not manage any entity, then exception will be thrown. */ protected get repository(): Repository<Entity>; /** * Gets the original ORM tree repository for the entity that is managed by this repository. * If current repository does not manage any entity, then exception will be thrown. */ protected get treeRepository(): TreeRepository<Entity>; /** * Creates a new query builder for the repository's entity that can be used to build a SQL query. * If current repository does not manage any entity, then exception will be thrown. */ protected createQueryBuilder(alias: string): SelectQueryBuilder<Entity>; /** * Creates a new query builder for the given entity that can be used to build a SQL query. */ protected createQueryBuilderFor<T extends ObjectLiteral>(entity: ObjectType<T>, alias: string): SelectQueryBuilder<T>; /** * Gets the original ORM repository for the given entity class. */ protected getRepositoryFor<T extends ObjectLiteral>(entity: ObjectType<T>): Repository<T>; /** * Gets the original ORM tree repository for the given entity class. */ protected getTreeRepositoryFor<T extends ObjectLiteral>(entity: ObjectType<T>): TreeRepository<T>; /** * Gets custom repository's managed entity. * If given custom repository does not manage any entity then undefined will be returned. */ private getCustomRepositoryTarget; } /** * Base abstract entity for all entities, used in ActiveRecord patterns. */ declare class BaseEntity { /** * DataSource used in all static methods of the BaseEntity. */ private static dataSource; /** * Checks if entity has an id. * If entity composite compose ids, it will check them all. */ hasId(): boolean; /** * Saves current entity in the database. * If entity does not exist in the database then inserts, otherwise updates. */ save(options?: SaveOptions): Promise<this>; /** * Removes current entity from the database. */ remove(options?: RemoveOptions): Promise<this>; /** * Records the delete date of current entity. */ softRemove(options?: SaveOptions): Promise<this>; /** * Recovers a given entity in the database. */ recover(options?: SaveOptions): Promise<this>; /** * Reloads entity data from the database. */ reload(): Promise<void>; /** * Sets DataSource to be used by entity. */ static useDataSource(dataSource: DataSource | null): void; /** * Gets current entity's Repository. */ static getRepository<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity): Repository<T>; /** * Returns object that is managed by this repository. * If this repository manages entity from schema, * then it returns a name of that schema instead. */ static get target(): EntityTarget<any>; /** * Checks entity has an id. * If entity composite compose ids, it will check them all. */ static hasId(entity: BaseEntity): boolean; /** * Gets entity mixed id. */ static getId<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entity: T): any; /** * Creates a new query builder that can be used to build a SQL query. */ static createQueryBuilder<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, alias?: string): SelectQueryBuilder<T>; /** * Creates a new entity instance. */ static create<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity): T; /** * Creates a new entities and copies all entity properties from given objects into their new entities. * Note that it copies only properties that present in entity schema. */ static create<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entityLikeArray: DeepPartial<T>[]): T[]; /** * Creates a new entity instance and copies all entity properties from this object into a new entity. * Note that it copies only properties that present in entity schema. */ static create<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entityLike: DeepPartial<T>): T; /** * Merges multiple entities (or entity-like objects) into a given entity. */ static merge<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, mergeIntoEntity: T, ...entityLikes: DeepPartial<T>[]): T; /** * Creates a new entity from the given plain javascript object. If entity already exist in the database, then * it loads it (and everything related to it), replaces all values with the new ones from the given object * and returns this new entity. This new entity is actually a loaded from the db entity with all properties * replaced from the new object. * * Note that given entity-like object must have an entity id / primary key to find entity by. * Returns undefined if entity with given id was not found. */ static preload<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entityLike: DeepPartial<T>): Promise<T | undefined>; /** * Saves all given entities in the database. * If entities do not exist in the database then inserts, otherwise updates. */ static save<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entities: DeepPartial<T>[], options?: SaveOptions): Promise<T[]>; /** * Saves a given entity in the database. * If entity does not exist in the database then inserts, otherwise updates. */ static save<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entity: DeepPartial<T>, options?: SaveOptions): Promise<T>; /** * Removes a given entities from the database. */ static remove<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entities: T[], options?: RemoveOptions): Promise<T[]>; /** * Removes a given entity from the database. */ static remove<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entity: T, options?: RemoveOptions): Promise<T>; /** * Records the delete date of all given entities. */ static softRemove<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entities: T[], options?: SaveOptions): Promise<T[]>; /** * Records the delete date of a given entity. */ static softRemove<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entity: T, options?: SaveOptions): Promise<T>; /** * Inserts a given entity into the database. * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient INSERT query. * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted. */ static insert<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entity: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T>[]): Promise<InsertResult>; /** * Updates entity partially. Entity can be found by a given conditions. * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient UPDATE query. * Does not check if entity exist in the database. */ static update<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, criteria: string | string[] | number | number[] | Date | Date[] | ObjectId | ObjectId[] | FindOptionsWhere<T>, partialEntity: QueryDeepPartialEntity<T>): Promise<UpdateResult$1>; /** * Inserts a given entity into the database, unless a unique constraint conflicts then updates the entity * Unlike save method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query. */ static upsert<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, entityOrEntities: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T>[], conflictPathsOrOptions: string[] | UpsertOptions<T>): Promise<InsertResult>; /** * Deletes entities by a given criteria. * Unlike remove method executes a primitive operation without cascades, relations and other operations included. * Executes fast and efficient DELETE query. * Does not check if entity exist in the database. */ static delete<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, criteria: string | string[] | number | number[] | Date | Date[] | ObjectId | ObjectId[] | FindOptionsWhere<T>): Promise<DeleteResult$1>; /** * Checks whether any entity exists that matches the given options. */ static exists<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, options?: FindManyOptions<T>): Promise<boolean>; /** * Checks whether any entity exists that matches the given conditions. */ static existsBy<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<boolean>; /** * Counts entities that match given options. */ static count<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, options?: FindManyOptions<T>): Promise<number>; /** * Counts entities that match given WHERE conditions. */ static countBy<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<number>; /** * Return the SUM of a column */ static sum<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, columnName: PickKeysByType<T, number>, where: FindOptionsWhere<T>): Promise<number | null>; /** * Return the AVG of a column */ static average<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, columnName: PickKeysByType<T, number>, where: FindOptionsWhere<T>): Promise<number | null>; /** * Return the MIN of a column */ static minimum<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, columnName: PickKeysByType<T, number>, where: FindOptionsWhere<T>): Promise<number | null>; /** * Return the MAX of a column */ static maximum<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, columnName: PickKeysByType<T, number>, where: FindOptionsWhere<T>): Promise<number | null>; /** * Finds entities that match given options. */ static find<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, options?: FindManyOptions<T>): Promise<T[]>; /** * Finds entities that match given WHERE conditions. */ static findBy<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<T[]>; /** * Finds entities that match given find options. * Also counts all entities that match given conditions, * but ignores pagination settings (from and take options). */ static findAndCount<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, options?: FindManyOptions<T>): Promise<[T[], number]>; /** * Finds entities that match given WHERE conditions. * Also counts all entities that match given conditions, * but ignores pagination settings (from and take options). */ static findAndCountBy<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<[T[], number]>; /** * Finds entities by ids. * Optionally find options can be applied. * * @deprecated use `findBy` method instead in conjunction with `In` operator, for example: * * .findBy({ * id: In([1, 2, 3]) * }) */ static findByIds<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, ids: any[]): Promise<T[]>; /** * Finds first entity that matches given conditions. */ static findOne<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, options: FindOneOptions<T>): Promise<T | null>; /** * Finds first entity that matches given conditions. */ static findOneBy<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<T | null>; /** * Finds first entity that matches given options. * * @deprecated use `findOneBy` method instead in conjunction with `In` operator, for example: * * .findOneBy({ * id: 1 // where "id" is your primary column name * }) */ static findOneById<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, id: string | number | Date | ObjectId): Promise<T | null>; /** * Finds first entity that matches given conditions. */ static findOneOrFail<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, options: FindOneOptions<T>): Promise<T>; /** * Finds first entity that matches given conditions. */ static findOneByOrFail<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<T>; /** * Executes a raw SQL query and returns a raw database results. * Raw query execution is supported only by relational databases (MongoDB is not supported). */ static query<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity, query: string, parameters?: any[]): Promise<any>; /** * Clears all the data from the given table/collection (truncates/drops it). */ static clear<T extends BaseEntity>(this: { new (): T; } & typeof BaseEntity): Promise<void>; } /** * Special options passed to TreeRepository#findTrees */ interface FindTreesOptions { /** * When loading a tree from a TreeRepository, limits the depth of the descendents loaded */ depth?: number; } export { AbstractRepository, BaseEntity, MongoRepository, Repository, TreeRepository }; export type { FindTreesOptions, RemoveOptions, SaveOptions, UpsertOptions };