MCPcopy Create free account
hub / github.com/porsager/postgres / parseOptions

Function parseOptions

cf/src/index.js:431–502  ·  view source on GitHub ↗
(a, b)

Source from the content-addressed store, hash-verified

429}
430
431function parseOptions(a, b) {
432 if (a && a.shared)
433 return a
434
435 const env = process.env // eslint-disable-line
436 , o = (!a || typeof a === 'string' ? b : a) || {}
437 , { url, multihost } = parseUrl(a)
438 , query = [...url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})
439 , host = o.hostname || o.host || multihost || url.hostname || env.PGHOST || 'localhost'
440 , port = o.port || url.port || env.PGPORT || 5432
441 , user = o.user || o.username || url.username || env.PGUSERNAME || env.PGUSER || osUsername()
442
443 o.no_prepare && (o.prepare = false)
444 query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)
445 'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line
446 query.sslrootcert === 'system' && (query.ssl = 'verify-full')
447
448 const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']
449 const defaults = {
450 max : globalThis.Cloudflare ? 3 : 10,
451 ssl : false,
452 sslnegotiation : null,
453 idle_timeout : null,
454 connect_timeout : 30,
455 max_lifetime : max_lifetime,
456 max_pipeline : 100,
457 backoff : backoff,
458 keep_alive : 60,
459 prepare : true,
460 debug : false,
461 fetch_types : true,
462 publications : 'alltables',
463 target_session_attrs: null
464 }
465
466 return {
467 host : Array.isArray(host) ? host : host.split(',').map(x => x.split(':')[0]),
468 port : Array.isArray(port) ? port : host.split(',').map(x => parseInt(x.split(':')[1] || port)),
469 path : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,
470 database : o.database || o.db || (url.pathname || '').slice(1) || env.PGDATABASE || user,
471 user : user,
472 pass : o.pass || o.password || url.password || env.PGPASSWORD || '',
473 ...Object.entries(defaults).reduce(
474 (acc, [k, d]) => {
475 const value = k in o ? o[k] : k in query
476 ? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
477 : env['PG' + k.toUpperCase()] || d
478 acc[k] = typeof value === 'string' && ints.includes(k)
479 ? +value
480 : value
481 return acc
482 },
483 {}
484 ),
485 connection : {
486 application_name: env.PGAPPNAME || 'postgres.js',
487 ...o.connection,
488 ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})

Callers 1

PostgresFunction · 0.70

Calls 5

mergeUserTypesFunction · 0.90
parseUrlFunction · 0.70
osUsernameFunction · 0.70
tsaFunction · 0.70
parseTransformFunction · 0.70

Tested by

no test coverage detected