Skip to content

Commit

Permalink
Add option for using different promise implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
martinj committed Dec 1, 2016
1 parent e4a71d8 commit b98ec1e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ Using [mysql2](https://github.com/sidorares/node-mysql2)

db.configure(opts, require('mysql2'));


Use different promise implementation

var db = require('mysql-promise')();

db.configure(dbOpts, null, PromiseImpl);
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Promise = require('bluebird');
var BlueBird = require('bluebird');
var instances = {};
var defaultMysqlDriver;

Expand All @@ -9,21 +9,26 @@ var defaultMysqlDriver;
*/
function DB() {
this.pool = null;
this.PromiseImpl = BlueBird;
}

/**
* Setup the Database connection pool for this instance
* @param {Object} config
* @param {Object} [mysql] mysql driver
* @param {Object} [PromiseImpl] PromiseImpl promise implementation to use
*/
DB.prototype.configure = function (config, mysql) {
DB.prototype.configure = function (config, mysql, PromiseImpl) {
if (!mysql) {
if (!defaultMysqlDriver) {
defaultMysqlDriver = require('mysql');
}
mysql = defaultMysqlDriver;
}

if (PromiseImpl) {
this.PromiseImpl = PromiseImpl;
}
this.pool = mysql.createPool(config);
};

Expand All @@ -42,7 +47,7 @@ DB.prototype.isConfigured = function () {
DB.prototype.getConnection = function () {
var self = this;

return new Promise(function (resolve, reject) {
return new self.PromiseImpl(function (resolve, reject) {
self.pool.getConnection(function (err, con) {
if (err) {
if (con) {
Expand All @@ -63,12 +68,13 @@ DB.prototype.getConnection = function () {
* @return {Promise}
*/
DB.prototype.query = function (query, params) {
var self = this;
params = params || {};

return this
.getConnection()
.then(function (con) {
return new Promise(function (resolve, reject) {
return new self.PromiseImpl(function (resolve, reject) {
con.query(query, params, function (err) {
if (err) {
if (con) {
Expand All @@ -91,7 +97,7 @@ DB.prototype.query = function (query, params) {
DB.prototype.end = function () {
var self = this;

return new Promise(function (resolve, reject) {
return new self.PromiseImpl(function (resolve, reject) {
self.pool.end(function (err) {
if (err) {
return reject(err);
Expand Down
24 changes: 24 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ describe('mysql-promise', function () {

});

describe('Configureable Promise Implementation', function () {

it('should use bundled bluebird promise by default', function () {
var db = mysql('promise-by-default');
db.configure(dbConfig);

db.PromiseImpl.should.equal(require('bluebird'));
var promise = db.getConnection();
promise.spread.should.be.a.Function;
});

it('should use configured promise implementation', function () {
var db = mysql('native-promise');
db.configure(dbConfig, null, Promise); // eslint-disable-line

db.PromiseImpl.should.equal(Promise); // eslint-disable-line

var promise = db.getConnection();
should.not.exist(promise.spread);
promise.then.should.be.a.Function;
});

});

describe('getConnection()', function () {

it('should return a connection', function (done) {
Expand Down

0 comments on commit b98ec1e

Please sign in to comment.