Cli: JSON the right format for config?

Created on 27 Dec 2014  路  9Comments  路  Source: sequelize/cli

Is JSON the right format for the config? The only reason I ask is because I generally pull production config from ENV. You fine people have done a good job making this thing customizable so I wouldn't be surprised if you have something for this and I'm not seeing it.

Most helpful comment

For anyone who might be lost like me...

If you're using dotenv, you should require it in config.js.

const dotenv = require('dotenv').config();

module.exports = {
  development: {
    database: process.env.DB_NAME,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    dialect: 'mysql',
    define: {
      underscored: true
    },
  },
}

All 9 comments

Hi @anthonator,

there is this thing called use_env_variable... You can find it here: https://github.com/sequelize/cli/blob/ff719146589d28c198493e5b57b1aaa6a3cd0a73/lib/tasks/db.js#L191

If I get that right, and please let me know if that is the truth, because we should add that to the docs then, you can do this in your config.json:

{
  "production": {
    "use_env_variable": "DB_HOST"
  }
}

Having that you can set an env var DB_HOST to the URL of your database. Not entirely sure how that url looks like though. Please let me know if this helps already. Otherwise I will try to find out one is supposed to use that :D

Thanks for the reply @sdepold! I haven't had a chance to try your suggestion yet, but it seems like a rather round about solution when you can write native JavaScript to handle this scenario easily.

module.exports = {
  production: {
    database: process.env.PG_DATABASE,
    host: process.env.PG_HOST,
    dialect: 'postgres'
    ...snip...
  }
};

Is there any reason the above wouldn't work? Could simplify the config code (no JSON parsing!).

Yeah that would work as well, you only have to tell the CLI to use the js file as configuration file:

sequelize -c my_config.js

Perfect! Thanks @sdepold! This might be handy in the docs if it's not already there.

For anyone who might be lost like me...

If you're using dotenv, you should require it in config.js.

const dotenv = require('dotenv').config();

module.exports = {
  development: {
    database: process.env.DB_NAME,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    dialect: 'mysql',
    define: {
      underscored: true
    },
  },
}

For anyone wondering why sequelize -c my_config.js doesn't work, the syntax has been updated. Use this instead sequelize --config my_config.js

Is this still a problem or am I wrong? https://github.com/sequelize/cli/issues/688

There is no config.js in my installation. Is this to be manually written? I have an automatically created models/index.js when I ran sequelie init.

Current version of sequelize-cli supports config.JS by default. All you have to do is write your config.js as this:

require('dotenv').config();

module.exports = {
    dev: {
        username: process.env.DB_USER,
        password: process.env.DB_PASS,
        database: process.env.DB_NAME,
        host: process.env.DB_HOST,
        dialect: 'mysql'
    },
    "production": {
        "username": process.env.DB_USER,
        "password": process.env.DB_PASS,
        "database": process.env.DB_NAME,
        "host": process.env.DB_HOST,
        "dialect": "mysql"
    }
}

Note - your "dev" parameter name in config.js has to be exactly the same as in your .env file

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eharoldreyes picture eharoldreyes  路  3Comments

arndeash picture arndeash  路  3Comments

PavelPolyakov picture PavelPolyakov  路  5Comments

gotrevor picture gotrevor  路  5Comments

LucasBadico picture LucasBadico  路  5Comments