feat: add beginTransaction API on datasource

add beginTransaction method which calls begin
method from the Transaction class which in turn
calls the connector's beginTransaction method if
it supports transactions.

Co-Authored-By: Miroslav Bajtoš <mbajtoss@gmail.com>
This commit is contained in:
Biniam Admikew 2019-06-27 09:10:30 -04:00
parent 39555a010a
commit 1ed385e393
3 changed files with 30 additions and 0 deletions

View File

@ -2648,6 +2648,17 @@ DataSource.prototype.execute = function(command, args = [], options = {}) {
}
};
/**
* Begin a new Transaction.
*
*
* @param [options] Options {isolationLevel: '...', timeout: 1000}
* @returns Promise A promise which resolves to a Transaction object
*/
DataSource.prototype.beginTransaction = function(options) {
return Transaction.begin(this.connector, options);
};
/*! The hidden property call is too expensive so it is not used that much
*/
/**

View File

@ -71,6 +71,13 @@ describe('Transactions on test connector without execute()', () => {
}, done);
});
it('beginTransaction returns a transaction', async () => {
const promise = db.beginTransaction(Transaction.READ_UNCOMMITTED);
promise.should.be.Promise();
const transaction = await promise;
transaction.should.be.instanceof(EventEmitter);
});
it('exposes and caches slave models', done => {
testModelCaching(tx.models, db.models);
done();

12
types/datasource.d.ts vendored
View File

@ -12,6 +12,7 @@ import {
PropertyDefinition,
} from './model';
import {EventEmitter} from 'events';
import {IsolationLevel, Transaction} from './transaction-mixin';
/**
* LoopBack models can manipulate data via the DataSource object.
@ -185,4 +186,15 @@ export declare class DataSource extends EventEmitter {
args?: any[] | object,
options?: Options
): Promise<any>;
/**
* Begin a new transaction.
*
*
* @param [options] Options {isolationLevel: '...', timeout: 1000}
* @returns Promise A promise which resolves to a Transaction object
*/
beginTransaction(
options?: IsolationLevel | Options,
): PromiseOrVoid<Transaction>;
}