Search code examples
jsonnode.jssequelize.jssequelize-cli

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

Solution

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