How to reset and seed a Prisma database

The admin panel that you'll actually want to use. Try for free.

October 24, 2023

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.

  1. Navigate to the root directory of your Prisma project.
  2. 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:

  1. 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(); });
  1. 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 }
  1. 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.

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

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.

TOC

Resetting the Prisma database
Seeding the Prisma database
Validating the result
Conclusion

October 24, 2023

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.

  1. Navigate to the root directory of your Prisma project.
  2. 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:

  1. 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(); });
  1. 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 }
  1. 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.

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

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.

October 24, 2023

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.

  1. Navigate to the root directory of your Prisma project.
  2. 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:

  1. 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(); });
  1. 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 }
  1. 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.

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

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.

What is Basedash?

What is Basedash?

What is Basedash?

Basedash 🤝 Prisma

Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.

Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.

Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.

Dashboards and charts

Edit data, create records, oversee how your product is running without the need to build or manage custom software.

USER CRM

ADMIN PANEL

SQL COMPOSER WITH AI

Screenshot of a users table in a database. The interface is very data-dense with information.