Skip to content

Commit 610ecd2

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

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
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

+23-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ for (var key in process.env) {
1414
suite.test('default values are used in new clients', function () {
1515
assert.same(pg.defaults, {
1616
user: process.env.USER,
17-
database: process.env.USER,
17+
database: undefined,
1818
password: null,
1919
port: 5432,
2020
rows: 0,
@@ -54,6 +54,28 @@ 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+
pg.defaults.database = undefined
60+
61+
const client = new Client({
62+
user: 'foo',
63+
})
64+
65+
assert.strictEqual(client.database, 'foo')
66+
}
67+
68+
{
69+
pg.defaults.database = 'bar'
70+
71+
const client = new Client({
72+
user: 'foo',
73+
})
74+
75+
assert.strictEqual(client.database, 'bar')
76+
}
77+
})
78+
5779
suite.test('cleanup', () => {
5880
// restore process.env
5981
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)