mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added entity manager
This commit is contained in:
parent
72ada22675
commit
074852ea1e
@ -8,6 +8,7 @@ import {SchemaCreator} from "../schema-creator/SchemaCreator";
|
||||
import {MetadataNotFoundError} from "./error/MetadataNotFoundError";
|
||||
import {ConstructorFunction} from "../common/ConstructorFunction";
|
||||
import {EntityListenerMetadata} from "../metadata-builder/metadata/EntityListenerMetadata";
|
||||
import {EntityManager} from "../repository/EntityManager";
|
||||
|
||||
interface RepositoryAndMetadata {
|
||||
repository: Repository<any>;
|
||||
@ -30,6 +31,7 @@ export class Connection {
|
||||
private _subscribers: OrmSubscriber<any>[] = [];
|
||||
private repositoryAndMetadatas: RepositoryAndMetadata[] = [];
|
||||
private _options: ConnectionOptions;
|
||||
private entityManager: EntityManager;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructor
|
||||
@ -40,6 +42,7 @@ export class Connection {
|
||||
this._driver = driver;
|
||||
this._driver.connection = this;
|
||||
this._options = options;
|
||||
this.entityManager = new EntityManager(this);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@ -139,6 +142,10 @@ export class Connection {
|
||||
this._subscribers = this._subscribers.concat(subscribers);
|
||||
}
|
||||
|
||||
getEntityManager() {
|
||||
return this.entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets repository for the given entity class.
|
||||
*/
|
||||
|
||||
201
src/repository/EntityManager.ts
Normal file
201
src/repository/EntityManager.ts
Normal file
@ -0,0 +1,201 @@
|
||||
import {Connection} from "../connection/Connection";
|
||||
import {QueryBuilder} from "../query-builder/QueryBuilder";
|
||||
import {FindOptions} from "./FindOptions";
|
||||
import {Repository} from "./Repository";
|
||||
import {ConstructorFunction} from "../common/ConstructorFunction";
|
||||
|
||||
/**
|
||||
* Entity manager supposed to work with any entity, automatically find its repository and call its method, whatever
|
||||
* entity type are you passing.
|
||||
*/
|
||||
export class EntityManager {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructor
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(private connection: Connection) {
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Public Methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Gets repository of the given entity.
|
||||
*/
|
||||
getRepository<Entity>(entityClass: ConstructorFunction<Entity>|Function): Repository<Entity> {
|
||||
return this.connection.getRepository(entityClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if entity has an id.
|
||||
*/
|
||||
hasId(entity: Function): boolean {
|
||||
return this.getRepository(entity).hasId(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new query builder that can be used to build an sql query.
|
||||
*/
|
||||
createQueryBuilder<Entity>(entityClass: ConstructorFunction<Entity>|Function, alias: string): QueryBuilder<Entity> {
|
||||
return this.getRepository(entityClass).createQueryBuilder(alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new entity. If fromRawEntity is given then it creates a new entity and copies all entity properties
|
||||
* from this object into a new entity (copies only properties that should be in a new entity).
|
||||
*/
|
||||
create<Entity>(entityClass: ConstructorFunction<Entity>|Function, fromRawEntity?: Object): Entity {
|
||||
return this.getRepository(entityClass).create(fromRawEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a entities from the given array of plain javascript objects.
|
||||
*/
|
||||
createMany<Entity>(entityClass: ConstructorFunction<Entity>|Function, copyFromObjects: any[]): Entity[] {
|
||||
return this.getRepository(entityClass).createMany(copyFromObjects);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
initialize<Entity>(entityClass: ConstructorFunction<Entity>|Function, object: Object): Promise<Entity> {
|
||||
return this.getRepository(entityClass).initialize(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges two entities into one new entity.
|
||||
*/
|
||||
merge<Entity>(entity1: Entity, entity2: Entity): Entity {
|
||||
return <Entity> this.getRepository(<any> entity1).merge(entity1, entity2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists (saves) a given entity in the database.
|
||||
*/
|
||||
persist<Entity>(entity: Entity): Promise<Entity> {
|
||||
return this.getRepository(<any> entity).persist(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a given entity from the database.
|
||||
*/
|
||||
remove<Entity>(entity: Entity) {
|
||||
return this.getRepository(<any> entity).remove(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
find<Entity>(entityClass: ConstructorFunction<Entity>|Function): Promise<Entity[]>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
find<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditions: Object): Promise<Entity[]>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
find<Entity>(entityClass: ConstructorFunction<Entity>|Function, options: FindOptions): Promise<Entity[]>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
find<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditions: Object, options: FindOptions): Promise<Entity[]>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
find<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity[]> {
|
||||
return this.getRepository(entityClass).find(conditionsOrFindOptions, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>|Function): Promise<{ items: Entity[], count: number }>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditions: Object): Promise<{ items: Entity[], count: number }>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>|Function, options: FindOptions): Promise<{ items: Entity[], count: number }>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditions: Object, options: FindOptions): Promise<{ items: Entity[], count: number }>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<{ items: Entity[], count: number }> {
|
||||
return this.getRepository(entityClass).findAndCount(conditionsOrFindOptions, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds first entity that matches given conditions.
|
||||
*/
|
||||
findOne<Entity>(entityClass: ConstructorFunction<Entity>|Function): Promise<Entity>;
|
||||
|
||||
/**
|
||||
* Finds first entity that matches given conditions.
|
||||
*/
|
||||
findOne<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditions: Object): Promise<Entity>;
|
||||
|
||||
/**
|
||||
* Finds first entity that matches given conditions.
|
||||
*/
|
||||
findOne<Entity>(entityClass: ConstructorFunction<Entity>|Function, options: FindOptions): Promise<Entity>;
|
||||
|
||||
/**
|
||||
* Finds first entity that matches given conditions.
|
||||
*/
|
||||
findOne<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditions: Object, options: FindOptions): Promise<Entity>;
|
||||
|
||||
/**
|
||||
* Finds first entity that matches given conditions.
|
||||
*/
|
||||
findOne<Entity>(entityClass: ConstructorFunction<Entity>|Function, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity> {
|
||||
return this.getRepository(entityClass).findOne(conditionsOrFindOptions, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds entity with given id.
|
||||
*/
|
||||
findById<Entity>(entityClass: ConstructorFunction<Entity>|Function, id: any, options?: FindOptions): Promise<Entity> {
|
||||
return this.getRepository(entityClass).findById(id, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes raw SQL query and returns raw database results.
|
||||
*/
|
||||
query(query: string): Promise<any> {
|
||||
return this.connection.driver.query(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps given function execution (and all operations made there) in a transaction.
|
||||
*/
|
||||
transaction(runInTransaction: () => Promise<any>): Promise<any> {
|
||||
let runInTransactionResult: any;
|
||||
return this.connection.driver
|
||||
.beginTransaction()
|
||||
.then(() => runInTransaction())
|
||||
.then(result => {
|
||||
runInTransactionResult = result;
|
||||
return this.connection.driver.endTransaction();
|
||||
})
|
||||
.then(() => runInTransactionResult);
|
||||
}
|
||||
|
||||
}
|
||||
@ -82,7 +82,7 @@ export class Repository<Entity> {
|
||||
/**
|
||||
* Persists (saves) a given entity in the database.
|
||||
*/
|
||||
persist(entity: Entity) {
|
||||
persist(entity: Entity): Promise<Entity> {
|
||||
let loadedDbEntity: any;
|
||||
const persister = new PersistOperationExecutor(this.connection);
|
||||
const builder = new EntityPersistOperationBuilder(this.connection);
|
||||
@ -102,7 +102,7 @@ export class Repository<Entity> {
|
||||
/**
|
||||
* Removes a given entity from the database.
|
||||
*/
|
||||
remove(entity: Entity) {
|
||||
remove(entity: Entity): Promise<Entity> {
|
||||
const persister = new PersistOperationExecutor(this.connection);
|
||||
return this.initialize(entity).then(dbEntity => {
|
||||
(<any> entity)[this.metadata.primaryColumn.name] = undefined;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user