Alvin
Alvin

Reputation: 8499

sequalize migration with dotenv

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

Upvotes: 17

Views: 17115

Answers (4)

Tharindu Thisarasinghe
Tharindu Thisarasinghe

Reputation: 3998

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

sequelize db:migrate --env production

Upvotes: 19

moeabdol
moeabdol

Reputation: 5059

  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

Upvotes: 27

TheHanna
TheHanna

Reputation: 546

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

Upvotes: 5

tadman
tadman

Reputation: 211680

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".

Upvotes: 4

Related Questions