cli
cli copied to clipboard
Improve documentation for async configuration object
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.
I'm happy to create a PR to update the documentation if you think the above is ok.
@sushantdhiman me too... I've lost a couple of hours to find that out.
I can help with pull request if pointed to right direction.
https://github.com/sequelize/cli/issues/637
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?
So, is this available or not? Have the docs been implemented? Could not find anything
See https://github.com/sequelize/sequelize/issues/11652
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 };
});
For what it's worth: I have been able to use the OP's solution using the CLI.