nodejs sequelize/cli how to use with node-config
Asked Answered
E

3

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.

Educable answered 19/7, 2015 at 9:59 Comment(4)
Do you think that question is readable? That it makes sense?Oyster
@Oyster I've updated my question.Educable
so you want sequelize to be able to "compose" it's configuration from multiple source files, the same manner that node-config does?Oyster
@Oyster could I found better words :) I've edited my question. ThanksEducable
G
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.

Glottology answered 11/12, 2018 at 23:39 Comment(3)
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 onBullough
@Bullough 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)Glottology
I solved the problem by adding dotenv on the beginning of my config.js file ThanksBullough
H
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.

Hamsun answered 2/9, 2015 at 20:42 Comment(0)
H
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')},
};
Handicraft answered 11/11, 2022 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.