How to reset and seed a Prisma database
Prisma is an open-source database toolkit that simplifies database workflows, making them more efficient and type-safe.
One of the common tasks during development is to reset your database and seed it with initial data, especially when you're setting up the development environment or testing your application. In this post, we'll walk you through how to do this with Prisma.
Resetting the Prisma database
When you want to revert your database to its initial state or apply new changes to the Prisma schema, you might need to reset it. Here's how:
⚠️ Resetting your database will delete all data and drop the schema in your database.
- Navigate to the root directory of your Prisma project.
- Run the following command to reset the database:
npx prisma migrate reset
This will:
- Drop the database
- Create a new database
- Apply all migrations
- Generate the Prisma client
Seeding the Prisma database
Seeding allows you to populate your database with predefined data. Here's how to set up and run seeding with Prisma:
- Create a seed script
Create a new file named seed.ts
(for TypeScript) or seed.js
(for JavaScript) inside the prisma
folder.
This file should export a function named seed
that will handle the seeding logic. Here's an example using TypeScript:
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function seed() { await prisma.user.create({ data: { name: 'John Doe', email: 'john.doe@example.com', }, }); } seed() .catch((error) => { console.error(error); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });
- Update Prisma schema
In your schema.prisma
file, you'll need to tell Prisma about your seed script. Modify the generator
block to include a seed
field:
generator client { provider = "prisma-client-js" seed = "prisma/seed.ts" // or seed.js if you're using JavaScript }
- Run the seed script
With everything in place, you can now seed your database with the following command:
npx prisma db seed
Prisma will execute the seed script, and your database will be populated with the data you've defined.
Validating the result
You can validate that the database was reset and seeded properly by using a database tool like Basedash. You should validate the schema, check the _prisma_migrations
table to ensure that all migrations succeeded as expected, and view the data in some of the tables that were seeded.
Conclusion
With Prisma's streamlined CLI and the seeding setup, you can make sure your development environment is always in a consistent state. Remember to exercise caution when resetting databases, especially in production environments, to avoid any unintended data loss.
Invite only
We're building the next generation of data visualization.
How to automate Prisma migrations in a CI/CD pipeline
Max Musing
How to implement soft deletes in Prisma
Max Musing
How to use the shadow database in Prisma
Kris Lachance
UUID vs GUID vs CUID vs NanoID: A guide to database primary keys
Max Musing
How to generate UUIDs in Prisma
Max Musing
How to squash migrations in Prisma
Max Musing