Skip to content

Commit

Permalink
Tie default DB name to database user name
Browse files Browse the repository at this point in the history
Before, the default database name was tied to the system user name, which differs from the behavior of the native client.
  • Loading branch information
solidsnack committed May 26, 2018
1 parent 3ac356a commit 6063d7e
Showing 1 changed file with 10 additions and 3 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

0 comments on commit 6063d7e

Please sign in to comment.