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

Sentry image

Make it make sense

Only get the information you need to fix your code that’s broken with Sentry.

Start debugging →

Top comments (0)

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay