This repository has been archived by the owner on Dec 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Delisle
committed
Apr 17, 2017
1 parent
659a448
commit 5c2c72e
Showing
5 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var objectAssign = require('object-assign') | ||
|
||
module.exports = SafeClient | ||
|
||
function SafeClient (client, options) { | ||
if (!(this instanceof SafeClient)) { | ||
return new SafeClient(client) | ||
} | ||
|
||
this.options = objectAssign({}, options) | ||
this.Promise = this.options.Promise || global.Promise | ||
this.log = this.options.log || function () { } | ||
|
||
this._client = client | ||
this._released = false | ||
this._releasedError = 'This connection has already been released!' | ||
} | ||
|
||
SafeClient.prototype.query = function (text, values, cb) { | ||
if (this._released) { | ||
var err = new Error(this._releasedError) | ||
if (cb) { | ||
return cb(err) | ||
} else { | ||
return this.Promise.reject(err) | ||
} | ||
} else { | ||
return this._client.query(text, values, cb) | ||
} | ||
} | ||
|
||
SafeClient.prototype.release = function (err) { | ||
if (this._released) { | ||
throw new Error(this._releasedError) | ||
} else { | ||
this._released = true | ||
return this._client.release(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var expect = require('expect.js') | ||
|
||
var describe = require('mocha').describe | ||
var it = require('mocha').it | ||
var Promise = require('bluebird') | ||
|
||
var Pool = require('../') | ||
|
||
if (typeof global.Promise === 'undefined') { | ||
global.Promise = Promise | ||
} | ||
|
||
describe('safe-client', function () { | ||
var query = 'SELECT $1::text as name' | ||
describe('with callbacks', function () { | ||
it('does not allow queries after release', function (done) { | ||
var pool = new Pool() | ||
pool.connect(function (err, client, release) { | ||
if (err) { | ||
pool.end(function () { | ||
done(err) | ||
}) | ||
} | ||
client.query(query, ['brianc'], function (err, res) { | ||
if (err) { | ||
return done(err) | ||
} | ||
expect(res.rows[0]).to.eql({ name: 'brianc' }) | ||
release() | ||
client.query(query, ['brianc'], function (err, res) { | ||
if (!err) { | ||
return done(new Error('Did not receive error!')) | ||
} | ||
expect(err.message).to.not.be(undefined) | ||
expect(err.message).to.contain(client._releasedError) | ||
pool.end(done) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('with promises', function () { | ||
it('does not allow queries after release', function (done) { | ||
var pool = new Pool() | ||
var clientHandle | ||
pool.connect().then(function (client) { | ||
clientHandle = client // Handle for examination and brevity | ||
return clientHandle.query(query, ['brianc']).then(function () { | ||
return clientHandle.release() | ||
}).then(function () { | ||
return clientHandle.query(query, ['brianc']) | ||
}) | ||
}).then(function () { | ||
return done(new Error('Should have thrown an error!')) | ||
}).catch(function (err) { | ||
expect(err.message).to.not.be(undefined) | ||
expect(err.message).to.contain(clientHandle._releasedError) | ||
return pool.end(done) | ||
}) | ||
}) | ||
}) | ||
}) |