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

Function parseOptions

cjs/src/index.js:430–501  ·  view source on GitHub ↗
(a, b)

Source from the content-addressed store, hash-verified

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

Callers 1

PostgresFunction · 0.70

Calls 5

parseUrlFunction · 0.70
osUsernameFunction · 0.70
tsaFunction · 0.70
parseTransformFunction · 0.70
mergeUserTypesFunction · 0.50

Tested by

no test coverage detected