DEV Community

Cover image for Nest JS for Beginners #4:Adding seeders and migrations to the NestJS project with Sequelize
Mithun
Mithun

Posted on • Edited on

Nest JS for Beginners #4:Adding seeders and migrations to the NestJS project with Sequelize

1. Install Sequelize CLI:

npm install --save-dev sequelize-cli

2. Create .sequelizerc File:

Create a file named .sequelizerc in the root of your project:

Image description

3. Generate a Migration:

Run the following command to generate a migration:

npx sequelize-cli migration:generate --name create-books

This will create a migration file in the src/database/migrations directory. Open the generated migration file (XXXXXXXXXXXXXX-create-books.js) and define your schema changes in the up and down functions.

Image description

4. Run the Migration:

Execute the following command to apply the changes defined in your migration file to the database:

npx sequelize-cli db:migrate

Image description

5. Generate a Seeder:

Run the following command to generate a seeder:

npx sequelize-cli seed:generate --name demo-books
This will create a seeder file in the src/database/seeders directory. Open the generated seeder file (XXXXXXXXXXXXXX-demo-books.js) and define the data you want to seed in the up function.

Image description

6. Run Seeders:

Execute the following command to run the seeders and populate your database with initial data:

npx sequelize-cli db:seed:all

Image description

Image description

Top comments (0)