import {EntityMetadata} from "../metadata/EntityMetadata"; import {FindManyOptions} from "../find-options/FindManyOptions"; import {ObjectLiteral} from "../common/ObjectLiteral"; import {FindOneOptions} from "../find-options/FindOneOptions"; import {DeepPartial} from "../common/DeepPartial"; import {SaveOptions} from "./SaveOptions"; import {RemoveOptions} from "./RemoveOptions"; import {EntityManager} from "../entity-manager/EntityManager"; import {QueryRunner} from "../query-runner/QueryRunner"; import {SelectQueryBuilder} from "../query-builder/SelectQueryBuilder"; import {DeleteResult} from "../query-builder/result/DeleteResult"; import {UpdateResult} from "../query-builder/result/UpdateResult"; import {InsertResult} from "../query-builder/result/InsertResult"; import {QueryPartialEntity} from "../query-builder/QueryPartialEntity"; import {ObjectID} from "../driver/mongodb/typings"; import {FindConditions} from "../find-options/FindConditions"; /** * Repository is supposed to work with your entity objects. Find entities, insert, update, delete, etc. */ export class Repository { // ------------------------------------------------------------------------- // Public Properties // ------------------------------------------------------------------------- /** * Entity Manager used by this repository. */ readonly manager: EntityManager; /** * Entity metadata of the entity current repository manages. */ readonly metadata: EntityMetadata; /** * Query runner provider used for this repository. */ readonly queryRunner?: QueryRunner; // ------------------------------------------------------------------------- // Public Methods // ------------------------------------------------------------------------- /** * Creates a new query builder that can be used to build a sql query. */ createQueryBuilder(alias?: string, queryRunner?: QueryRunner): SelectQueryBuilder { return this.manager.createQueryBuilder(this.metadata.target, alias || this.metadata.targetName, queryRunner || this.queryRunner); } /** * Returns object that is managed by this repository. * If this repository manages entity from schema, * then it returns a name of that schema instead. */ get target(): Function|string { return this.metadata.target; } /** * Checks if entity has an id. * If entity composite compose ids, it will check them all. */ hasId(entity: Entity): boolean { return this.manager.hasId(this.metadata.target, entity); } /** * Gets entity mixed id. */ getId(entity: Entity): any { return this.manager.getId(this.metadata.target, entity); } /** * Creates a new entity instance. */ create(): Entity; /** * 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. */ create(entityLikeArray: DeepPartial[]): 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 present in entity schema. */ create(entityLike: DeepPartial): Entity; /** * Creates a new entity instance or instances. * Can copy properties from the given object into new entities. */ create(plainEntityLikeOrPlainEntityLikes?: DeepPartial|DeepPartial[]): Entity|Entity[] { return this.manager.create(this.metadata.target, plainEntityLikeOrPlainEntityLikes as any); } /** * Merges multiple entities (or entity-like objects) into a given entity. */ merge(mergeIntoEntity: Entity, ...entityLikes: DeepPartial[]): Entity { return this.manager.merge(this.metadata.target, mergeIntoEntity, ...entityLikes); } /** * Creates a new entity from the given plan 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): Promise { return this.manager.preload(this.metadata.target, entityLike); } /** * Saves all given entities in the database. * If entities do not exist in the database then inserts, otherwise updates. */ save>(entities: T[], options?: SaveOptions): Promise; /** * Saves a given entity in the database. * If entity does not exist in the database then inserts, otherwise updates. */ save>(entity: T, options?: SaveOptions): Promise; /** * Saves one or many given entities. */ save>(entityOrEntities: T|T[], options?: SaveOptions): Promise { return this.manager.save(this.metadata.target, entityOrEntities as any, options); } /** * Removes a given entities from the database. */ remove(entities: Entity[], options?: RemoveOptions): Promise; /** * Removes a given entity from the database. */ remove(entity: Entity, options?: RemoveOptions): Promise; /** * Removes one or many given entities. */ remove(entityOrEntities: Entity|Entity[], options?: RemoveOptions): Promise { return this.manager.remove(this.metadata.target, entityOrEntities as any, options); } /** * 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: QueryPartialEntity|(QueryPartialEntity[]), options?: SaveOptions): Promise { return this.manager.insert(this.metadata.target, entity, options); } /** * 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[]|FindConditions, partialEntity: DeepPartial, options?: SaveOptions): Promise { return this.manager.update(this.metadata.target, criteria as any, partialEntity, options); } /** * 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[]|FindConditions, options?: RemoveOptions): Promise { return this.manager.delete(this.metadata.target, criteria as any, options); } /** * Counts entities that match given options. */ count(options?: FindManyOptions): Promise; /** * Counts entities that match given conditions. */ count(conditions?: FindConditions): Promise; /** * Counts entities that match given find options or conditions. */ count(optionsOrConditions?: FindManyOptions|FindConditions): Promise { return this.manager.count(this.metadata.target, optionsOrConditions as any); } /** * Finds entities that match given options. */ find(options?: FindManyOptions): Promise; /** * Finds entities that match given conditions. */ find(conditions?: FindConditions): Promise; /** * Finds entities that match given find options or conditions. */ find(optionsOrConditions?: FindManyOptions|FindConditions): Promise { return this.manager.find(this.metadata.target, optionsOrConditions as any); } /** * 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): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. * Also counts all entities that match given conditions, * but ignores pagination settings (from and take options). */ findAndCount(conditions?: FindConditions): 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). */ findAndCount(optionsOrConditions?: FindManyOptions|FindConditions): Promise<[ Entity[], number ]> { return this.manager.findAndCount(this.metadata.target, optionsOrConditions as any); } /** * Finds entities by ids. * Optionally find options can be applied. */ findByIds(ids: any[], options?: FindManyOptions): Promise; /** * Finds entities by ids. * Optionally conditions can be applied. */ findByIds(ids: any[], conditions?: FindConditions): Promise; /** * Finds entities by ids. * Optionally find options can be applied. */ findByIds(ids: any[], optionsOrConditions?: FindManyOptions|FindConditions): Promise { return this.manager.findByIds(this.metadata.target, ids, optionsOrConditions as any); } /** * Finds first entity that matches given options. */ findOne(id?: string|number|Date|ObjectID, options?: FindOneOptions): Promise; /** * Finds first entity that matches given options. */ findOne(options?: FindOneOptions): Promise; /** * Finds first entity that matches given conditions. */ findOne(conditions?: FindConditions, options?: FindOneOptions): Promise; /** * Finds first entity that matches given conditions. */ findOne(optionsOrConditions?: string|number|Date|ObjectID|FindOneOptions|FindConditions, maybeOptions?: FindOneOptions): Promise { return this.manager.findOne(this.metadata.target, optionsOrConditions as any, maybeOptions); } /** * Finds first entity that matches given options. */ findOneOrFail(id?: string|number|Date|ObjectID, options?: FindOneOptions): Promise; /** * Finds first entity that matches given options. */ findOneOrFail(options?: FindOneOptions): Promise; /** * Finds first entity that matches given conditions. */ findOneOrFail(conditions?: FindConditions, options?: FindOneOptions): Promise; /** * Finds first entity that matches given conditions. */ findOneOrFail(optionsOrConditions?: string|number|Date|ObjectID|FindOneOptions|FindConditions, maybeOptions?: FindOneOptions): Promise { return this.manager.findOneOrFail(this.metadata.target, optionsOrConditions as any, maybeOptions); } /** * Executes a raw SQL query and returns a raw database results. * Raw query execution is supported only by relational databases (MongoDB is not supported). */ query(query: string, parameters?: any[]): Promise { return this.manager.query(query, parameters); } /** * 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 { return this.manager.clear(this.metadata.target); } /** * Increments some column by provided value of the entities matched given conditions. */ increment(conditions: FindConditions, propertyPath: string, value: number): Promise { return this.manager.increment(this.metadata.target, conditions, propertyPath, value); } /** * Decrements some column by provided value of the entities matched given conditions. */ decrement(conditions: FindConditions, propertyPath: string, value: number): Promise { return this.manager.decrement(this.metadata.target, conditions, propertyPath, value); } }