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?

sqldoug
  • 429
  • 1
  • 3
  • 10
  • See this: https://stackoverflow.com/questions/34382796/where-should-i-initialize-pg-promise. Also, unlcear what `pgp(connStr, (err) =>` means, the library only takes a connection string/object. – vitaly-t Jul 25 '17 at 17:50
  • Thanks; edited for pgp. – sqldoug Jul 25 '17 at 18:08

1 Answers1

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:

vitaly-t
  • 24,279
  • 15
  • 116
  • 138