Skip to content

feat: add connection parameter nativeConnectionString #2941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/pg/lib/native/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var Client = (module.exports = function (config) {
// keep these on the object for legacy reasons
// for the time being. TODO: deprecate all this jazz
var cp = (this.connectionParameters = new ConnectionParameters(config))
if (config.nativeConnectionString) cp.nativeConnectionString = config.nativeConnectionString
this.user = cp.user

// "hiding" the password so it doesn't show up in stack traces
Expand Down Expand Up @@ -82,6 +83,7 @@ Client.prototype._connect = function (cb) {
this._connecting = true

this.connectionParameters.getLibpqConnectionString(function (err, conString) {
if (self.connectionParameters.nativeConnectionString) conString = self.connectionParameters.nativeConnectionString
if (err) return cb(err)
self.native.connect(conString, function (err) {
if (err) {
Expand Down
50 changes: 50 additions & 0 deletions packages/pg/test/native/native-connection-string-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'
var helper = require('../test-helper')
var Client = require('../../lib/native')
const suite = new helper.Suite()

suite.test('respects nativeConnectionString in config', function (done) {
const realPort = helper.config.port
const nativeConnectionString = `host=${helper.config.host} port=${helper.config.port} dbname=${helper.config.database} user=${helper.config.user} password=${helper.config.password}`

// setting wrong port to make sure config is take from nativeConnectionString and not env
helper.config.port = '90929'

var client = new Client({
...helper.config,
nativeConnectionString,
})

client.connect(function (err) {
assert(!err)
client.query(
'SELECT 1 as num',
assert.calls(function (err, result) {
assert(!err)
assert.equal(result.rows[0].num, 1)
assert.strictEqual(result.rowCount, 1)
// restore post in case helper config will be reused
helper.config.port = realPort
client.end(done)
})
)
})
})

suite.test('respects nativeConnectionString in config even when it is corrupted', function (done) {
const nativeConnectionString = `foobar`

var client = new Client({
nativeConnectionString,
})

client.connect(function (err) {
assert(err)
assert.equal(
err.message,
'missing "=" after "foobar" in connection info string\n',
'Connection error should have been thrown'
)
client.end(done)
})
})