To use environment variables (stored in a .env file) in your Sequelize CLI configuration, you'll need to use a package like dotenv to load the variables and then access them in your configuration file.
npm install dotenv
Once dotenv is installed, add keys to the .env
file
DB_USERNAME=myusername
DB_PASSWORD=mypassword
DB_HOST=localhost
DB_NAME=mydatabase
To use .env in sequelize, you have to update .sequelizerc
. Please create one if you don't have one already. The important thing to notice here is the change in config.json to config.js
const path = require('path');
module.exports = {
'config': path.resolve('config', 'config.js'),
};
Update your config/config.js
with the following content. You can change the migrationStorageTableName
and seederStorageTableName
as per your linking.
const dotenv = (require("dotenv").config()).parsed
let config = {
username: dotenv.DB_USERNAME,
password: dotenv.DB_PASSWORD,
database: dotenv.DB_NAME,
host: dotenv.DB_HOST,
port: dotenv.DB_PORT,
dialect: dotenv.DB_DIALECT,
migrationStorageTableName: "sequelize_migrations",
seederStorageTableName: "sequelize_seeds"
};
module.exports = {
development: config,
test: config,
production: config
}
And that's it. You are good to go for the use.
Want to know more about Sequelize CLI, Click Here.