Skip to content

Commit

Permalink
Support options connection parameter
Browse files Browse the repository at this point in the history
This supports the connection parameter documented here:
https://www.postgresql.org/docs/9.1/libpq-connect.html#LIBPQ-CONNECT-OPTIONS
  • Loading branch information
rafiss committed May 13, 2020
1 parent 70c8e5f commit d0d8cef
Show file tree
Hide file tree
Showing 6 changed files with 18 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
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
5 changes: 5 additions & 0 deletions packages/pg/test/integration/client/configuration-tests.js
Expand Up @@ -26,6 +26,7 @@ suite.test('default values are used in new clients', function () {
application_name: undefined,
fallback_application_name: undefined,
parseInputDatesAsUTC: false,
options: undefined,
})

var client = new pg.Client()
Expand All @@ -43,6 +44,7 @@ suite.test('modified values are passed to created clients', function () {
pg.defaults.database = 'pow'
pg.defaults.port = 1234
pg.defaults.host = 'blam'
pg.defaults.options = 'cats'

var client = new Client()
assert.same(client, {
Expand All @@ -51,6 +53,9 @@ suite.test('modified values are passed to created clients', function () {
database: 'pow',
port: 1234,
host: 'blam',
connectionParameters: {
options: 'cats',
},
})
})

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 d0d8cef

Please sign in to comment.