Skip to content

Commit 7d63a16

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

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
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) {

test/unit/connection-parameters/creation-tests.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ test('ConnectionParameters construction', function () {
1616
})
1717

1818
var compare = function (actual, expected, type) {
19+
const expectedDatabase =
20+
expected.database === undefined
21+
? expected.user
22+
: expected.database
23+
1924
assert.equal(actual.user, expected.user, type + ' user')
20-
assert.equal(actual.database, expected.database, type + ' database')
25+
assert.equal(actual.database, expectedDatabase, type + ' database')
2126
assert.equal(actual.port, expected.port, type + ' port')
2227
assert.equal(actual.host, expected.host, type + ' host')
2328
assert.equal(actual.password, expected.password, type + ' password')

0 commit comments

Comments
 (0)