Skip to content

Commit

Permalink
Merge pull request #2216 from rafiss/pgoptions
Browse files Browse the repository at this point in the history
Support options connection parameter
  • Loading branch information
brianc committed Jul 8, 2020
2 parents dec892e + 06cdf3e commit da5d4ef
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/pg-connection-string/index.d.ts
Expand Up @@ -11,4 +11,5 @@ export interface ConnectionOptions {

application_name?: string
fallback_application_name?: string
options?: string
}
6 changes: 3 additions & 3 deletions packages/pg-connection-string/test/parse.js
Expand Up @@ -188,10 +188,10 @@ describe('parse', function () {
subject.fallback_application_name.should.equal('TheAppFallback')
})

it('configuration parameter fallback_application_name', function () {
var connectionString = 'pg:///?fallback_application_name=TheAppFallback'
it('configuration parameter options', function () {
var connectionString = 'pg:///?options=-c geqo=off'
var subject = parse(connectionString)
subject.fallback_application_name.should.equal('TheAppFallback')
subject.options.should.equal('-c geqo=off')
})

it('configuration parameter ssl=true', function () {
Expand Down
3 changes: 3 additions & 0 deletions packages/pg/lib/client.js
Expand Up @@ -391,6 +391,9 @@ Client.prototype.getStartupConf = function () {
if (params.idle_in_transaction_session_timeout) {
data.idle_in_transaction_session_timeout = String(parseInt(params.idle_in_transaction_session_timeout, 10))
}
if (params.options) {
data.options = params.options
}

return data
}
Expand Down
2 changes: 2 additions & 0 deletions packages/pg/lib/connection-parameters.js
Expand Up @@ -70,6 +70,7 @@ var ConnectionParameters = function (config) {
})

this.binary = val('binary', config)
this.options = val('options', config)

this.ssl = typeof config.ssl === 'undefined' ? readSSLConfigFromEnvironment() : config.ssl

Expand Down Expand Up @@ -126,6 +127,7 @@ ConnectionParameters.prototype.getLibpqConnectionString = function (cb) {
add(params, this, 'application_name')
add(params, this, 'fallback_application_name')
add(params, this, 'connect_timeout')
add(params, this, 'options')

var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
add(params, ssl, 'sslmode')
Expand Down
2 changes: 2 additions & 0 deletions packages/pg/lib/defaults.js
Expand Up @@ -53,6 +53,8 @@ module.exports = {

fallback_application_name: undefined,

options: undefined,

parseInputDatesAsUTC: false,

// max milliseconds any query using this connection will execute for before timing out in error.
Expand Down
Expand Up @@ -25,6 +25,7 @@ var compare = function (actual, expected, type) {
assert.equal(actual.password, expected.password, type + ' password')
assert.equal(actual.binary, expected.binary, type + ' binary')
assert.equal(actual.statement_timeout, expected.statement_timeout, type + ' statement_timeout')
assert.equal(actual.options, expected.options, type + ' options')
assert.equal(
actual.idle_in_transaction_session_timeout,
expected.idle_in_transaction_session_timeout,
Expand All @@ -48,12 +49,14 @@ test('ConnectionParameters initializing from defaults with connectionString set'
binary: defaults.binary,
statement_timeout: false,
idle_in_transaction_session_timeout: false,
options: '-c geqo=off',
}

var original_value = defaults.connectionString
// Just changing this here doesn't actually work because it's no longer in scope when viewed inside of
// of ConnectionParameters() so we have to pass in the defaults explicitly to test it
defaults.connectionString = 'postgres://brians-are-the-best:mypassword@foo.bar.net:7777/scoobysnacks'
defaults.connectionString =
'postgres://brians-are-the-best:mypassword@foo.bar.net:7777/scoobysnacks?options=-c geqo=off'
var subject = new ConnectionParameters(defaults)
defaults.connectionString = original_value
compare(subject, config, 'defaults-connectionString')
Expand All @@ -73,6 +76,7 @@ test('ConnectionParameters initializing from config', function () {
},
statement_timeout: 15000,
idle_in_transaction_session_timeout: 15000,
options: '-c geqo=off',
}
var subject = new ConnectionParameters(config)
compare(subject, config, 'config')
Expand Down

0 comments on commit da5d4ef

Please sign in to comment.