Merge pull request #147 from strongloop/chore/promisify-transaction-methods
chore: add promise support
This commit is contained in:
commit
b8aeb23378
|
@ -9,6 +9,7 @@ var util = require('util');
|
||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
var debug = require('debug')('loopback:connector:transaction');
|
var debug = require('debug')('loopback:connector:transaction');
|
||||||
var uuid = require('uuid');
|
var uuid = require('uuid');
|
||||||
|
const {createPromiseCallback} = require('./utils');
|
||||||
|
|
||||||
module.exports = Transaction;
|
module.exports = Transaction;
|
||||||
|
|
||||||
|
@ -46,7 +47,13 @@ Transaction.hookTypes = {
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
Transaction.prototype.commit = function(cb) {
|
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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,7 +62,13 @@ Transaction.prototype.commit = function(cb) {
|
||||||
* @returns {*|boolean}
|
* @returns {*|boolean}
|
||||||
*/
|
*/
|
||||||
Transaction.prototype.rollback = function(cb) {
|
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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,6 +82,7 @@ Transaction.begin = function(connector, options, cb) {
|
||||||
cb = options;
|
cb = options;
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
cb = cb || createPromiseCallback();
|
||||||
if (typeof options === 'string') {
|
if (typeof options === 'string') {
|
||||||
options = {isolationLevel: options};
|
options = {isolationLevel: options};
|
||||||
}
|
}
|
||||||
|
@ -118,4 +132,5 @@ Transaction.begin = function(connector, options, cb) {
|
||||||
}
|
}
|
||||||
cb(err, tx);
|
cb(err, tx);
|
||||||
});
|
});
|
||||||
|
if (cb.promise) return cb.promise;
|
||||||
};
|
};
|
||||||
|
|
|
@ -31,7 +31,8 @@
|
||||||
"uuid": "^3.0.1"
|
"uuid": "^3.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"chai": "^4.1.2",
|
"chai": "^4.2.0",
|
||||||
|
"chai-as-promised": "^7.1.1",
|
||||||
"eslint": "^4.19.1",
|
"eslint": "^4.19.1",
|
||||||
"eslint-config-loopback": "^10.0.0",
|
"eslint-config-loopback": "^10.0.0",
|
||||||
"loopback-datasource-juggler": "^3.12.0",
|
"loopback-datasource-juggler": "^3.12.0",
|
||||||
|
|
|
@ -6,7 +6,10 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
var Transaction = require('../index').Transaction;
|
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 testConnector = require('./connectors/test-sql-connector');
|
||||||
|
|
||||||
var juggler = require('loopback-datasource-juggler');
|
var juggler = require('loopback-datasource-juggler');
|
||||||
|
@ -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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue