Prisma Seed File Builder

Generate a Prisma seed.ts file with sample data for development

Builds a TypeScript Prisma seed file with upsert operations for each model, realistic sample records, a main() function, PrismaClient lifecycle, and try/catch error handling — wired for prisma db seed.

Why use upsert instead of create?

upsert is idempotent: running the seed twice updates existing rows instead of inserting duplicates or throwing a unique-constraint error. That makes the file safe to re-run during development.

Generate a re-runnable Prisma seed file

A good seed file gives every developer the same realistic data after a fresh migrate reset. Writing it by hand means repeating PrismaClient setup, looping to create rows, and wrapping everything so a failure does not leave a dangling connection. This builder produces a complete, idempotent seed.ts from a short description of your models.

How it works

For each model you list with its fields, the builder emits an upsert inside a loop. upsert is the key choice: it takes a where (a unique match), an update, and a create, so re-running the seed never duplicates rows or violates a unique constraint. The first field you list becomes the unique match key.

The generated file:

  • instantiates a single const prisma = new PrismaClient(),
  • defines an async function main() that loops the requested number of times per model, varying values ([email protected], [email protected], and so on) so each row is distinct,
  • calls main() then chains .catch() to log and process.exit(1), and .finally() to await prisma.$disconnect().

Field values are inferred from the field name and a small type hint — strings get readable text, numbers get an index-based integer, booleans alternate, and dates use new Date(). Relation fields are left as a clearly marked TODO because they require real foreign keys.

Tips and notes

  • Configure the seed command once: add "prisma": { "seed": "tsx prisma/seed.ts" } to package.json. Then npx prisma db seed and prisma migrate reset both run it.
  • The unique match field (first in your list) must be @id or @unique in schema.prisma, or the upsert will not behave idempotently.
  • Seed parent models before child models so foreign keys exist when you fill in the relation TODOs.
  • Keep the $disconnect() in finally — leaving the client open can hang the process or exhaust the connection pool in CI.