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

1

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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay