Skip to content

Commit 3a6b841

Browse files
caubbrianc
authored andcommitted
allow both connectionString and additional options
1 parent 6517207 commit 3a6b841

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

lib/connection-parameters.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var dns = require('dns')
1111

1212
var defaults = require('./defaults')
1313

14+
var parse = require('pg-connection-string').parse // parses a connection string
15+
1416
var val = function (key, config, envVar) {
1517
if (envVar === undefined) {
1618
envVar = process.env[ 'PG' + key.toUpperCase() ]
@@ -25,9 +27,6 @@ var val = function (key, config, envVar) {
2527
defaults[key]
2628
}
2729

28-
// parses a connection string
29-
var parse = require('pg-connection-string').parse
30-
3130
var useSsl = function () {
3231
switch (process.env.PGSSLMODE) {
3332
case 'disable':
@@ -43,12 +42,14 @@ var useSsl = function () {
4342

4443
var ConnectionParameters = function (config) {
4544
// if a string is passed, it is a raw connection string so we parse it into a config
46-
config = typeof config === 'string' ? parse(config) : (config || {})
45+
config = typeof config === 'string' ? parse(config) : config || {}
46+
4747
// if the config has a connectionString defined, parse IT into the config we use
4848
// this will override other default values with what is stored in connectionString
4949
if (config.connectionString) {
50-
config = parse(config.connectionString)
50+
config = Object.assign({}, config, parse(config.connectionString))
5151
}
52+
5253
this.user = val('user', config)
5354
this.database = val('database', config)
5455
this.port = parseInt(val('port', config), 10)

lib/index.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ var Pool = require('pg-pool')
1515

1616
const poolFactory = (Client) => {
1717
var BoundPool = function (options) {
18-
var config = { Client: Client }
19-
for (var key in options) {
20-
config[key] = options[key]
21-
}
18+
var config = Object.assign({ Client: Client }, options)
2219
return new Pool(config)
2320
}
2421

test/unit/connection-parameters/creation-tests.js

+22
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ test('ConnectionParameters initializing from config', function () {
6767
assert.ok(subject.isDomainSocket === false)
6868
})
6969

70+
test('ConnectionParameters initializing from config and config.connectionString', function() {
71+
var subject1 = new ConnectionParameters({
72+
connectionString: 'postgres://test@host/db'
73+
})
74+
var subject2 = new ConnectionParameters({
75+
connectionString: 'postgres://test@host/db?ssl=1'
76+
})
77+
var subject3 = new ConnectionParameters({
78+
connectionString: 'postgres://test@host/db',
79+
ssl: true
80+
})
81+
var subject4 = new ConnectionParameters({
82+
connectionString: 'postgres://test@host/db?ssl=1',
83+
ssl: false
84+
})
85+
86+
assert.equal(subject1.ssl, false)
87+
assert.equal(subject2.ssl, true)
88+
assert.equal(subject3.ssl, true)
89+
assert.equal(subject4.ssl, true)
90+
});
91+
7092
test('escape spaces if present', function () {
7193
var subject = new ConnectionParameters('postgres://localhost/post gres')
7294
assert.equal(subject.database, 'post gres')

0 commit comments

Comments
 (0)