Prisma Interview Questions
Prisma Interview Questions Prisma vs raw SQL vs other ORMs? Prisma: type-safe, auto-generated client, great DX, schema-first. Drizzle: SQL-like API, smaller bun…
Prisma Interview Questions
Prisma vs raw SQL vs other ORMs? Prisma: type-safe, auto-generated client, great DX, schema-first. Drizzle: SQL-like API, smaller bundle, same type safety. TypeORM: decorator-based, more traditional ORM. Raw SQL: full control, most performant, no type safety by default
How does Prisma handle N+1? Eager loading with include or select nested relations. Prisma batches related queries. For heavy aggregations, use raw SQL via db.$queryRaw or add @relation to model
db push vs migrate dev? db push: direct schema sync, no migration files, for prototyping. migrate dev: creates versioned migration SQL files, for production. Always use migrate in production
How to run raw SQL? db.$queryRaw`SELECT ...` (returns typed rows), db.$executeRaw`UPDATE ...` (returns count). Use when Prisma query API isn't expressive enough
Singleton Prisma client pattern? In development, Next.js hot reload creates multiple PrismaClient instances, exhausting connection pool. Fix: store client on globalThis in dev, create new instance in prod
How to add middleware? db.$use((params, next) => { ... return next(params); }) — for logging, soft delete, multi-tenancy filters. Prisma 5+ uses client extensions instead
Singleton Pattern (Next.js)
// lib/db.ts — prevent multiple PrismaClient instances
import { PrismaClient } from '@prisma/client';
const globalForPrisma = global as unknown as { prisma: PrismaClient };
export const db = globalForPrisma.prisma ?? new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['query', 'error'] : ['error'],
});
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db;