Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different directories for seed data using sequelize

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"
like image 908
Danny Sullivan Avatar asked Sep 21 '25 09:09

Danny Sullivan


2 Answers

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:

  1. --env string The environment to run the command in
  2. --config string The path to the config file
  3. --options-path string The path to a JSON file with additional options
  4. --migrations-pat string The path to the migrations folder
  5. --seeders-path string The path to the seeders folder
  6. --models-path string The path to the models folder
  7. --url string The database connection string to use. Alternative to using --config files
  8. --debug bolean When available show various debug information bolean

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
like image 118
maxim.u Avatar answered Sep 23 '25 00:09

maxim.u


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

like image 35
Shashikant Pandit Avatar answered Sep 22 '25 22:09

Shashikant Pandit