Skip to content

Commit a542253

Browse files
author
mjgp2
committed
Add support for password function to native
1 parent f5e87ac commit a542253

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

packages/pg/lib/connection-parameters.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,25 @@ class ConnectionParameters {
124124
}
125125

126126
getLibpqConnectionString(cb) {
127-
var params = []
128-
add(params, this, 'user')
129-
add(params, this, 'password')
127+
if (typeof this.password === 'function') {
128+
const pw = this.password();
129+
if (typeof pw === 'string') {
130+
return this._getLibpqConnectionString(cb, pw);
131+
}
132+
pw.then(pwString => this._getLibpqConnectionString(cb, pwString));
133+
return;
134+
}
135+
this._getLibpqConnectionString(cb);
136+
}
137+
138+
_getLibpqConnectionString(cb, pw) {
139+
var params = [];
140+
add(params, this, 'user');
141+
if (pw != null) {
142+
params.push('password=' + quoteParamValue(pw))
143+
} else {
144+
add(params, this, 'password');
145+
}
130146
add(params, this, 'port')
131147
add(params, this, 'application_name')
132148
add(params, this, 'fallback_application_name')

packages/pg/test/unit/connection-parameters/creation-tests.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,30 @@ suite.testAsync('builds simple string', async function () {
184184
})
185185
})
186186

187+
suite.testAsync('builds simple string with pw function', async function () {
188+
var config = {
189+
user: 'brian',
190+
password: () => 'xyz',
191+
port: 888,
192+
host: 'localhost',
193+
database: 'bam',
194+
}
195+
var subject = new ConnectionParameters(config)
196+
const dnsHost = await getDNSHost(config.host)
197+
return new Promise((resolve) => {
198+
subject.getLibpqConnectionString(function (err, constring) {
199+
assert(!err)
200+
var parts = constring.split(' ')
201+
checkForPart(parts, "user='brian'")
202+
checkForPart(parts, "password='xyz'")
203+
checkForPart(parts, "port='888'")
204+
checkForPart(parts, `hostaddr='${dnsHost}'`)
205+
checkForPart(parts, "dbname='bam'")
206+
resolve()
207+
})
208+
})
209+
})
210+
187211
suite.test('builds dns string', async function () {
188212
var config = {
189213
user: 'brian',

0 commit comments

Comments
 (0)