cleanup persist operations executer and builder

This commit is contained in:
Umed Khudoiberdiev 2016-03-13 19:15:45 +05:00
parent 74596edb0d
commit e530d719b0
2 changed files with 83 additions and 98 deletions

View File

@ -134,46 +134,6 @@ export class EntityPersistOperationBuilder {
return insertOperations.reduce((operations, insertOperation) => {
return operations.concat(this.findRelationsWithEntityInside(insertOperation, newEntity));
}, <UpdateByRelationOperation[]> []);
/*const entitiesWithoutIds = allEntities
.filter(entityWithId => !!entityWithId.id)
.map(entityWithId => entityWithId.entity);*/
/*
entitiesWithoutIds.find(entity => entity === insertOperation.entity);
metadata.relations.map(relation => {
});
const oneToOneManyToOneUpdates = Promise.all(meta.relations.map(relation => {
let insertOperationUpdates: Promise<any>, updateOperationUpdates: Promise<any>;
if (insertOperation.entity[relation.propertyName] instanceof Array && relation.isOneToMany) {
insertOperationUpdates = Promise.all(persistOperations.inserts.filter(o => {
return insertOperation.entity[relation.propertyName].indexOf(o.entity) !== -1;
}).map(o => {
const oMetadata = this.connection.getMetadata(o.entity.constructor);
const inverseRelation = relation.inverseRelation;
const query = `UPDATE ${oMetadata.table.name} SET ${inverseRelation.name}='${insertOperation.entityId}' WHERE ${oMetadata.primaryColumn.name}='${o.entityId}'`;
return this.connection.driver.query(query);
}));
updateOperationUpdates = Promise.all(persistOperations.updates.filter(o => {
return insertOperation.entity[relation.propertyName].indexOf(o.entity) !== -1;
}).map(o => {
const oMetadata = this.connection.getMetadata(o.entity.constructor);
const inverseRelation = relation.inverseRelation;
const id = insertOperation.entity[meta.primaryColumn.name];
const query = `UPDATE ${oMetadata.table.name} SET ${inverseRelation.name}='${insertOperation.entityId}' WHERE ${oMetadata.primaryColumn.name}='${id}'`;
return this.connection.driver.query(query);
}));
}
//return Promise.all([insertOperationUpdates, updateOperationUpdates]);
}));
});*/
}
private findJunctionInsertOperations(metadata: EntityMetadata, newEntity: any, dbEntities: EntityWithId[]): JunctionInsertOperation[] {

View File

@ -22,60 +22,95 @@ export class PersistOperationExecutor {
executePersistOperation(persistOperation: PersistOperation) {
return Promise.resolve()
.then(() => { // insert new relations
return Promise.all(persistOperation.inserts.map(operation => {
return this.insert(operation.entity).then((result: any) => {
operation.entityId = result.insertId;
});
}));
}).then(() => { // insert junction table insertions
return Promise.all(persistOperation.junctionInserts.map(junctionOperation => {
return this.insertJunctions(junctionOperation, persistOperation.inserts);
}));
}).then(() => { // remove junction table insertions
return Promise.all(persistOperation.junctionRemoves.map(junctionOperation => {
return this.removeJunctions(junctionOperation);
}));
}).then(() => {
return Promise.all(persistOperation.updatesByRelations.map(updateByRelation => {
this.updateByRelation(updateByRelation, persistOperation.inserts);
}));
}).then(() => { // perform updates
return Promise.all(persistOperation.updates.map(updateOperation => {
return this.update(updateOperation);
}));
}).then(() => { // remove removed relations
return Promise.all(persistOperation.removes.map(operation => {
return this.updateDeletedRelations(operation);
}));
}).then(() => { // remove removed entities
return Promise.all(persistOperation.removes.map(operation => {
return this.delete(operation.entity);
}));
}).then(() => { // update ids
persistOperation.inserts.forEach(insertOperation => {
const metadata = this.connection.getMetadata(insertOperation.entity.constructor);
insertOperation.entity[metadata.primaryColumn.name] = insertOperation.entityId;
});
});
.then(() => this.executeInsertOperations(persistOperation))
.then(() => this.executeInsertJunctionsOperations(persistOperation))
.then(() => this.executeRemoveJunctionsOperations(persistOperation))
.then(() => this.executeUpdateRelationsOperations(persistOperation))
.then(() => this.executeUpdateOperations(persistOperation))
.then(() => this.executeRemoveRelationOperations(persistOperation))
.then(() => this.executeRemoveOperations(persistOperation))
.then(() => this.executeUpdateByIdOperations(persistOperation));
}
// -------------------------------------------------------------------------
// Private Methods
// -------------------------------------------------------------------------
/**
* Executes insert operations.
*/
private executeInsertOperations(persistOperation: PersistOperation) {
return Promise.all(persistOperation.inserts.map(operation => {
return this.insert(operation.entity).then((result: any) => {
operation.entityId = result.insertId;
});
}));
}
/**
* Executes insert junction operations.
*/
private executeInsertJunctionsOperations(persistOperation: PersistOperation) {
return Promise.all(persistOperation.junctionInserts.map(junctionOperation => {
return this.insertJunctions(junctionOperation, persistOperation.inserts);
}));
}
/**
* Executes remove junction operations.
*/
private executeRemoveJunctionsOperations(persistOperation: PersistOperation) {
return Promise.all(persistOperation.junctionRemoves.map(junctionOperation => {
return this.removeJunctions(junctionOperation);
}));
}
/**
* Executes update relations operations.
*/
private executeUpdateRelationsOperations(persistOperation: PersistOperation) {
return Promise.all(persistOperation.updatesByRelations.map(updateByRelation => {
this.updateByRelation(updateByRelation, persistOperation.inserts);
}));
}
/**
* Executes update operations.
*/
private executeUpdateOperations(persistOperation: PersistOperation) {
return Promise.all(persistOperation.updates.map(updateOperation => {
return this.update(updateOperation);
}));
}
/**
* Executes remove relations operations.
*/
private executeRemoveRelationOperations(persistOperation: PersistOperation) {
return Promise.all(persistOperation.removes.map(operation => {
return this.updateDeletedRelations(operation);
}));
}
/**
* Executes remove operations.
*/
private executeRemoveOperations(persistOperation: PersistOperation) {
return Promise.all(persistOperation.removes.map(operation => {
return this.delete(operation.entity);
}));
}
/**
* Executes update by id operations.
*/
private executeUpdateByIdOperations(persistOperation: PersistOperation) {
persistOperation.inserts.forEach(insertOperation => {
const metadata = this.connection.getMetadata(insertOperation.entity.constructor);
insertOperation.entity[metadata.primaryColumn.name] = insertOperation.entityId;
});
}
private updateByRelation(operation: UpdateByRelationOperation, insertOperations: InsertOperation[]) {
let tableName: string, relationName: string, relationId: any, idColumn: string, id: any;
const idInInserts = insertOperations.find(o => o.entity === operation.targetEntity).entityId;
@ -129,20 +164,10 @@ export class PersistOperationExecutor {
.filter(column => !column.isVirtual)
.filter(column => entity.hasOwnProperty(column.propertyName))
.map(column => column.name);
/*const virtualColumns = metadata.columns
.filter(column => column.isVirtual)
.filter(column => entity.hasOwnProperty(column.propertyName))
.map(column => column.name);*/
const values = metadata.columns
.filter(column => !column.isVirtual)
.filter(column => entity.hasOwnProperty(column.propertyName))
.map(column => "'" + entity[column.propertyName] + "'");
/*const virtualValues = metadata.columns
.filter(column => column.isVirtual)
.filter(column => entity.hasOwnProperty(column.propertyName))
.map(column => "'" + entity[column.propertyName] + "'");
const allColumns = columns.concat(virtualColumns);
const allVolumes = values.concat(virtualValues);*/
const relationColumns = metadata.relations
.filter(relation => relation.isOwning && !!relation.relatedEntityMetadata)
.filter(relation => entity.hasOwnProperty(relation.propertyName))