Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for password function to native #2741

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/pg/lib/connection-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,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)).catch((e) => cb(e))
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 @@ -187,6 +187,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