5

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.

llnathanll
  • 80
  • 6
Whisher
  • 31,320
  • 32
  • 120
  • 201

3 Answers3

4

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.

type
  • 366
  • 3
  • 8
  • I'm using custom-environment-variables.json files, but the properties defined there is not visible in config.js.. Can you please help me find out what is going on – Liana Jun 19 '20 at 14:20
  • @Liana can you show me what the project structure looks like? The directory structure you should have is like `./config/` `./config/custom-environment-variables.json` `./config/config.js` (sequelize magic, use the code in my answer) `./.sequelizerc` (don't forget the ., use the code I put in my answer) – type Jun 20 '20 at 01:14
  • I solved the problem by adding dotenv on the beginning of my config.js file Thanks – Liana Jun 21 '20 at 16:11
2

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.

Max
  • 623
  • 6
  • 8
1

There are mistakes in config.js in answer by @type.

  1. Must use getter instead of .database
  2. node-config doesn't allow mutation of config (sequelize try to do) Solution is either using ALLOW_CONFIG_MUTATIONS=true or spread object.
// config/config.js
const config = require('config');

module.exports = {
  [process.env.NODE_ENV || 'development']: {...config.get('database')},
};
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
doorsfan
  • 36
  • 2