sequalize migration with dotenv
Asked Answered
C

4

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
Crustacean answered 21/8, 2017 at 6:44 Comment(0)
A
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".

Abirritant answered 21/8, 2017 at 6:48 Comment(0)
H
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
Hemmer answered 15/8, 2019 at 19:18 Comment(0)
J
19

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

sequelize db:migrate --env production
Justice answered 8/8, 2020 at 15:32 Comment(1)
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 --helpDaves
K
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

Klotz answered 18/3, 2019 at 21:39 Comment(1)
A slight variation is to install dotenv-cli as a dev dependency. Then you can run npx dotenv -e .env sequelize db:migrate locally.Anaanabaena
A
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".

Abirritant answered 21/8, 2017 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.