mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added transaction support
This commit is contained in:
parent
68fe639fa5
commit
60eadca654
@ -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>;
|
||||
|
||||
}
|
||||
@ -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(() => {});
|
||||
}
|
||||
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -50,10 +50,10 @@ describe("insertion", function() {
|
||||
// -------------------------------------------------------------------------
|
||||
// Specifications: persist
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
|
||||
describe("basic insert functionality", function() {
|
||||
let newPost: Post, savedPost: Post;
|
||||
|
||||
|
||||
before(reloadDatabase);
|
||||
|
||||
beforeEach(function() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user