Skip to content
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

Support options connection parameter #2216

Merged
merged 1 commit into from Jul 8, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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