P PasteCode
Regel

Claude Code Regeln für Prisma-Projekte

Claude Code Regeln für Prisma ORM-Projekte, die Migrationssicherheit, Abfragemuster, Relationsladen und die Verhinderung von Schema-Korruption durch Agenten abdecken.

Claude CodeCursorCodex Next.jsPostgreSQLTypeScript
.md .json Aktualisiert 8. Juni 2026

Fügen Sie dies als CLAUDE.md-Datei im Stammverzeichnis Ihres Repositorys hinzu. Claude Code liest CLAUDE.md automatisch (zusätzlich zu AGENTS.md) und wendet es als projektspezifische Anweisungen für jede Sitzung an.

Claude Code-Regeldatei

Claude Code Rules
# Prisma Project Rules
## Schema rules — highest risk area
- NEVER edit `prisma/schema.prisma` without explicitly confirming the change with the
developer first. Schema changes generate migrations that alter production data.
- When asked to add a field, propose the schema diff and the migration name, then
STOP and wait for approval before running `prisma migrate dev`.
- New required fields MUST have a `@default(...)` value or be added as optional
(`String?`). Adding a required field without a default breaks `migrate deploy` on
a non-empty database.
- Do NOT add `@@map` or `@map` attributes to rename existing tables or columns without
explicitly discussing the migration strategy — renames require data migration steps.
- Use `@db.Text` for long strings, `@db.VarChar(n)` for bounded strings. Never use
bare `String` for fields that will store user-generated content longer than 255 chars.
## Migration rules
- Run `prisma migrate dev --name <descriptive-name>` to generate migrations locally.
Never hand-edit files in `prisma/migrations/` — they are append-only.
- In CI/CD, run `prisma migrate deploy` (not `migrate dev`). Never run `migrate dev`
against a production database.
- After adding or changing models, always regenerate the client: `prisma generate`.
Never commit code that imports from `@prisma/client` without a matching `generate` run.
## Query patterns
- Import the Prisma client from `src/lib/prisma.ts` (the singleton). Never instantiate
`new PrismaClient()` in application code — this leaks connections in serverless.
- Use `select` or `include` explicitly on every query. Never rely on Prisma's default
select-all behaviour — it causes over-fetching and leaks sensitive fields.
- Paginate all list queries with `take` and `skip` (or cursor-based pagination with
`cursor` + `take`). Never run a `findMany` without a limit on a user-facing endpoint.
- Wrap multi-step mutations in `prisma.$transaction([...])` or the interactive
transaction callback. Never perform two sequential writes that must be atomic outside
a transaction.
- Use `prisma.$queryRaw` and `Prisma.sql` template tag for raw SQL only. Never
concatenate user input into a raw query string — this is a SQL injection vector.
## Relation loading
- Prefer `include` over N+1 patterns. If a resolver loads a relation in a loop,
refactor to a single query with `include` or use a DataLoader.
- Do not `include` deeply nested relations more than 2 levels deep in a single query.
Flatten with multiple targeted queries and join in application code if needed.
## Type safety
- All repository functions must return typed Prisma result types or mapped domain types.
Never return `any` or plain `object` from a database function.
- Use `Prisma.validator()` to define reusable select/include shapes that keep return
types precise.
## Definition of done
- `prisma validate` passes.
- `tsc --noEmit` passes.
- No `new PrismaClient()` outside `src/lib/prisma.ts`.
- No `findMany` calls without `take`.
- Migration files are committed alongside schema changes.

Warum diese Regeln

  • Niemals das Schema ohne Bestätigung bearbeiten ist nicht verhandelbar. Prisma-Migrationen sind in der Produktion irreversibel – eine gelöschte Spalte oder eine umbenannte Tabelle ohne Datenmigrationsplan führt zu Datenverlust. Agenten, die “hilfreich” sind, werden freudig migrate dev ausführen, sobald sie das Schema berühren.
  • Singleton-Client-Import verhindert eine der häufigsten Serverless-Fallen: Erschöpfung des Verbindungspools. In Next.js serverlosen Funktionen erzeugt jede Modulevaluierung eine neue PrismaClient-Instanz (und einen neuen Verbindungspool), sofern das Singleton-Muster nicht verwendet wird. Agenten, die Beispiele aus der Prisma-Dokumentation kopieren, überspringen dies oft.

Geeignet für

  • Next.js- oder Node.js-Apps, die Prisma mit PostgreSQL verwenden, bei denen das Schema ausgereift ist und Migrationssicherheit entscheidend ist.

Nicht geeignet für

  • Greenfield-Projekte ohne vorhandene Daten, bei denen der Agent frei am Schema iterieren soll – lockern Sie in dieser Phase die Anforderung der Migrationsgenehmigung.