sequelize / cli

The Sequelize CLI
MIT License
2.53k stars 527 forks source link

Improve documentation for async configuration object #668

Open prawana-perera opened 6 years ago

prawana-perera commented 6 years ago

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 commented 6 years ago

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

regisbsb commented 6 years ago

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

regisbsb commented 6 years ago

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

regisbsb commented 6 years ago

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

mooniker commented 5 years ago

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?

MatteoGioioso commented 4 years ago

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

papb commented 4 years ago

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

AKhoo commented 4 years ago

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 };
});
Henridv commented 4 years ago

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