P
Prismaā€¢5d ago
vpjm

Seeding database with pnpm

I followed this tutorial and I'm using SQLite, but now when I run my Next.js app (pnpm run dev), a dev.db file is created, but it's not seeded. I assume it's normal for a new database to be created for each app, but how can I make sure it's seeded ?
2 Replies
Prisma AI Help
Prisma AI Helpā€¢5d ago
You opted for speed from this bot. Let's conjure up an answer right now! Based on the information provided in the knowledge sources, I can offer some guidance on seeding your database when using Prisma with pnpm and Next.js. However, it's important to note that the specific tutorial you mentioned is not directly referenced in the provided sources. To ensure your database is seeded when using pnpm, you should follow these steps: 1. First, make sure you have a seed script set up in your prisma/seed.ts (or seed.js) file. This script should contain the data you want to seed into your database. 2. In your package.json file, add the prisma.seed configuration to specify how to run your seed script. For TypeScript, it should look like this:
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
}

"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
}

For JavaScript, it would be:
"prisma": {
"seed": "node prisma/seed.js"
}

"prisma": {
"seed": "node prisma/seed.js"
}

3. To manually seed your database, you can run the following command:
pnpm prisma db seed

pnpm prisma db seed

This command should execute your seed script and populate your database with the initial data. However, it's important to note that running pnpm run dev for your Next.js app doesn't automatically trigger the seeding process. The seeding is typically done manually or as part of your deployment process. If you want to ensure that your database is seeded every time you start your development environment, you could modify your dev script in package.json to include the seeding step:
"scripts": {
"dev": "pnpm prisma db seed && next dev"
}
"scripts": {
"dev": "pnpm prisma db seed && next dev"
}
This way, every time you run pnpm run dev, it will first seed the database before starting the Next.js development server. Keep in mind that repeatedly seeding the database might lead to duplicate entries if your seed script doesn't handle this scenario. You might want to use upsert operations in your seed script to avoid this issue. For more detailed information on seeding with Prisma, you can refer to t he Prisma documentation on seeding. If you're still encountering issues, it might be helpful to check the specific error messages or logs you're getting when trying to seed the database. šŸ¤” Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Nurul
Nurulā€¢4d ago
Hey šŸ‘‹ From the guide you are following, by default it will not have the data, you can add it manually via Prisma Studio: https://www.prisma.io/docs/guides/use-prisma-in-pnpm-worspaces#35-optional-add-data-to-your-database-using-prisma-studio You can also seed the database by following the instructions which bot gave ā˜ļø Let us know if you face any issues
How to use Prisma ORM in a pnpm workspaces monorepo | Prisma Docume...
Learn step-by-step how to integrate Prisma ORM in a pnpm workspaces monorepo to build scalable and modular applications efficiently.

Did you find this page helpful?