Seed different data for the different environment with Sequelize.
Tags: sequelize, node, database, webdev
Published: 2023-09-07 21:17:58



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.

<u>Let's get started</u>

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](https://dev.to/idmega2000/seeding-data-with-sequelize-1f3o) to do so.

**_Setup_**
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t3r775u3eic68iwbmpu.png)


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


```javascript

const dotenv = require('dotenv');
dotenv.config();
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')
}; 

```

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.

```javascript

const dotenv = require('dotenv');
dotenv.config();

```

Now lets focus on the seeders-path
```javascript
'seeders-path': path.resolve(`./database/seeders/${process.env.NODE_ENV}`),
```

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


```

sequelize db:seed:all

```

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.

<u>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</u>


_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_

```javascript
Thanks for reading this 🤗
```

I am [idris kelani](https://github.com/idmega2000) 🤗