6

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.

4
  • Do you think that question is readable? That it makes sense? Commented Jul 19, 2015 at 10:06
  • @Amit I've updated my question. Commented Jul 19, 2015 at 10:22
  • so you want sequelize to be able to "compose" it's configuration from multiple source files, the same manner that node-config does? Commented Jul 19, 2015 at 10:32
  • @Amit could I found better words :) I've edited my question. Thanks Commented Jul 19, 2015 at 10:35

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 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)
I solved the problem by adding dotenv on the beginning of my config.js file Thanks
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.

Comments

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')},
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.