cli icon indicating copy to clipboard operation
cli copied to clipboard

Improve documentation for async configuration object

Open prawana-perera opened this issue 7 years ago • 9 comments

What you are doing?

Hi, we have a requirement to asynchronously fetch the database password (e.g. from our secrets management service). The documentation on dynamic configuration (http://docs.sequelizejs.com/manual/tutorial/migrations.html#dynamic-configuration) does not seem to mention how to do this.

However having looked at the source code for how the config object is loaded (https://github.com/sequelize/cli/blob/master/src/helpers/config-helper.js#L117), this seems to be supported. I.e. it checks whether the config object is an object or promise. So we were able to do the following:

const {myAsyncPasswordFunction} = require('.....')

module.exports = async () => ({
  ....,
  production: {
    username: process.env.USER,
    password: await myAsyncPasswordFunction(),
    database: 'mydb',
    host: process.env.HOST,
    dialect: 'my-db-dialect',
  },
})

What do you expect to happen?

This info maybe helpful for others who maybe looking around. So it would be great if the documentation could be updated on how to use an async configuration object.

Thanks.

prawana-perera avatar Jun 25 '18 04:06 prawana-perera

I'm happy to create a PR to update the documentation if you think the above is ok.

prawana-perera avatar Jun 25 '18 04:06 prawana-perera

@sushantdhiman me too... I've lost a couple of hours to find that out.

regisbsb avatar Jul 26 '18 19:07 regisbsb

I can help with pull request if pointed to right direction.

regisbsb avatar Jul 26 '18 19:07 regisbsb

https://github.com/sequelize/cli/issues/637

regisbsb avatar Jul 26 '18 19:07 regisbsb

Is an async config object still supported (as suggested in OP)? I tried this to access a secrets manager, but it seems that the index models is synchronous and file chokes on TypeError: Cannot read property 'use_env_variable' of undefined - seems there's nothing waiting for the config to resolve or am I missing something?

mooniker avatar Sep 23 '19 16:09 mooniker

So, is this available or not? Have the docs been implemented? Could not find anything

MatteoGioioso avatar Mar 28 '20 05:03 MatteoGioioso

See https://github.com/sequelize/sequelize/issues/11652

papb avatar Mar 28 '20 18:03 papb

I was able to fetch configuration asynchronously by using Sequelize's beforeConnect Connection Hook: https://sequelize.org/master/manual/hooks.html

Here's an example of how I set up my Sequelize connection:

sequelize.beforeConnect(async (config) => {
  const fetchDbConfigDetails = await // implementation not shown
  const pgConnStr = fetchDbConfigDetails.data.value;
  const { host, port, user, password, database } = parsePgConnStr(pgConnStr);
  config.database = database;
  config.username = user;
  config.password = password;
  config.host = host;
  config.port = port;
  config.dialectOptions = { ssl: true };
});

AKhoo avatar May 03 '20 19:05 AKhoo

For what it's worth: I have been able to use the OP's solution using the CLI.

Henridv avatar May 28 '20 21:05 Henridv