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.

Related rules

Astro content site rules

Cursor rules for Astro 5 sites with content collections, islands, and zero-JS-by-default pages.

CursorAstro

Chrome extension (Manifest V3) rules

Cursor rules for building Manifest V3 browser extensions with TypeScript and minimal permissions.

CursorChrome extensions

Cloudflare Agents SDK rules

Cursor rules for stateful AI agents on Cloudflare (agents package): Durable Object state, SQL, scheduling, and WebSockets.

CursorCloudflare Workers / Hono

Convex backend rules

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

CursorConvex

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.