diff --git a/sample/sample5-subscribers/subscriber/EverythingSubscriber.ts b/sample/sample5-subscribers/subscriber/EverythingSubscriber.ts index 8bd1ca3a2..33748ec11 100644 --- a/sample/sample5-subscribers/subscriber/EverythingSubscriber.ts +++ b/sample/sample5-subscribers/subscriber/EverythingSubscriber.ts @@ -8,21 +8,21 @@ import {UpdateEvent} from "../../../src/subscriber/event/UpdateEvent"; export class EverythingSubscriber implements EntitySubscriberInterface { /** - * Called after entity insertion. + * Called before entity insertion. */ beforeInsert(event: InsertEvent) { console.log(`BEFORE ENTITY INSERTED: `, event.entity); } /** - * Called after entity insertion. + * Called before entity insertion. */ beforeUpdate(event: UpdateEvent) { console.log(`BEFORE ENTITY UPDATED: `, event.entity); } /** - * Called after entity insertion. + * Called before entity insertion. */ beforeRemove(event: RemoveEvent) { console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity); diff --git a/src/entity-manager/BaseEntityManager.ts b/src/entity-manager/BaseEntityManager.ts index 689164e99..8c9f65d77 100644 --- a/src/entity-manager/BaseEntityManager.ts +++ b/src/entity-manager/BaseEntityManager.ts @@ -9,6 +9,7 @@ import {RepositoryAggregator} from "../repository/RepositoryAggregator"; import {RepositoryNotTreeError} from "../connection/error/RepositoryNotTreeError"; import {NoNeedToReleaseEntityManagerError} from "./error/NoNeedToReleaseEntityManagerError"; import {QueryRunnerProviderAlreadyReleasedError} from "../query-runner/error/QueryRunnerProviderAlreadyReleasedError"; +import {SpecificRepository} from "../repository/SpecificRepository"; /** * Common functions shared between different entity manager types. @@ -108,6 +109,37 @@ export abstract class BaseEntityManager { return this.connection.getTreeRepository(entityClassOrName as any); } + /** + * Gets specific repository for the given entity class. + * If single database connection mode is used, then repository is obtained from the + * repository aggregator, where each repository is individually created for this entity manager. + * When single database connection is not used, repository is being obtained from the connection. + */ + getSpecificRepository(entityClass: ObjectType): SpecificRepository; + + /** + * Gets specific repository for the given entity name. + * If single database connection mode is used, then repository is obtained from the + * repository aggregator, where each repository is individually created for this entity manager. + * When single database connection is not used, repository is being obtained from the connection. + */ + getSpecificRepository(entityName: string): SpecificRepository; + + /** + * Gets specific repository for the given entity class or name. + * If single database connection mode is used, then repository is obtained from the + * repository aggregator, where each repository is individually created for this entity manager. + * When single database connection is not used, repository is being obtained from the connection. + */ + getSpecificRepository(entityClassOrName: ObjectType|string): SpecificRepository { + + // if single db connection is used then create its own repository with reused query runner + if (this.queryRunnerProvider) + return this.obtainRepositoryAggregator(entityClassOrName).specificRepository; + + return this.connection.getSpecificRepository(entityClassOrName as any); + } + /** * Checks if entity has an id. */ @@ -187,7 +219,7 @@ export abstract class BaseEntityManager { /** * Releases all resources used by entity manager. * This is used when entity manager is created with a single query runner, - * and this single query runner needs to be released after job with repository is done. + * and this single query runner needs to be released after job with entity manager is done. */ async release(): Promise { if (!this.queryRunnerProvider) diff --git a/src/entity-manager/EntityManager.ts b/src/entity-manager/EntityManager.ts index 07f8c7c61..77e02f2f3 100644 --- a/src/entity-manager/EntityManager.ts +++ b/src/entity-manager/EntityManager.ts @@ -4,6 +4,7 @@ import {ObjectType} from "../common/ObjectType"; import {BaseEntityManager} from "./BaseEntityManager"; import {QueryRunnerProviderAlreadyReleasedError} from "../query-runner/error/QueryRunnerProviderAlreadyReleasedError"; import {QueryRunnerProvider} from "../query-runner/QueryRunnerProvider"; +import {ObjectLiteral} from "../common/ObjectLiteral"; /** * Entity manager supposed to work with any entity, automatically find its repository and call its methods, @@ -132,7 +133,7 @@ export class EntityManager extends BaseEntityManager { /** * Finds entities that match given conditions. */ - find(entityClass: ObjectType, conditions: Object): Promise; + find(entityClass: ObjectType, conditions: ObjectLiteral): Promise; /** * Finds entities that match given conditions. @@ -142,12 +143,12 @@ export class EntityManager extends BaseEntityManager { /** * Finds entities that match given conditions. */ - find(entityClass: ObjectType, conditions: Object, options: FindOptions): Promise; + find(entityClass: ObjectType, conditions: ObjectLiteral, options: FindOptions): Promise; /** * Finds entities that match given conditions. */ - find(entityClass: ObjectType, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise { + find(entityClass: ObjectType, conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise { if (conditionsOrFindOptions && options) { return this.getRepository(entityClass).find(conditionsOrFindOptions, options); @@ -161,28 +162,38 @@ export class EntityManager extends BaseEntityManager { /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ findAndCount(entityClass: ObjectType): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ - findAndCount(entityClass: ObjectType, conditions: Object): Promise<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType, conditions: ObjectLiteral): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ findAndCount(entityClass: ObjectType, options: FindOptions): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ - findAndCount(entityClass: ObjectType, conditions: Object, options: FindOptions): Promise<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType, conditions: ObjectLiteral, options: FindOptions): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ - findAndCount(entityClass: ObjectType, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<[Entity[], number]> { + findAndCount(entityClass: ObjectType, conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<[Entity[], number]> { if (conditionsOrFindOptions && options) { return this.getRepository(entityClass).findAndCount(conditionsOrFindOptions, options); @@ -202,7 +213,7 @@ export class EntityManager extends BaseEntityManager { /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ObjectType, conditions: Object): Promise; + findOne(entityClass: ObjectType, conditions: ObjectLiteral): Promise; /** * Finds first entity that matches given conditions. @@ -212,12 +223,12 @@ export class EntityManager extends BaseEntityManager { /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ObjectType, conditions: Object, options: FindOptions): Promise; + findOne(entityClass: ObjectType, conditions: ObjectLiteral, options: FindOptions): Promise; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ObjectType, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise { + findOne(entityClass: ObjectType, conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise { if (conditionsOrFindOptions && options) { return this.getRepository(entityClass).findOne(conditionsOrFindOptions, options); diff --git a/src/repository/Repository.ts b/src/repository/Repository.ts index 887a356a5..bc5fc68b3 100644 --- a/src/repository/Repository.ts +++ b/src/repository/Repository.ts @@ -29,7 +29,8 @@ export class Repository { /** * Returns object that is managed by this repository. - * If this repository manages entity from schema, then it returns a name of that schema instead. + * If this repository manages entity from schema, + * then it returns a name of that schema instead. */ get target(): Function|string { return this.metadata.target; @@ -202,7 +203,7 @@ export class Repository { /** * Finds entities that match given conditions. */ - async find(conditions: Object): Promise; + async find(conditions: ObjectLiteral): Promise; /** * Finds entities with given find options. @@ -212,40 +213,50 @@ export class Repository { /** * Finds entities that match given conditions and find options. */ - async find(conditions: Object, options: FindOptions): Promise; + async find(conditions: ObjectLiteral, options: FindOptions): Promise; /** * Finds entities that match given conditions and/or find options. */ - async find(conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise { + async find(conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise { return this.createFindQueryBuilder(conditionsOrFindOptions, options) .getResults(); } /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ async findAndCount(): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ - async findAndCount(conditions: Object): Promise<[ Entity[], number ]>; + async findAndCount(conditions: ObjectLiteral): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ async findAndCount(options: FindOptions): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ - async findAndCount(conditions: Object, options: FindOptions): Promise<[ Entity[], number ]>; + async findAndCount(conditions: ObjectLiteral, options: FindOptions): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. + * Also counts all entities that match given conditions, + * but ignores pagination settings (maxResults, firstResult) options. */ - async findAndCount(conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<[ Entity[], number ]> { + async findAndCount(conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<[ Entity[], number ]> { return this.createFindQueryBuilder(conditionsOrFindOptions, options) .getResultsAndCount(); } @@ -258,7 +269,7 @@ export class Repository { /** * Finds first entity that matches given conditions. */ - async findOne(conditions: Object): Promise; + async findOne(conditions: ObjectLiteral): Promise; /** * Finds first entity that matches given find options. @@ -268,18 +279,19 @@ export class Repository { /** * Finds first entity that matches given conditions and find options. */ - async findOne(conditions: Object, options: FindOptions): Promise; + async findOne(conditions: ObjectLiteral, options: FindOptions): Promise; /** * Finds first entity that matches given conditions and/or find options. */ - async findOne(conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise { + async findOne(conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise { return this.createFindQueryBuilder(conditionsOrFindOptions, options) .getSingleResult(); } /** * Finds entity with given id. + * Optionally find options can be applied. */ async findOneById(id: any, options?: FindOptions): Promise { const conditions: ObjectLiteral = {};