17

I am saving my database config in dotenv file.

I am using sequelize migration which has a config.json file in config folder:

{
 "development": {
    "username": "root",
    "password": null,
    "database": "test",
    "host": "127.0.0.1",
    "dialect": "postgres"
  },
  ....
}

Since I have configuration in dotenv do I have to convert it to js file:

require('dotenv').config({ silent: env === 'production'})

const devConfig = {
  dialect: 'postgres',
  host: process.env.DB_HOST || 'localhost',
  port: process.env.DB_PORT || 5432,
  database: process.env.DB_NAME || '',
  username: process.env.DB_USER || 'postgres',
  password: process.env.DB_PASSWORD || '',
  migrationStorageTableName: 'migrations'
};

module.exports = {
  development: devConfig,
  production: devConfig
};

but how can I run the the migration, which the config is not JSON?

node_modules/.bin/sequelize db:migrate --config config/config.js
Alvin
  • 8,219
  • 25
  • 96
  • 177

4 Answers4

27
  1. Rename config.json to config.js and call your environment variables inside.
module.exports = {
  development: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    dialect: 'postgres',
    logging: false,
  },
  test: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    dialect: 'postgres',
    logging: false,
  },
  production: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    dialect: 'postgres',
    logging: false,
    pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000,
    },
  },
};
  1. Create a .sequelizerc file with the following:
'use strict';

require('dotenv').config();    // don't forget to require dotenv
const path = require('path');

module.exports = {
  'config': path.resolve('config', 'config.js'),
  'models-path': path.resolve('models'),
  'seeders-path': path.resolve('seeders'),
  'migrations-path': path.resolve('migrations'),
};
  1. Run sequelize db:migrate
moeabdol
  • 4,779
  • 6
  • 44
  • 43
19

Sequelize does mention this in their docs. You can run migrations command by specifying the environment.

sequelize db:migrate --env production
Tharindu Thisarasinghe
  • 3,846
  • 8
  • 39
  • 70
  • thanks for the answer, just wanted to point out that it's not explicitly mentioned in the latest version of the docs, it's implied. It's not even available when you run `npx sequelize-cli --help` – Rukky Kofi Mar 24 '21 at 22:19
5

I think the best way to accomplish this by using dotenv-cli.

  1. Install dotenv-cli (locally or globally
  2. Make sure you've created a .sequelizerc file in the root of your application. It should look something like this:
const path = require('path')

module.exports = {
  config: path.resolve('config', 'config.js')
}
  1. Run the command as follows: dotenv -e path/to/.env sequelize db:migrate

dotenv-cli will populate the environment variables and then run the command, so your dynamic settings should all work with your existing configuration file

TheHanna
  • 496
  • 5
  • 9
  • 3
    A slight variation is to install dotenv-cli as a dev dependency. Then you can run `npx dotenv -e .env sequelize db:migrate` locally. – Mike Clymer May 16 '19 at 21:34
4

If you're using version 2.0 or later config/config.js is one of the defaults, so that shouldn't be a concern.

You can also create a .sequelizerc file that can override this path and/or name if you'd prefer. It's in the documentation under "Options".

tadman
  • 208,517
  • 23
  • 234
  • 262