Reputation: 8499
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
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
Reputation: 5059
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,
},
},
};
.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'),
};
sequelize db:migrate
Upvotes: 27
Reputation: 546
I think the best way to accomplish this by using dotenv-cli.
.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')
}
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
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