Skip to content

Commit

Permalink
Use user name as default database when user is non-default
Browse files Browse the repository at this point in the history
Not entirely backwards-compatible.
  • Loading branch information
charmander committed Jun 17, 2018
1 parent 3ac356a commit 4107323
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/connection-parameters.js
Expand Up @@ -52,6 +52,11 @@ var ConnectionParameters = function (config) {

this.user = val('user', config)
this.database = val('database', config)

if (this.database === undefined) {
this.database = this.user
}

this.port = parseInt(val('port', config), 10)
this.host = val('host', config)
this.password = val('password', config)
Expand Down
2 changes: 1 addition & 1 deletion lib/defaults.js
Expand Up @@ -15,7 +15,7 @@ module.exports = {
user: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,

// name of database to connect
database: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
database: undefined,

// database user's password
password: null,
Expand Down
20 changes: 20 additions & 0 deletions test/integration/client/configuration-tests.js
Expand Up @@ -54,6 +54,26 @@ suite.test('modified values are passed to created clients', function () {
})
})

suite.test('database defaults to user when user is non-default', () => {
{
const client = new Client({
user: 'foo',
})

assert.strictEqual(client.database, 'foo')
}

{
pg.defaults.database = 'bar'

const client = new Client({
user: 'foo',
})

assert.strictEqual(client.database, 'bar')
}
})

suite.test('cleanup', () => {
// restore process.env
for (var key in realEnv) {
Expand Down

0 comments on commit 4107323

Please sign in to comment.