Skip to content

Commit 0b33b71

Browse files
committed
Fallback to user for db name instead of 'postgres' - fixes porsager#141
1 parent 559ebd9 commit 0b33b71

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

lib/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,14 @@ function parseOptions(a, b) {
518518
, auth = (url.auth || '').split(':')
519519
, host = o.hostname || o.host || url.hostname || env.PGHOST || 'localhost'
520520
, port = o.port || url.port || env.PGPORT || 5432
521+
, user = o.user || o.username || auth[0] || env.PGUSERNAME || env.PGUSER || osUsername()
521522

522523
return Object.assign({
523524
host,
524525
port,
525526
path : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,
526-
database : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || 'postgres',
527-
user : o.user || o.username || auth[0] || env.PGUSERNAME || env.PGUSER || osUsername(),
527+
database : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,
528+
user : user,
528529
pass : o.pass || o.password || auth[1] || env.PGPASSWORD || '',
529530
max : o.max || url.query.max || 10,
530531
types : o.types || {},

tests/index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ t('Connects with no options', async() => {
5353
return [1, result]
5454
})
5555

56-
t('Uses default database without slash', async() =>
57-
['postgres', postgres('postgres://localhost').options.database]
58-
)
56+
t('Uses default database without slash', async() => {
57+
const sql = postgres('postgres://localhost')
58+
return [sql.options.user, sql.options.database]
59+
})
5960

60-
t('Uses default database with slash', async() =>
61-
['postgres', postgres('postgres://localhost/').options.database]
62-
)
61+
t('Uses default database with slash', async() => {
62+
const sql = postgres('postgres://localhost/')
63+
return [sql.options.user, sql.options.database]
64+
})
6365

6466
t('Result is array', async() =>
6567
[true, Array.isArray(await sql`select 1`)]

0 commit comments

Comments
 (0)