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

allow both connectionString and additional options #1363

Merged
merged 1 commit into from Aug 4, 2017
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
11 changes: 6 additions & 5 deletions lib/connection-parameters.js
Expand Up @@ -11,6 +11,8 @@ var dns = require('dns')

var defaults = require('./defaults')

var parse = require('pg-connection-string').parse // parses a connection string

var val = function (key, config, envVar) {
if (envVar === undefined) {
envVar = process.env[ 'PG' + key.toUpperCase() ]
Expand All @@ -25,9 +27,6 @@ var val = function (key, config, envVar) {
defaults[key]
}

// parses a connection string
var parse = require('pg-connection-string').parse

var useSsl = function () {
switch (process.env.PGSSLMODE) {
case 'disable':
Expand All @@ -43,12 +42,14 @@ var useSsl = function () {

var ConnectionParameters = function (config) {
// if a string is passed, it is a raw connection string so we parse it into a config
config = typeof config === 'string' ? parse(config) : (config || {})
config = typeof config === 'string' ? parse(config) : config || {}

// if the config has a connectionString defined, parse IT into the config we use
// this will override other default values with what is stored in connectionString
if (config.connectionString) {
config = parse(config.connectionString)
config = Object.assign({}, config, parse(config.connectionString))
}

this.user = val('user', config)
this.database = val('database', config)
this.port = parseInt(val('port', config), 10)
Expand Down
5 changes: 1 addition & 4 deletions lib/index.js
Expand Up @@ -15,10 +15,7 @@ var Pool = require('pg-pool')

const poolFactory = (Client) => {
var BoundPool = function (options) {
var config = { Client: Client }
for (var key in options) {
config[key] = options[key]
}
var config = Object.assign({ Client: Client }, options)
return new Pool(config)
}

Expand Down
22 changes: 22 additions & 0 deletions test/unit/connection-parameters/creation-tests.js
Expand Up @@ -67,6 +67,28 @@ test('ConnectionParameters initializing from config', function () {
assert.ok(subject.isDomainSocket === false)
})

test('ConnectionParameters initializing from config and config.connectionString', function() {
var subject1 = new ConnectionParameters({
connectionString: 'postgres://test@host/db'
})
var subject2 = new ConnectionParameters({
connectionString: 'postgres://test@host/db?ssl=1'
})
var subject3 = new ConnectionParameters({
connectionString: 'postgres://test@host/db',
ssl: true
})
var subject4 = new ConnectionParameters({
connectionString: 'postgres://test@host/db?ssl=1',
ssl: false
})

assert.equal(subject1.ssl, false)
assert.equal(subject2.ssl, true)
assert.equal(subject3.ssl, true)
assert.equal(subject4.ssl, true)
});

test('escape spaces if present', function () {
var subject = new ConnectionParameters('postgres://localhost/post gres')
assert.equal(subject.database, 'post gres')
Expand Down