@@ -376,44 +376,62 @@ function parseOptions(a, b) {
376
376
const env = process . env // eslint-disable-line
377
377
, o = ( typeof a === 'string' ? b : a ) || { }
378
378
, { url, multihost } = parseUrl ( a )
379
- , query = url . searchParams
379
+ , query = [ ... url . searchParams ] . reduce ( ( a , [ b , c ] ) => ( a [ b ] = c , a ) , { } )
380
380
, host = o . hostname || o . host || multihost || url . hostname || env . PGHOST || 'localhost'
381
381
, port = o . port || url . port || env . PGPORT || 5432
382
382
, user = o . user || o . username || url . username || env . PGUSERNAME || env . PGUSER || osUsername ( )
383
383
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 {
385
404
host : Array . isArray ( host ) ? host : host . split ( ',' ) . map ( x => x . split ( ':' ) [ 0 ] ) ,
386
405
port : Array . isArray ( port ) ? port : host . split ( ',' ) . map ( x => parseInt ( x . split ( ':' ) [ 1 ] || port ) ) ,
387
406
path : o . path || host . indexOf ( '/' ) > - 1 && host + '/.s.PGSQL.' + port ,
388
407
database : o . database || o . db || ( url . pathname || '' ) . slice ( 1 ) || env . PGDATABASE || user ,
389
408
user : user ,
390
409
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
+ } ,
392
423
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 ) ,
401
425
onnotice : o . onnotice ,
402
426
onnotify : o . onnotify ,
403
427
onclose : o . onclose ,
404
428
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 ,
409
429
socket : o . socket ,
410
- fetch_types : 'fetch_types' in o ? o . fetch_types : true ,
430
+ transform : parseTransform ( o . transform || { undefined : undefined } ) ,
411
431
parameters : { } ,
412
432
shared : { retries : 0 , typeArrayMap : { } } ,
413
- publications : o . publications || query . get ( 'publications' ) || 'alltables'
414
- } ,
415
- mergeUserTypes ( o . types )
416
- )
433
+ ...mergeUserTypes ( o . types )
434
+ }
417
435
}
418
436
419
437
function tsa ( o , url , env ) {
@@ -450,10 +468,6 @@ function parseTransform(x) {
450
468
}
451
469
}
452
470
453
- function parseSSL ( x ) {
454
- return x !== 'disable' && x !== 'false' && x
455
- }
456
-
457
471
function parseUrl ( url ) {
458
472
if ( typeof url !== 'string' )
459
473
return { url : { searchParams : new Map ( ) } }
@@ -469,11 +483,6 @@ function parseUrl(url) {
469
483
}
470
484
}
471
485
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
-
477
486
function osUsername ( ) {
478
487
try {
479
488
return os . userInfo ( ) . username // eslint-disable-line
0 commit comments