Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/pg/lib/connection-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const defaults = require('./defaults')
const parse = require('pg-connection-string').parse // parses a connection string

const val = function (key, config, envVar) {
if (config[key] !== undefined) {
return config[key]
}

if (envVar === undefined) {
envVar = process.env['PG' + key.toUpperCase()]
} else if (envVar === false) {
Expand All @@ -15,7 +19,7 @@ const val = function (key, config, envVar) {
envVar = process.env[envVar]
}

return config[key] || envVar || defaults[key]
return envVar || defaults[key]
}

const readSSLConfigFromEnvironment = function () {
Expand Down
9 changes: 8 additions & 1 deletion packages/pg/lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
'use strict'

let user
try {
user = process.platform === 'win32' ? process.env.USERNAME : process.env.USER
} catch {
// ignore, e.g., Deno without --allow-env
}

module.exports = {
// database host. defaults to localhost
host: 'localhost',

// database user's name
user: process.platform === 'win32' ? process.env.USERNAME : process.env.USER,
user,

// name of database to connect
database: undefined,
Expand Down
54 changes: 30 additions & 24 deletions packages/pg/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,37 @@ const PG = function (clientConstructor) {
this.utils = utils
}

if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
module.exports = new PG(require('./native'))
} else {
module.exports = new PG(Client)
let clientConstructor = Client

// lazy require native module...the native module may not have installed
Object.defineProperty(module.exports, 'native', {
configurable: true,
enumerable: false,
get() {
let native = null
try {
native = new PG(require('./native'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
}
try {
if (process.env.NODE_PG_FORCE_NATIVE) {
clientConstructor = require('./native')
}
} catch {
// ignore, e.g., Deno without --allow-env
}

module.exports = new PG(clientConstructor)

// lazy require native module...the native module may not have installed
Object.defineProperty(module.exports, 'native', {
configurable: true,
enumerable: false,
get() {
let native = null
try {
native = new PG(require('./native'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
}
}

// overwrite module.exports.native so that getter is never called again
Object.defineProperty(module.exports, 'native', {
value: native,
})
// overwrite module.exports.native so that getter is never called again
Object.defineProperty(module.exports, 'native', {
value: native,
})

return native
},
})
}
return native
},
})
Loading