Search code examples
javascriptsequelize.jssequelize-cli

sequelize dynamic db config


I want to ask if it's possible to use promise based config in sequelize-cli. So, the idea behind it is that my config file is in AWS S3, but it's not formatted to match sequelize's config file (I have to reformat it in the code)

I didn't find anything in the documentation. http://docs.sequelizejs.com/manual/tutorial/migrations.html#dynamic-configuration. They say they can use a js file, but can the js file download the config file first from S3?

Thanks!


Solution

  • TL;DR you can export a promise in config.js which return the configuration object. e.g.:

    module.exports = somePromise().then(data => {
      ....,
      production: {
        username: data.user,
        password: data.password,
        database: data.db,
        host: data.host,
        dialect: 'mysql',
      },
    })
    

    After an extensive research, I found out that config.js can actually handle promise.

    So to make it work, you need to provide .sequelizerc file in the root folder (where you use sequelize) and copy this to the file

    const path = require('path');
    
    module.exports = {
      'config': path.resolve('config', 'config.js')
    }
    

    then, create a config.js file. These steps is documented in http://docs.sequelizejs.com/manual/tutorial/migrations.html#dynamic-configuration

    The next step is to use promise in config.js. I found an answer on sequelize github issues tracker and found this issue: https://github.com/sequelize/cli/issues/668