I would like to have different seed data between development and production. How can I specify this in the configuration? I'm aware that in the .sequelizerc
I can load a dynamic configuration file and specify a seeders-path
// .sequelizerc
const path = require('path')
module.exports = {
"config": path.resolve('./app/config', 'config.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/migrations')
"seeders-path": path.resolve('./app/seeders')
}
And the config
// ./app/config/config.json
{
"development": {
// environment specific configuration
...
Is it possible to choose the seeders-path
within the dynamic configuration? Or shall I make some logic like
"seeders-path": process.env.NODE_ENV === 'development'?
"seeders/development" :
"seeders/production"
You can use npm scripts something like this:
// package.json
"scripts": {
"sequelize:prod": "sequelize $* --seeders-path seeders/production",
"sequelize:dev": "sequelize $* --seeders-path seeders/development",
}
Or you can pass config directly like this:
// package.json
"scripts": {
"sequelize:dev": "sequelize $* --config some/path/dev.js"
"sequelize:prod": "sequelize $* --config some/path/prod.js"
}
Lists of options that u can pass to sequelize:
In sequelize-cli sources have this code link:
function loadRCFile(optionsPath) {
const rcFile = optionsPath || path.resolve(process.cwd(), '.sequelizerc');
const rcFileResolved = path.resolve(rcFile);
return fs.existsSync(rcFileResolved)
? JSON.parse(JSON.stringify(require(rcFileResolved)))
: {};
}
const args = yargs
.config(loadRCFile(yargs.argv.optionsPath));
So you can load different files like ".sequelizerc":
// package.json
"scripts": {
"sequelize:dev": "sequelize $* --options-path some/path/dev.js"
"sequelize:prod": "sequelize $* --options-path some/path/prod.js"
}
// some/path/dev.js
const path = require('path');
module.exports = {
"config": path.resolve('./app/config', 'config.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/migrations')
// here your development path to seeders
"seeders-path": path.resolve('./app/seeders/development')
}
// some/path/prod.js
const path = require('path');
module.exports = {
"config": path.resolve('./app/config', 'config.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/migrations')
// here your development path to seeders
"seeders-path": path.resolve('./app/seeders/production')
}
But u can create seeders for different ENV in one seeder directory.
Lets say, we have .sequelizerc
// .sequelizerc
const path = require('path')
module.exports = {
"config": path.resolve('./app/config', 'config.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/migrations')
"seeders-path": path.resolve('./app/seeders')
}
Create seeders:
sequelize seed:generate --name create_users-dev.js
sequelize seed:generate --name create_users-prod.js
sequelize seed:generate --name create_users-whatever.js
And we run only dev seeders:
sequelize db:seed --seed `basename $(ls app/seeders/*-dev.js)`
or prod seeders:
sequelize db:seed --seed `basename $(ls app/seeders/*-prod.js)`
or just users dev seeders
sequelize db:seed --seed `basename $(ls app/seeders/*users-dev.js)`
You can choose what seeder u want to run.
Format:
sequelize db:seed --seed <array>
sequelize db:seed --seed file1 file2 fileN
Here i used the below command to execute the specific file in the seeders folder of Sequelize.
Command:- sequelize db:seed --seed [seed file name]
like:-
Example:- sequelize db:seed --seed 20181204125705-executiveholds
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With