Seeding and Testing Your Database
A seed script fills a fresh database with starter data, so you (and your teammates) aren't testing against an empty database every time.
// prisma/seed.ts
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
await prisma.user.create({
data: {
email: 'ada@example.com',
name: 'Ada',
posts: { create: [{ title: 'My first post' }] },
},
});
}
main().finally(() => prisma.$disconnect());
Wire it up in package.json:
{
"prisma": {
"seed": "tsx prisma/seed.ts"
}
}
Then run it with:
npx prisma db seed
Resetting during development
npx prisma migrate reset
Drops your dev database, reapplies every migration from scratch, and reruns your seed script, an easy way to get back to a known, clean state while building.
TIP
This whole platform's own course catalog, including this lesson, is generated by exactly this kind of seed script.