added transaction support

This commit is contained in:
Umed Khudoiberdiev 2016-03-21 12:04:12 +05:00
parent 68fe639fa5
commit 60eadca654
5 changed files with 43 additions and 2 deletions

View File

@ -67,5 +67,15 @@ export interface Driver {
* Performs a simple DELETE query by a given conditions in a given table.
*/
delete(tableName: string, conditions: Object): Promise<void>;
/**
* Starts transaction.
*/
beginTransaction(): Promise<void>;
/**
* Ends transaction.
*/
endTransaction(): Promise<void>;
}

View File

@ -182,4 +182,18 @@ export class MysqlDriver extends BaseDriver implements Driver {
return qb.execute().then(() => {});
}
/**
* Starts mysql transaction.
*/
beginTransaction(): Promise<void> {
return this.query("START TRANSACTION").then(() => {});
}
/**
* Ends mysql transaction.
*/
endTransaction(): Promise<void> {
return this.query("COMMIT").then(() => {});
}
}

View File

@ -28,6 +28,7 @@ export class PersistOperationExecutor {
*/
executePersistOperation(persistOperation: PersistOperation) {
return Promise.resolve()
.then(() => this.connection.driver.beginTransaction())
.then(() => this.executeInsertOperations(persistOperation))
.then(() => this.executeInsertJunctionsOperations(persistOperation))
.then(() => this.executeRemoveJunctionsOperations(persistOperation))
@ -35,6 +36,7 @@ export class PersistOperationExecutor {
.then(() => this.executeUpdateOperations(persistOperation))
.then(() => this.executeRemoveRelationOperations(persistOperation))
.then(() => this.executeRemoveOperations(persistOperation))
.then(() => this.connection.driver.endTransaction())
.then(() => this.updateIdsOfInsertedEntities(persistOperation))
.then(() => this.updateIdsOfRemovedEntities(persistOperation));
}

View File

@ -168,4 +168,19 @@ export class Repository<Entity> {
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;
this.connection.driver.endTransaction()
})
.then(() => runInTransactionResult);
}
}

View File

@ -50,10 +50,10 @@ describe("insertion", function() {
// -------------------------------------------------------------------------
// Specifications: persist
// -------------------------------------------------------------------------
describe("basic insert functionality", function() {
let newPost: Post, savedPost: Post;
before(reloadDatabase);
beforeEach(function() {