small fixes

This commit is contained in:
Umed Khudoiberdiev 2016-09-26 20:29:35 +05:00
parent 32f7fc021a
commit a89f4a4b11
4 changed files with 78 additions and 23 deletions

View File

@ -8,21 +8,21 @@ import {UpdateEvent} from "../../../src/subscriber/event/UpdateEvent";
export class EverythingSubscriber implements EntitySubscriberInterface<any> {
/**
* Called after entity insertion.
* Called before entity insertion.
*/
beforeInsert(event: InsertEvent<any>) {
console.log(`BEFORE ENTITY INSERTED: `, event.entity);
}
/**
* Called after entity insertion.
* Called before entity insertion.
*/
beforeUpdate(event: UpdateEvent<any>) {
console.log(`BEFORE ENTITY UPDATED: `, event.entity);
}
/**
* Called after entity insertion.
* Called before entity insertion.
*/
beforeRemove(event: RemoveEvent<any>) {
console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);

View File

@ -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<Entity>(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<Entity>(entityClass: ObjectType<Entity>): SpecificRepository<Entity>;
/**
* 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<Entity>(entityName: string): SpecificRepository<Entity>;
/**
* 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<Entity>(entityClassOrName: ObjectType<Entity>|string): SpecificRepository<Entity> {
// 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<Entity>(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<void> {
if (!this.queryRunnerProvider)

View File

@ -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<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Promise<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>, conditions: ObjectLiteral): Promise<Entity[]>;
/**
* Finds entities that match given conditions.
@ -142,12 +143,12 @@ export class EntityManager extends BaseEntityManager {
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Promise<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>, conditions: ObjectLiteral, options: FindOptions): Promise<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity[]> {
find<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<Entity[]> {
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<Entity>(entityClass: ObjectType<Entity>): 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<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Promise<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>, 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<Entity>(entityClass: ObjectType<Entity>, 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<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Promise<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>, 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<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<[Entity[], number]> {
findAndCount<Entity>(entityClass: ObjectType<Entity>, 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<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Promise<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>, conditions: ObjectLiteral): Promise<Entity>;
/**
* Finds first entity that matches given conditions.
@ -212,12 +223,12 @@ export class EntityManager extends BaseEntityManager {
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Promise<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>, conditions: ObjectLiteral, options: FindOptions): Promise<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity> {
findOne<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<Entity> {
if (conditionsOrFindOptions && options) {
return this.getRepository(entityClass).findOne(conditionsOrFindOptions, options);

View File

@ -29,7 +29,8 @@ export class Repository<Entity extends ObjectLiteral> {
/**
* 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<Entity extends ObjectLiteral> {
/**
* Finds entities that match given conditions.
*/
async find(conditions: Object): Promise<Entity[]>;
async find(conditions: ObjectLiteral): Promise<Entity[]>;
/**
* Finds entities with given find options.
@ -212,40 +213,50 @@ export class Repository<Entity extends ObjectLiteral> {
/**
* Finds entities that match given conditions and find options.
*/
async find(conditions: Object, options: FindOptions): Promise<Entity[]>;
async find(conditions: ObjectLiteral, options: FindOptions): Promise<Entity[]>;
/**
* Finds entities that match given conditions and/or find options.
*/
async find(conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity[]> {
async find(conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<Entity[]> {
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<Entity extends ObjectLiteral> {
/**
* Finds first entity that matches given conditions.
*/
async findOne(conditions: Object): Promise<Entity>;
async findOne(conditions: ObjectLiteral): Promise<Entity>;
/**
* Finds first entity that matches given find options.
@ -268,18 +279,19 @@ export class Repository<Entity extends ObjectLiteral> {
/**
* Finds first entity that matches given conditions and find options.
*/
async findOne(conditions: Object, options: FindOptions): Promise<Entity>;
async findOne(conditions: ObjectLiteral, options: FindOptions): Promise<Entity>;
/**
* Finds first entity that matches given conditions and/or find options.
*/
async findOne(conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity> {
async findOne(conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<Entity> {
return this.createFindQueryBuilder(conditionsOrFindOptions, options)
.getSingleResult();
}
/**
* Finds entity with given id.
* Optionally find options can be applied.
*/
async findOneById(id: any, options?: FindOptions): Promise<Entity> {
const conditions: ObjectLiteral = {};