Skip to content

Commit

Permalink
feat: add beginTransaction API on datasource
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Biniam Admikew and bajtos committed Jun 28, 2019
1 parent 39555a0 commit 1ed385e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/datasource.js
Expand Up @@ -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
*/
/**
Expand Down
7 changes: 7 additions & 0 deletions test/transaction.test.js
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions types/datasource.d.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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>;
}

0 comments on commit 1ed385e

Please sign in to comment.