DEV Community

Cover image for Seed different data for the different environment with Sequelize.
Idris Kelani
Idris Kelani

Posted on

Seed different data for the different environment with Sequelize.

Sequelize is a promise-based Node.js ORM with rich features that make database connection and management simple and easy

Data migration and seeding are important to connect and manage the database and application information.

Sequelize seeding makes it easy to populate data in the database. This is needed especially for the smooth running of the application where some default information is needed for the successful operation of the application e.g. roles, currencies, user-types and others.

This Tutorial is born out of the challenges I faced at my workplace when trying to seed in some data for only the development and Quality assurance environment(for testing purposes only) without pushing the same data to the staging and production environment.

I am here to discuss how I dynamically loaded the data for different environments using the environment name (as defined in my .env file) as a sub-folder in the seeders folder.

Let's get started

I assume you already have a node-express-Sequelize setup with es6 syntax. If you haven’t, you can search tutorials online to do so.

Also, I believe that you have set up Sequelize with seeders in your project. If you have not, please go through this tutorial to do so.

It is expected that you already have a .sequelizerc file in your application.
The first step is to go into your seeders folder and create sub-folders for the different environments that you have.
Take a look at the sample structure of my seeders folder

Seeders folder structure

From the image above, I created the different sub-folder names in the seeders folder: using the names of the different environments that exist on the project as sub-folder names. i.e. development, QA, production and others. (Note that if you don't create the sub-folder for any environment, on the first sequelize seed run on that environment, the seeder sub-folder for that environment will be created).

Now edit your .sequelizerc file to this

const dotenv = require('dotenv');
const path = require('path');

module.exports = {
  config: path.resolve('./database/config', 'config.js'),
  'models-path': path.resolve('./database/models'),
  'seeders-path': path.resolve(`./database/seeders/${process.env.NODE_ENV}`),
  'migrations-path': path.resolve('./database/migrations')

Enter fullscreen mode Exit fullscreen mode

Firstly we imported and set dotenv module to make sure the the environment variables are loaded at the time sequelize is running the seeders files.

const dotenv = require('dotenv');

Enter fullscreen mode Exit fullscreen mode

Now lets focus on the seeders-path

'seeders-path': path.resolve(`./database/seeders/${process.env.NODE_ENV}`),
Enter fullscreen mode Exit fullscreen mode

Here we am pointing the location in which sequelize would pickup the files to the name of the environment.

sequelize db:seed:all

Enter fullscreen mode Exit fullscreen mode

Finally when we run the above code in different environment, only the data present for that environment files will be inserted. i.e if we run sequelize db:seed:all in development environment, sequelize would run seeder files present in the /database/seeders/development folder.

With this approach the different data can be seeded for different application environment.

Note(side effects)
When you create a squelize seed using the sequelize command
sequelize seed:create --name currency-types
Sequelize would create the seed file in the development environment only. So

So if you want to create the seed file for other environment…. There are two ways you can do that
1 Create the seed file in the development folder(like we did) and move/copy it to the seeders sub-folder or
2 change your NODE_ENV to the environment, run the create seed script.

By using this approach, it is very important to make sure that the right environment variable value is set in NODE_ENV as setting a wrong value especially when project is already live can lead to duplicated data

Lastly, this tutorial was written in 2021 and I decided to publish it in late 2023 when I couldn't find any tutorial online that specifically tackles this issue. This method did the job for me but if you find a better way to handle seeding data for different environments on sequelize please reach out to me or comment below

Thanks for reading this 🤗
Enter fullscreen mode Exit fullscreen mode

I am idris kelani 🤗

Top comments (0)