Prisma in Production: Patterns and Pitfalls After a Dozen Projects
Prisma is the best ORM for TypeScript applications. After using it across a dozen projects, here are the patterns that make it genuinely excellent -- and the pitfalls that catch developers the firs...

Source: DEV Community
Prisma is the best ORM for TypeScript applications. After using it across a dozen projects, here are the patterns that make it genuinely excellent -- and the pitfalls that catch developers the first time. Setup npm install prisma @prisma/client npx prisma init This creates prisma/schema.prisma and a .env file with DATABASE_URL. // prisma/schema.prisma generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } The Singleton Client Pattern In Next.js serverless functions, you can accidentally create thousands of database connections. Use a global singleton: // src/lib/db.ts import { PrismaClient } from "@prisma/client" const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined } export const db = globalForPrisma.prisma ?? new PrismaClient({ log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"], }) if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = db Import