Skip to content

Commit 4107323

Browse files
committed
Use user name as default database when user is non-default
Not entirely backwards-compatible.
1 parent 3ac356a commit 4107323

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

lib/connection-parameters.js

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ var ConnectionParameters = function (config) {
5252

5353
this.user = val('user', config)
5454
this.database = val('database', config)
55+
56+
if (this.database === undefined) {
57+
this.database = this.user
58+
}
59+
5560
this.port = parseInt(val('port', config), 10)
5661
this.host = val('host', config)
5762
this.password = val('password', config)

lib/defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
user: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
1616

1717
// name of database to connect
18-
database: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
18+
database: undefined,
1919

2020
// database user's password
2121
password: null,

test/integration/client/configuration-tests.js

+20
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ suite.test('modified values are passed to created clients', function () {
5454
})
5555
})
5656

57+
suite.test('database defaults to user when user is non-default', () => {
58+
{
59+
const client = new Client({
60+
user: 'foo',
61+
})
62+
63+
assert.strictEqual(client.database, 'foo')
64+
}
65+
66+
{
67+
pg.defaults.database = 'bar'
68+
69+
const client = new Client({
70+
user: 'foo',
71+
})
72+
73+
assert.strictEqual(client.database, 'bar')
74+
}
75+
})
76+
5777
suite.test('cleanup', () => {
5878
// restore process.env
5979
for (var key in realEnv) {

0 commit comments

Comments
 (0)