From d79092aadbb83647fcdb59a52e78f7083177cf4c Mon Sep 17 00:00:00 2001 From: Biniam Admikew Date: Wed, 26 Jun 2019 07:39:15 -0400 Subject: [PATCH] chore: add promise support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add promise support to transaction methods. Co-Authored-By: Miroslav Bajtoš --- lib/transaction.js | 19 +++++++++++++++++-- package.json | 3 ++- test/transaction.test.js | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/transaction.js b/lib/transaction.js index d32ffc2..93b2c2b 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -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; @@ -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); + } }; /** @@ -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); + } }; /** @@ -69,6 +82,7 @@ Transaction.begin = function(connector, options, cb) { cb = options; options = {}; } + cb = cb || createPromiseCallback(); if (typeof options === 'string') { options = {isolationLevel: options}; } @@ -118,4 +132,5 @@ Transaction.begin = function(connector, options, cb) { } cb(err, tx); }); + if (cb.promise) return cb.promise; }; diff --git a/package.json b/package.json index 1c5b8a5..d3653d5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/transaction.test.js b/test/transaction.test.js index e9690a3..66d3009 100644 --- a/test/transaction.test.js +++ b/test/transaction.test.js @@ -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'); @@ -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); + }); });