Skip to content

Commit

Permalink
Allow a custom type to be used for Client promises
Browse files Browse the repository at this point in the history
Matches the Pool API.
  • Loading branch information
charmander committed Oct 23, 2018
1 parent ff6fe1e commit d3be101
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
12 changes: 5 additions & 7 deletions lib/client.js
Expand Up @@ -31,6 +31,7 @@ var Client = function (config) {

var c = config || {}

this._Promise = c.Promise || global.Promise
this._types = new TypeOverrides(c.types)
this._ending = false
this._connecting = false
Expand Down Expand Up @@ -232,7 +233,7 @@ Client.prototype.connect = function (callback) {
return
}

return new Promise((resolve, reject) => {
return new this._Promise((resolve, reject) => {
this._connect((error) => {
if (error) {
reject(error)
Expand Down Expand Up @@ -409,12 +410,9 @@ Client.prototype.query = function (config, values, callback) {
} else {
query = new Query(config, values, callback)
if (!query.callback) {
let resolveOut, rejectOut
result = new Promise((resolve, reject) => {
resolveOut = resolve
rejectOut = reject
result = new this._Promise((resolve, reject) => {
query.callback = (err, res) => err ? reject(err) : resolve(res)
})
query.callback = (err, res) => err ? rejectOut(err) : resolveOut(res)
}
}

Expand Down Expand Up @@ -458,7 +456,7 @@ Client.prototype.end = function (cb) {
if (cb) {
this.connection.once('end', cb)
} else {
return new Promise((resolve) => {
return new this._Promise((resolve) => {
this.connection.once('end', resolve)
})
}
Expand Down
12 changes: 5 additions & 7 deletions lib/native/client.js
Expand Up @@ -25,6 +25,7 @@ var Client = module.exports = function (config) {
EventEmitter.call(this)
config = config || {}

this._Promise = config.Promise || global.Promise
this._types = new TypeOverrides(config.types)

this.native = new Native({
Expand Down Expand Up @@ -121,7 +122,7 @@ Client.prototype.connect = function (callback) {
return
}

return new Promise((resolve, reject) => {
return new this._Promise((resolve, reject) => {
this._connect((error) => {
if (error) {
reject(error)
Expand Down Expand Up @@ -156,7 +157,7 @@ Client.prototype.query = function (config, values, callback) {
query = new NativeQuery(config, values, callback)
if (!query.callback) {
let resolveOut, rejectOut
result = new Promise((resolve, reject) => {
result = new this._Promise((resolve, reject) => {
resolveOut = resolve
rejectOut = reject
})
Expand Down Expand Up @@ -196,11 +197,8 @@ Client.prototype.end = function (cb) {
}
var result
if (!cb) {
var resolve, reject
cb = (err) => err ? reject(err) : resolve()
result = new global.Promise(function (res, rej) {
resolve = res
reject = rej
result = new this._Promise(function (resolve, reject) {
cb = (err) => err ? reject(err) : resolve()
})
}
this.native.end(function () {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
},
"devDependencies": {
"async": "0.9.0",
"bluebird": "3.5.2",
"co": "4.6.0",
"eslint": "4.2.0",
"eslint-config-standard": "10.2.1",
Expand Down
24 changes: 23 additions & 1 deletion test/integration/client/query-as-promise-tests.js
@@ -1,4 +1,5 @@
'use strict'
var bluebird = require('bluebird')
var helper = require(__dirname + '/../test-helper')
var pg = helper.pg

Expand All @@ -7,10 +8,10 @@ process.on('unhandledRejection', function (e) {
process.exit(1)
})

const pool = new pg.Pool()
const suite = new helper.Suite()

suite.test('promise API', (cb) => {
const pool = new pg.Pool()
pool.connect().then((client) => {
client.query('SELECT $1::text as name', ['foo'])
.then(function (result) {
Expand All @@ -31,3 +32,24 @@ suite.test('promise API', (cb) => {
})
})
})

suite.test('promise API with configurable promise type', (cb) => {
const client = new pg.Client({ Promise: bluebird })
const connectPromise = client.connect()
assert(connectPromise instanceof bluebird, 'Client connect() returns configured promise')

connectPromise
.then(() => {
const queryPromise = client.query('SELECT 1')
assert(queryPromise instanceof bluebird, 'Client query() returns configured promise')

return queryPromise.then(() => {
client.end(cb)
})
})
.catch((error) => {
process.nextTick(() => {
throw error
})
})
});

0 comments on commit d3be101

Please sign in to comment.