0

We're using HashiCorp's Vault to store database connection credentials, then constructing the connection string for pg-promise from those. The 'catch' is that the Vault details are provided from a Promise wrapper, due to request callbacks to the Vault API.

Example database.js module:

const pgp = require('pg-promise')(/* options obj */);

const getDbo = () => {
    return new Promise( (resolve, reject) => {
        vault.init().then(secrets => {
        let credentials = secrets.dbUser + ':' + secrets.dbPass
        let connStr = 'postgres://' + credentials + '<@endpoint/db>'
        let dbo = pgp(connStr, (err) => {
          reject(err)
        })
        resolve(dbo);
    })
    }
module.exports = { get: getDbo }

This is being imported in multiple routes. With this we are seeing the warning "WARNING: Creating a duplicate database object for the same connection." Is there a better way to resolve this so there is only one object per connection details?

2

1 Answer 1

1

Creating and initializing a connection for pg-promise is a completely synchronous operation, as per the API, so there is no point using promises for that.

For initializing the library see Where should I initialize pg-promise.

See also:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.