Skip to content

Commit 00e3157

Browse files
author
mjgp2
committed
Add support for password function to native
1 parent 5538df6 commit 00e3157

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

Diff for: packages/pg/lib/connection-parameters.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,25 @@ class ConnectionParameters {
125125
}
126126

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

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

+24
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,30 @@ suite.testAsync('builds simple string', async function () {
187187
})
188188
})
189189

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

0 commit comments

Comments
 (0)