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

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)