4
votes

First off it's my first time with Sequelize so be patient.

I'd like to use https://github.com/sequelize/cli along with https://github.com/lorenwest/node-config

I want sequelize to be able to "compose" it's configuration from multiple source files, the same manner that node-config does.

By now I've worked it out with

.sequelizerc

var path = require('path')
var Config = require('config');
var env =Config.util.getEnv('NODE_ENV');
module.exports = {
  'config':          path.resolve('config', env + '.json')
}

development.json ie

{
    "app": {
        "name": "my api"
    },
    "server": {
        "port": 8081
    },
    "development": {
            "username": "username",
            "password": "password",
            "database": "database",
            "host": "127.0.0.1",
            "dialect": "mysql"
    }
} 

You can see i have to set a redundant env key with no logical meaning in all my env.json files.

is there a better way ?

drawback

To get the data:

var env =Config.util.getEnv('NODE_ENV');
var configDb = Config.get(env);

and this way all the options of File Load Order are lost.

https://github.com/lorenwest/node-config/wiki/Configuration-Files

Other way

sequelize db:migrate --url 'mysql://root:password@mysql_host.com/database_name'

with the standard node-config json files.

2
Do you think that question is readable? That it makes sense? - Amit
@Amit I've updated my question. - Whisher
so you want sequelize to be able to "compose" it's configuration from multiple source files, the same manner that node-config does? - Amit
@Amit could I found better words :) I've edited my question. Thanks - Whisher

2 Answers

4
votes

In your config folder for node-config, create a file called config.js

// config/config.js
const config = require('config');

module.exports = {
  [process.env.NODE_ENV || 'development']: config.database
};

Then create a .sequelizerc in the top level of the project.

// .sequelizerc
const path = require('path');

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

Example config/development.json

{
  "database": {
    "username": "root",
    "password": "",
    "database": "my_database",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

To use an env var, use custom-environment-variables.json as you would normally with node-config.

1
votes

If I understood your question correctly, you have to put .sequelizerc file at the root of your project with this content:

var config = require('config');

config.database.config = __filename;

module.exports = config.database;

This exports database section of your config, composed from config files by node-config as the sequelize-cli config.