Add connectionSetup as configuration option to prepare a client#43
Add connectionSetup as configuration option to prepare a client#43bramkoot wants to merge 4 commits intobrianc:masterfrom
Conversation
|
Yeah i really dig this big time. |
|
Sorry I just saw it! :p been buuuusy! (too busy!) If you write some tests for this I can merge it no problemo. |
index.js
Outdated
| this.emit('connect', client) | ||
| cb(null, client) | ||
|
|
||
| if (this.options.connectionSetup && typeof this.options.connectionSetup === 'function') { |
There was a problem hiding this comment.
Functions can’t be falsy, so this just needs the typeof check.
There was a problem hiding this comment.
… although it probably shouldn’t fail silently, so maybe just if (this.options.connectionSetup !== undefined).
|
I've added some simple tests which works fine, but Travis fails for older node versions with some odd errors. Any idea's what that could be? node 0.10 / 0.12: |
|
The linter isn’t compatible with Node ≤0.12 anymore. (It would be nice if pg didn’t have to be either…) |
|
@charmander - I agree. In 1 week I'll be leaving full time employment to do freelancing and more full-time node-postgres support. At that point we should have a discussion about dropping old versions, splitting out promises into a sub-module of sorts, and doing other backwards incompatible changes. I want 7.x and beyond to be valuable and shed a bit of the support for versions of node that are > 3 years old. |
|
Sounds good @brianc! Let me know if you need any help with this PR. |
|
@brianc Let's wrap this up. This would be useful. |
| var pool = new Pool({ | ||
| host: 'no-postgres-server-here.com', | ||
| connectionSetup: function () { | ||
| called = true |
There was a problem hiding this comment.
This needs to call the callback to fail properly.
| pool.query('SELECT $1::text as name', ['brianc'], function (err, res) { | ||
| expect(err).to.be.an(Error) | ||
| expect(called).to.be(false) | ||
| pool.end(function (err) { |
| it('does not call setupConnection when an error occurs', function (done) { | ||
| var called = false | ||
| var pool = new Pool({ | ||
| host: 'no-postgres-server-here.com', |
There was a problem hiding this comment.
Maybe start a net.Server on a random port and use it as the host instead, to make the test fully local.
I propose to add an option 'connectionSetup' to make it possible to do setup before a client is used. My own use-case is to set the search path to a certain schema.
This relates to an open issue at node-postgres about the same use case: brianc/node-postgres#1123
I use node-postgres in combination with pg-async and with the adjustment you would be able to do something like this: