diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index f31f28dd3..d61accad6 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -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) { @@ -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 () { @@ -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)