From 07d531c5b0635ff4c5492d2edeb8d0bf89fa8c6f Mon Sep 17 00:00:00 2001 From: Jason Dusek Date: Fri, 25 May 2018 23:24:00 -0700 Subject: [PATCH 1/2] Tie default DB name to database user name 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... --- lib/connection-parameters.js | 13 ++++++++++--- test/integration/client/configuration-tests.js | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) 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) diff --git a/test/integration/client/configuration-tests.js b/test/integration/client/configuration-tests.js index 87bb52d47..88c358df4 100644 --- a/test/integration/client/configuration-tests.js +++ b/test/integration/client/configuration-tests.js @@ -40,7 +40,6 @@ 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' @@ -48,7 +47,8 @@ suite.test('modified values are passed to created clients', function () { assert.same(client, { user: 'boom', password: 'zap', - database: 'pow', + // default database name is user name + database: 'boom', port: 1234, host: 'blam' }) From 8a70eb6dc78743d62ff5ed92c72c601407044f32 Mon Sep 17 00:00:00 2001 From: Jason Dusek Date: Wed, 30 May 2018 13:20:34 -0700 Subject: [PATCH 2/2] Major version increment --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d3569be96..cdd4a4ce7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg", - "version": "7.4.3", + "version": "8.0.0", "description": "PostgreSQL client - pure javascript & libpq with the same API", "keywords": [ "database",