Skip to content

Commit

Permalink
Add support for password function to native
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgp2 committed May 3, 2022
1 parent f5e87ac commit a542253
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/pg/lib/connection-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,25 @@ class ConnectionParameters {
}

getLibpqConnectionString(cb) {
var params = []
add(params, this, 'user')
add(params, this, 'password')
if (typeof this.password === 'function') {
const pw = this.password();
if (typeof pw === 'string') {
return this._getLibpqConnectionString(cb, pw);
}
pw.then(pwString => this._getLibpqConnectionString(cb, pwString));
return;
}
this._getLibpqConnectionString(cb);
}

_getLibpqConnectionString(cb, pw) {
var params = [];
add(params, this, 'user');
if (pw != null) {
params.push('password=' + quoteParamValue(pw))
} else {
add(params, this, 'password');
}
add(params, this, 'port')
add(params, this, 'application_name')
add(params, this, 'fallback_application_name')
Expand Down
24 changes: 24 additions & 0 deletions packages/pg/test/unit/connection-parameters/creation-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ suite.testAsync('builds simple string', async function () {
})
})

suite.testAsync('builds simple string with pw function', async function () {
var config = {
user: 'brian',
password: () => 'xyz',
port: 888,
host: 'localhost',
database: 'bam',
}
var subject = new ConnectionParameters(config)
const dnsHost = await getDNSHost(config.host)
return new Promise((resolve) => {
subject.getLibpqConnectionString(function (err, constring) {
assert(!err)
var parts = constring.split(' ')
checkForPart(parts, "user='brian'")
checkForPart(parts, "password='xyz'")
checkForPart(parts, "port='888'")
checkForPart(parts, `hostaddr='${dnsHost}'`)
checkForPart(parts, "dbname='bam'")
resolve()
})
})
})

suite.test('builds dns string', async function () {
var config = {
user: 'brian',
Expand Down

0 comments on commit a542253

Please sign in to comment.