Home › Prisma › Prisma ORM rules

Prisma ORM rules

Cursor rules for Prisma ORM 6 with PostgreSQL: schema changes, migrations, typed queries, and transactions.

CursorPrisma

How to use

Save this as .cursor/rules/prisma.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/prisma.mdcDownload  
---
description: Prisma ORM conventions
globs: ["prisma/**","src/**/*.ts"]
alwaysApply: false
---

# Prisma ORM

- The schema is `prisma/schema.prisma`. After editing it, run `npx prisma migrate dev --name <change>` (creates SQL + regenerates the client). Never edit an applied migration; add a new one.
- Production applies migrations with `npx prisma migrate deploy` only. Don't use `db push` outside prototypes.
- One shared `PrismaClient` instance (`src/lib/db.ts`), cached on `globalThis` in dev to survive hot reload.
- Select only needed fields with `select`; use `include` deliberately. Never send whole user rows (password hashes) to the client.
- Avoid N+1: load relations in one query with `include`/`select`, or batch with `findMany({ where: { id: { in: ids } } })`.
- Multi-step writes in `prisma.$transaction(async (tx) => {...})` using `tx`, not the global client.
- Paginate with `take` + `cursor` for large tables.
- Raw SQL only through tagged `$queryRaw` templates (parameterized); never `$queryRawUnsafe` with user input.
- Handle `Prisma.PrismaClientKnownRequestError` codes (`P2002` unique, `P2025` not found) and map them to domain errors.
- Add `@@index` for fields used in `where`/`orderBy` on large tables; name relations explicitly when a model has two relations to the same table.

## Don't
- Don't run `migrate reset` or anything that drops data without asking.

## Done when
`npx prisma validate`, `npx prisma migrate status`, typecheck and tests pass.

More: all Prisma rules · all Cursor rules

Related rules

Convex backend rules

Cursor rules for Convex backends: schema, validated queries/mutations/actions, indexes, and auth checks.

CursorConvex

Drizzle ORM rules

Cursor rules for Drizzle ORM with Postgres or SQLite: schema files, drizzle-kit migrations, and type-safe queries.

CursorDrizzle ORM

Firebase (modular SDK) rules

Cursor rules for Firebase web apps with the modular v11+ SDK, Firestore security rules, and emulator tests.

CursorFirebase

Mongoose (MongoDB ODM) rules

Cursor rules for Mongoose 8 with TypeScript: typed schemas, indexes, lean queries, validation, and transactions.

CursorMongoDB

Supabase backend rules

Cursor rules for apps on Supabase: Row Level Security first, migrations, typed clients and Edge Functions.

CursorSupabase

MongoDB Windsurf rules

Windsurf rules for MongoDB 8 apps with the Node.js driver: schema design, indexes, aggregation, and transactions.

WindsurfMongoDB

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.