Skip to content

Commit 41ed84f

Browse files
committed
Pass on rest url params to connection (ootb support cockroach urls)
1 parent e67da29 commit 41ed84f

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ const sql = postgres('postgres://username:password@host:port/database', {
767767
max_lifetime : null, // Max lifetime in seconds (more info below)
768768
idle_timeout : 0, // Idle connection timeout in seconds
769769
connect_timeout : 30, // Connect timeout in seconds
770-
no_prepare : false, // No automatic creation of prepared statements
770+
prepare : true, // Automatic creation of prepared statements
771771
types : [], // Array of custom types, see more below
772772
onnotice : fn, // Defaults to console.log
773773
onparameter : fn, // (key, value) when server param change

src/index.js

+38-29
Original file line numberDiff line numberDiff line change
@@ -376,44 +376,62 @@ function parseOptions(a, b) {
376376
const env = process.env // eslint-disable-line
377377
, o = (typeof a === 'string' ? b : a) || {}
378378
, { url, multihost } = parseUrl(a)
379-
, query = url.searchParams
379+
, query = [...url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})
380380
, host = o.hostname || o.host || multihost || url.hostname || env.PGHOST || 'localhost'
381381
, port = o.port || url.port || env.PGPORT || 5432
382382
, user = o.user || o.username || url.username || env.PGUSERNAME || env.PGUSER || osUsername()
383383

384-
return Object.assign({
384+
o.no_prepare && (o.prepare = false)
385+
query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)
386+
'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line
387+
388+
const defaults = {
389+
max : 10,
390+
ssl : false,
391+
idle_timeout : null,
392+
connect_timeout : 30,
393+
max_lifetime : max_lifetime,
394+
max_pipeline : 100,
395+
backoff : backoff,
396+
keep_alive : 60,
397+
prepare : true,
398+
debug : false,
399+
fetch_types : true,
400+
publications : 'alltables'
401+
}
402+
403+
return {
385404
host : Array.isArray(host) ? host : host.split(',').map(x => x.split(':')[0]),
386405
port : Array.isArray(port) ? port : host.split(',').map(x => parseInt(x.split(':')[1] || port)),
387406
path : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,
388407
database : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,
389408
user : user,
390409
pass : o.pass || o.password || url.password || env.PGPASSWORD || '',
391-
max : o.max || query.get('max') || 10,
410+
...Object.entries(defaults).reduce((acc, [k, d]) =>
411+
(acc[k] = k in o ? o[k] : k in query
412+
? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
413+
: env['PG' + k.toUpperCase()] || d,
414+
acc
415+
),
416+
{}
417+
),
418+
connection : {
419+
application_name: 'postgres.js',
420+
...o.connection,
421+
...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})
422+
},
392423
types : o.types || {},
393-
ssl : o.ssl || parseSSL(query.get('sslmode') || query.get('ssl')) || false,
394-
idle_timeout : o.idle_timeout || query.get('idle_timeout') || env.PGIDLE_TIMEOUT || warn(o.timeout),
395-
connect_timeout : o.connect_timeout || query.get('connect_timeout') || env.PGCONNECT_TIMEOUT || 30,
396-
max_lifetime : o.max_lifetime || url.max_lifetime || max_lifetime,
397-
max_pipeline : o.max_pipeline || url.max_pipeline || 100,
398-
backoff : o.backoff || url.backoff || backoff,
399-
keep_alive : o.keep_alive || url.keep_alive || 60,
400-
prepare : 'prepare' in o ? o.prepare : 'no_prepare' in o ? !o.no_prepare : true,
424+
target_session_attrs: tsa(o, url, env),
401425
onnotice : o.onnotice,
402426
onnotify : o.onnotify,
403427
onclose : o.onclose,
404428
onparameter : o.onparameter,
405-
transform : parseTransform(o.transform || { undefined: undefined }),
406-
connection : Object.assign({ application_name: 'postgres.js' }, o.connection),
407-
target_session_attrs: tsa(o, url, env),
408-
debug : o.debug,
409429
socket : o.socket,
410-
fetch_types : 'fetch_types' in o ? o.fetch_types : true,
430+
transform : parseTransform(o.transform || { undefined: undefined }),
411431
parameters : {},
412432
shared : { retries: 0, typeArrayMap: {} },
413-
publications : o.publications || query.get('publications') || 'alltables'
414-
},
415-
mergeUserTypes(o.types)
416-
)
433+
...mergeUserTypes(o.types)
434+
}
417435
}
418436

419437
function tsa(o, url, env) {
@@ -450,10 +468,6 @@ function parseTransform(x) {
450468
}
451469
}
452470

453-
function parseSSL(x) {
454-
return x !== 'disable' && x !== 'false' && x
455-
}
456-
457471
function parseUrl(url) {
458472
if (typeof url !== 'string')
459473
return { url: { searchParams: new Map() } }
@@ -469,11 +483,6 @@ function parseUrl(url) {
469483
}
470484
}
471485

472-
function warn(x) {
473-
typeof x !== 'undefined' && console.log('The timeout option is deprecated, use idle_timeout instead') // eslint-disable-line
474-
return x
475-
}
476-
477486
function osUsername() {
478487
try {
479488
return os.userInfo().username // eslint-disable-line

0 commit comments

Comments
 (0)