Skip to content

Commit

Permalink
Tie default DB name to database user name
Browse files Browse the repository at this point in the history
The previous behaviour was at variance with the native client behaviour,
per:

  https://www.postgresql.org/docs/10/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS

> The database name. Defaults to be the same as the user name.
> In certain contexts, the value is checked for extended formats...
  • Loading branch information
solidsnack committed May 26, 2018
1 parent 3ac356a commit 07d531c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/connection-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var defaults = require('./defaults')

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

var val = function (key, config, envVar) {
var val = function (key, config, envVar, computed) {
if (envVar === undefined) {
envVar = process.env[ 'PG' + key.toUpperCase() ]
} else if (envVar === false) {
Expand All @@ -22,9 +22,13 @@ var val = function (key, config, envVar) {
envVar = process.env[ envVar ]
}

if (computed === undefined) {
computed = function (key) { return defaults[key] }
}

return config[key] ||
envVar ||
defaults[key]
computed(key)
}

var useSsl = function () {
Expand All @@ -50,8 +54,11 @@ var ConnectionParameters = function (config) {
config = Object.assign({}, config, parse(config.connectionString))
}

var dbDefaulting = function () { return this.user || defaults['database'] }
dbDefaulting = dbDefaulting.bind(this)

this.user = val('user', config)
this.database = val('database', config)
this.database = val('database', config, undefined, dbDefaulting)
this.port = parseInt(val('port', config), 10)
this.host = val('host', config)
this.password = val('password', config)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/client/configuration-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ suite.test('default values are used in new clients', function () {
suite.test('modified values are passed to created clients', function () {
pg.defaults.user = 'boom'
pg.defaults.password = 'zap'
pg.defaults.database = 'pow'
pg.defaults.port = 1234
pg.defaults.host = 'blam'

var client = new Client()
assert.same(client, {
user: 'boom',
password: 'zap',
database: 'pow',
// default database name is user name
database: 'boom',
port: 1234,
host: 'blam'
})
Expand Down

0 comments on commit 07d531c

Please sign in to comment.