Schema and Client Generation
Schema and Client Generation
schema.prisma is the single source of truth: a datasource block names the provider and URL, a generator emits the client, and models map to tables.
model User {
id Int @id @default(autoincrement())
email String @unique
posts Post[]
}
Running npx prisma generate regenerates typed classes in node_modules/.prisma/client — after any schema edit, stale types mean the generate step was skipped. prisma migrate dev creates a migration file, applies it locally, and regenerates the client; CI and production use prisma migrate deploy to replay committed migrations without prompting.
Queries and Transactions
Queries and Transactions
The client exposes CRUD plus relation filters; include loads relations in one typed call:
const users = await prisma.user.findMany({
where: { email: { contains: "@" } },
include: { posts: true },
});
await prisma.$transaction([
prisma.user.create({ data: { email: "a@b.c" } }),
prisma.post.create({ data: { title: "hi" } }),
]);
The array form batches statements in one transaction. The interactive form $transaction(async (tx) => {...}) runs arbitrary logic with a shared client and accepts an isolation level such as Serializable, which you need when reads and writes must not interleave.
Pitfalls and Interview Angles
Pitfalls and Interview Angles
N+1 reads. Looping over rows and calling findUnique per row issues one query each; use include or one where: { id: { in: ids } } query.
Generate is not automatic. CI that changes the schema but skips prisma generate compiles against old types — wire it into the build.
Types are not validation. Generated types describe what the database accepts; untrusted HTTP bodies still need runtime parsing (Zod or Valibot) before you trust them.
Interview: explain how Prisma translates the schema into a typed query engine client, compare migrate dev with migrate deploy, and say when the batch $transaction array is insufficient.
Quiz
1. Which command should CI run to apply committed migrations to a production database?
2. What does prisma generate actually produce?
3. When do you need the interactive $transaction(fn) form instead of the array form?
Flashcards
Question
schema.prisma
Click to reveal answer
Answer
Declarative source of truth: datasource, generator, and models mapped to tables.
Question
prisma generate
Click to reveal answer
Answer
Regenerates the typed client from the schema; no database access.
Question
migrate dev vs deploy
Click to reveal answer
Answer
dev creates/applies migrations locally with prompts; deploy replays committed migrations non-interactively.
Question
Batch vs interactive transaction
Click to reveal answer
Answer
Array form = fixed list of ops; callback form = arbitrary logic with one client and isolation options.
Revision Notes
Key Takeaways
- 1. Schema changes always require prisma generate or types go stale
- 2. Prefer include/in queries to avoid N+1 round trips
- 3. Prisma types describe storage shape; runtime input still needs parsing
Interview Tips
- • Walk the flow: schema → generate → typed client → query engine → SQL
- • Contrast migrate dev, migrate deploy, and db push, and say which belongs in CI
Cheat Sheet
schema.prisma → prisma generate (types) → migrate dev (local) / migrate deploy (CI-prod); include beats loops; parse untrusted input before create/update.
Continue Learning
Before You Start
- No prerequisites listed.