Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var util = require('util');
var EventEmitter = require('events').EventEmitter;
var debug = require('debug')('loopback:connector:transaction');
var uuid = require('uuid');
const {createPromiseCallback} = require('./utils');

module.exports = Transaction;

Expand Down Expand Up @@ -46,7 +47,13 @@ Transaction.hookTypes = {
* @returns {*}
*/
Transaction.prototype.commit = function(cb) {
return this.connector.commit(this.connection, cb);
cb = cb || createPromiseCallback();
if (cb.promise) {
this.connector.commit(this.connection, cb);
return cb.promise;
} else {
return this.connector.commit(this.connection, cb);
}
};

/**
Expand All @@ -55,7 +62,13 @@ Transaction.prototype.commit = function(cb) {
* @returns {*|boolean}
*/
Transaction.prototype.rollback = function(cb) {
return this.connector.rollback(this.connection, cb);
cb = cb || createPromiseCallback();
if (cb.promise) {
this.connector.rollback(this.connection, cb);
return cb.promise;
} else {
return this.connector.rollback(this.connection, cb);
}
};

/**
Expand All @@ -69,6 +82,7 @@ Transaction.begin = function(connector, options, cb) {
cb = options;
options = {};
}
cb = cb || createPromiseCallback();
if (typeof options === 'string') {
options = {isolationLevel: options};
}
Expand Down Expand Up @@ -118,4 +132,5 @@ Transaction.begin = function(connector, options, cb) {
}
cb(err, tx);
});
if (cb.promise) return cb.promise;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"uuid": "^3.0.1"
},
"devDependencies": {
"chai": "^4.1.2",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^4.19.1",
"eslint-config-loopback": "^10.0.0",
"loopback-datasource-juggler": "^3.12.0",
Expand Down
33 changes: 32 additions & 1 deletion test/transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
'use strict';
var Transaction = require('../index').Transaction;

var expect = require('chai').expect;
const chai = require('chai');
chai.use(require('chai-as-promised'));
const {expect} = chai;
const chaiAsPromised = require('chai-as-promised');
var testConnector = require('./connectors/test-sql-connector');

var juggler = require('loopback-datasource-juggler');
Expand Down Expand Up @@ -247,4 +250,32 @@ describe('transactions', function() {
});
});
});

it('can return promise for commit', function() {
const connectorObject = {};
connectorObject.commit = function(connection, cb) {
return cb(null, 'committed');
};
const transactionInstance = new Transaction(connectorObject, {});
return expect(transactionInstance.commit()).to.eventually.equal('committed');
});

it('can return promise for rollback', function() {
const connectorObject = {};
connectorObject.rollback = function(connection, cb) {
return cb(null, 'rolledback');
};
const transactionInstance = new Transaction(connectorObject, {});
return expect(transactionInstance.rollback()).to.eventually.equal('rolledback');
});

it('can return promise for begin', function() {
const connectorObject = {};
connectorObject.beginTransaction = function(connection, cb) {
return cb(null, 'begun');
};

return expect(Transaction.begin(connectorObject, '')
).to.eventually.be.instanceOf(Transaction);
});
});