added new repository methods to entity manager and reactive extensions

This commit is contained in:
Umed Khudoiberdiev 2016-05-26 19:24:43 +05:00
parent 27fe82279c
commit 5daf0d3ef5
6 changed files with 210 additions and 28 deletions

View File

@ -183,7 +183,7 @@ export class MysqlDriver extends BaseDriver implements Driver {
const updateValues = this.escapeObjectMap(valuesMap).join(",");
const conditionString = this.escapeObjectMap(conditions).join(" AND ");
const query = `UPDATE ${tableName} SET ${updateValues} ${conditionString ? (" WHERE " + conditionString) : ""}`;
console.log("executing update: ", query);
// console.log("executing update: ", query);
return this.query(query).then(() => {});
// const qb = this.createQueryBuilder().update(tableName, valuesMap).from(tableName, "t");
// Object.keys(conditions).forEach(key => qb.andWhere(key + "=:" + key, { [key]: (<any> conditions)[key] }));

View File

@ -268,6 +268,66 @@ export class EntityManager {
.then(() => runInTransactionResult);
}
/**
* Sets given relatedEntityId to the value of the relation of the entity with entityId id.
* Should be used when you want quickly and efficiently set a relation (for many-to-one and one-to-many) to some entity.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
setRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string, entityId: any, relatedEntityId: any): Promise<void>;
setRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: ((t: Entity) => string|any), entityId: any, relatedEntityId: any): Promise<void>;
setRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityId: any): Promise<void> {
return this.getRepository(entityClass).setRelation(relationName as any, entityId, relatedEntityId);
}
/**
* Adds a new relation between two entities into relation's many-to-many table.
* Should be used when you want quickly and efficiently add a relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
addToRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string, entityId: any, relatedEntityIds: any[]): Promise<void>;
addToRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: ((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Promise<void>;
addToRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Promise<void> {
return this.getRepository(entityClass).addToRelation(relationName as any, entityId, relatedEntityIds);
}
/**
* Removes a relation between two entities from relation's many-to-many table.
* Should be used when you want quickly and efficiently remove a many-to-many relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string, entityId: any, relatedEntityIds: any[]): Promise<void>;
removeFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: ((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Promise<void>;
removeFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Promise<void> {
return this.getRepository(entityClass).removeFromRelation(relationName as any, entityId, relatedEntityIds);
}
/**
* Performs both #addToRelation and #removeFromRelation operations.
* Should be used when you want quickly and efficiently and and remove a many-to-many relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
async addAndRemoveFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relation: string, entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void>;
async addAndRemoveFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relation: ((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void>;
async addAndRemoveFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relation: string|((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void> {
return this.getRepository(entityClass).addAndRemoveFromRelation(relation as any, entityId, addRelatedEntityIds, removeRelatedEntityIds);
}
/**
* Removes entity with the given id.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeById<Entity>(entityClass: ConstructorFunction<Entity>|Function, id: any) {
return this.getRepository(entityClass).removeById(id);
}
/**
* Removes all entities with the given ids.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeByIds<Entity>(entityClass: ConstructorFunction<Entity>|Function, ids: any[]) {
return this.getRepository(entityClass).removeByIds(ids);
}
/**
* Roots are entities that have no ancestors. Finds them all.
*/

View File

@ -241,10 +241,71 @@ export class ReactiveEntityManager {
.then(() => runInTransactionResult));
}
/**
* Sets given relatedEntityId to the value of the relation of the entity with entityId id.
* Should be used when you want quickly and efficiently set a relation (for many-to-one and one-to-many) to some entity.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
setRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string, entityId: any, relatedEntityId: any): Rx.Observable<void>;
setRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: ((t: Entity) => string|any), entityId: any, relatedEntityId: any): Rx.Observable<void>;
setRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityId: any): Rx.Observable<void> {
return this.getReactiveRepository(entityClass).setRelation(relationName as any, entityId, relatedEntityId);
}
/**
* Adds a new relation between two entities into relation's many-to-many table.
* Should be used when you want quickly and efficiently add a relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
addToRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string, entityId: any, relatedEntityIds: any[]): Rx.Observable<void>;
addToRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: ((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Rx.Observable<void>;
addToRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Rx.Observable<void> {
return this.getReactiveRepository(entityClass).addToRelation(relationName as any, entityId, relatedEntityIds);
}
/**
* Removes a relation between two entities from relation's many-to-many table.
* Should be used when you want quickly and efficiently remove a many-to-many relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string, entityId: any, relatedEntityIds: any[]): Rx.Observable<void>;
removeFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: ((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Rx.Observable<void>;
removeFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Rx.Observable<void> {
return this.getReactiveRepository(entityClass).removeFromRelation(relationName as any, entityId, relatedEntityIds);
}
/**
* Performs both #addToRelation and #removeFromRelation operations.
* Should be used when you want quickly and efficiently and and remove a many-to-many relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
addAndRemoveFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relation: string, entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Rx.Observable<void>;
addAndRemoveFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relation: ((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Rx.Observable<void>;
addAndRemoveFromRelation<Entity>(entityClass: ConstructorFunction<Entity>|Function, relation: string|((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Rx.Observable<void> {
return this.getReactiveRepository(entityClass).addAndRemoveFromRelation(relation as any, entityId, addRelatedEntityIds, removeRelatedEntityIds);
}
/**
* Removes entity with the given id.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeById<Entity>(entityClass: ConstructorFunction<Entity>|Function, id: any) {
return this.getReactiveRepository(entityClass).removeById(id);
}
/**
* Removes all entities with the given ids.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeByIds<Entity>(entityClass: ConstructorFunction<Entity>|Function, ids: any[]) {
return this.getReactiveRepository(entityClass).removeByIds(ids);
}
/**
* Roots are entities that have no ancestors. Finds them all.
*/
findRoots<Entity>(entityClass: ConstructorFunction<Entity>|Function): Promise<Entity[]> {
findRoots<Entity>(entityClass: ConstructorFunction<Entity>|Function): Rx.Observable<Entity[]> {
return this.getReactiveTreeRepository(entityClass).findRoots();
}
@ -258,21 +319,21 @@ export class ReactiveEntityManager {
/**
* Gets all children (descendants) of the given entity. Returns them all in a flat array.
*/
findDescendants<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Promise<Entity[]> {
findDescendants<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Rx.Observable<Entity[]> {
return this.getReactiveTreeRepository(entityClass).findDescendants(entity);
}
/**
* Gets all children (descendants) of the given entity. Returns them in a tree - nested into each other.
*/
findDescendantsTree<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Promise<Entity> {
findDescendantsTree<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Rx.Observable<Entity> {
return this.getReactiveTreeRepository(entityClass).findDescendantsTree(entity);
}
/**
* Gets number of descendants of the entity.
*/
countDescendants<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Promise<number> {
countDescendants<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Rx.Observable<number> {
return this.getReactiveTreeRepository(entityClass).countDescendants(entity);
}
@ -286,21 +347,21 @@ export class ReactiveEntityManager {
/**
* Gets all parents (ancestors) of the given entity. Returns them all in a flat array.
*/
findAncestors<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Promise<Entity[]> {
findAncestors<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Rx.Observable<Entity[]> {
return this.getReactiveTreeRepository(entityClass).findAncestors(entity);
}
/**
* Gets all parents (ancestors) of the given entity. Returns them in a tree - nested into each other.
*/
findAncestorsTree<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Promise<Entity> {
findAncestorsTree<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Rx.Observable<Entity> {
return this.getReactiveTreeRepository(entityClass).findAncestorsTree(entity);
}
/**
* Gets number of ancestors of the entity.
*/
countAncestors<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Promise<number> {
countAncestors<Entity>(entityClass: ConstructorFunction<Entity>|Function, entity: Entity): Rx.Observable<number> {
return this.getReactiveTreeRepository(entityClass).countAncestors(entity);
}

View File

@ -94,7 +94,7 @@ export class ReactiveRepository<Entity> {
find(conditions: Object): Rx.Observable<Entity[]>;
/**
* Finds entities with .
* Finds entities that match given find options.
*/
find(options: FindOptions): Rx.Observable<Entity[]>;
@ -209,6 +209,66 @@ export class ReactiveRepository<Entity> {
return Rx.Observable.fromPromise(this.repository.transaction(runInTransaction));
}
/**
* Sets given relatedEntityId to the value of the relation of the entity with entityId id.
* Should be used when you want quickly and efficiently set a relation (for many-to-one and one-to-many) to some entity.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
setRelation(relationName: string, entityId: any, relatedEntityId: any): Rx.Observable<void>;
setRelation(relationName: ((t: Entity) => string|any), entityId: any, relatedEntityId: any): Rx.Observable<void>;
setRelation(relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityId: any): Rx.Observable<void> {
return Rx.Observable.fromPromise(this.repository.setRelation(relationName as any, entityId, relatedEntityId));
}
/**
* Adds a new relation between two entities into relation's many-to-many table.
* Should be used when you want quickly and efficiently add a relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
addToRelation(relationName: string, entityId: any, relatedEntityIds: any[]): Rx.Observable<void>;
addToRelation(relationName: ((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Rx.Observable<void>;
addToRelation(relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Rx.Observable<void> {
return Rx.Observable.fromPromise(this.repository.addToRelation(relationName as any, entityId, relatedEntityIds));
}
/**
* Removes a relation between two entities from relation's many-to-many table.
* Should be used when you want quickly and efficiently remove a many-to-many relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeFromRelation(relationName: string, entityId: any, relatedEntityIds: any[]): Rx.Observable<void>;
removeFromRelation(relationName: ((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Rx.Observable<void>;
removeFromRelation(relationName: string|((t: Entity) => string|any), entityId: any, relatedEntityIds: any[]): Rx.Observable<void> {
return Rx.Observable.fromPromise(this.repository.removeFromRelation(relationName as any, entityId, relatedEntityIds));
}
/**
* Performs both #addToRelation and #removeFromRelation operations.
* Should be used when you want quickly and efficiently and and remove a many-to-many relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
addAndRemoveFromRelation(relation: string, entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Rx.Observable<void>;
addAndRemoveFromRelation(relation: ((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Rx.Observable<void>;
addAndRemoveFromRelation(relation: string|((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Rx.Observable<void> {
return Rx.Observable.fromPromise(this.repository.addAndRemoveFromRelation(relation as any, entityId, addRelatedEntityIds, removeRelatedEntityIds));
}
/**
* Removes entity with the given id.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeById(id: any) {
return Rx.Observable.fromPromise(this.repository.removeById(id));
}
/**
* Removes all entities with the given ids.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
removeByIds(ids: any[]) {
return Rx.Observable.fromPromise(this.repository.removeByIds(ids));
}
// -------------------------------------------------------------------------
// Static Methods
// -------------------------------------------------------------------------

View File

@ -1,6 +1,7 @@
import {ReactiveRepository} from "./ReactiveRepository";
import {TreeRepository} from "./TreeRepository";
import {QueryBuilder} from "../query-builder/QueryBuilder";
import * as Rx from "rxjs/Rx";
/**
* Tree repository is supposed to work with your entity objects. Find entities, insert, update, delete, etc.
@ -23,8 +24,8 @@ export class ReactiveTreeRepository<Entity> extends ReactiveRepository<Entity> {
/**
* Roots are entities that have no ancestors. Finds them all.
*/
findRoots(): Promise<Entity[]> {
return this.repository.findRoots();
findRoots(): Rx.Observable<Entity[]> {
return Rx.Observable.fromPromise(this.repository.findRoots());
}
/**
@ -37,22 +38,22 @@ export class ReactiveTreeRepository<Entity> extends ReactiveRepository<Entity> {
/**
* Gets all children (descendants) of the given entity. Returns them all in a flat array.
*/
findDescendants(entity: Entity): Promise<Entity[]> {
return this.repository.findDescendants(entity);
findDescendants(entity: Entity): Rx.Observable<Entity[]> {
return Rx.Observable.fromPromise(this.repository.findDescendants(entity));
}
/**
* Gets all children (descendants) of the given entity. Returns them in a tree - nested into each other.
*/
findDescendantsTree(entity: Entity): Promise<Entity> {
return this.repository.findDescendantsTree(entity);
findDescendantsTree(entity: Entity): Rx.Observable<Entity> {
return Rx.Observable.fromPromise(this.repository.findDescendantsTree(entity));
}
/**
* Gets number of descendants of the entity.
*/
countDescendants(entity: Entity): Promise<number> {
return this.repository.countDescendants(entity);
countDescendants(entity: Entity): Rx.Observable<number> {
return Rx.Observable.fromPromise(this.repository.countDescendants(entity));
}
/**
@ -65,28 +66,28 @@ export class ReactiveTreeRepository<Entity> extends ReactiveRepository<Entity> {
/**
* Gets all parents (ancestors) of the given entity. Returns them all in a flat array.
*/
findAncestors(entity: Entity): Promise<Entity[]> {
return this.repository.findAncestors(entity);
findAncestors(entity: Entity): Rx.Observable<Entity[]> {
return Rx.Observable.fromPromise(this.repository.findAncestors(entity));
}
/**
* Gets all parents (ancestors) of the given entity. Returns them in a tree - nested into each other.
*/
findAncestorsTree(entity: Entity): Promise<Entity> {
return this.repository.findAncestorsTree(entity);
findAncestorsTree(entity: Entity): Rx.Observable<Entity> {
return Rx.Observable.fromPromise(this.repository.findAncestorsTree(entity));
}
/**
* Gets number of ancestors of the entity.
*/
countAncestors(entity: Entity): Promise<number> {
return this.repository.countAncestors(entity);
countAncestors(entity: Entity): Rx.Observable<number> {
return Rx.Observable.fromPromise(this.repository.countAncestors(entity));
}
/**
* Moves entity to the children of then given entity.
*
move(entity: Entity, to: Entity): Promise<void> {
move(entity: Entity, to: Entity): Rx.Observable<void> {
return this.repository.move(entity, to);
}
*/

View File

@ -327,7 +327,7 @@ export class Repository<Entity> {
values[relation.junctionEntityMetadata.columns[0].name] = relatedEntityId;
}
return this.driver.insert(relation.junctionEntityMetadata.table.name, values)
return this.driver.insert(relation.junctionEntityMetadata.table.name, values);
});
return Promise.all(insertPromises).then(() => {});
}
@ -370,9 +370,9 @@ export class Repository<Entity> {
* Should be used when you want quickly and efficiently and and remove a many-to-many relation between two entities.
* Note that event listeners and event subscribers won't work (and will not send any events) when using this operation.
*/
async addAndRemoveFromRelation(relation: string, entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void>;
async addAndRemoveFromRelation(relation: ((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void>;
async addAndRemoveFromRelation(relation: string|((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void> {
addAndRemoveFromRelation(relation: string, entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void>;
addAndRemoveFromRelation(relation: ((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void>;
addAndRemoveFromRelation(relation: string|((t: Entity) => string|any), entityId: any, addRelatedEntityIds: any[], removeRelatedEntityIds: any[]): Promise<void> {
return Promise.all([
this.addToRelation(relation as any, entityId, addRelatedEntityIds),
this.removeFromRelation(relation as any, entityId, removeRelatedEntityIds)