prisma-orm
cohen-liel/hivemind/.claude/skills/prisma-orm/SKILL.md
Prisma ORM patterns for Node.js/TypeScript backends. Use when defining Prisma schemas, writing queries, handling relations, migrations, or any database work in a Node.js/TypeScript project.
Skill108 starsChanged 7 months ago
- Reads credentials
What's in it
- Prisma ORM Patterns
- Schema Design
- Client Setup
- Query Patterns
- Migrations
- Soft Delete Pattern
- Rules
---
name: prisma-orm
description: Prisma ORM patterns for Node.js/TypeScript backends. Use when defining Prisma schemas, writing queries, handling relations, migrations, or any database work in a Node.js/TypeScript project.
---
# Prisma ORM Patterns
## Schema Design
```prisma
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String
password String
role Role @default(USER)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime? // Soft delete
posts Post[]
sessions Session[]
@@index([email])
@@map("users") // Table name
}
enum Role {
USER
ADMIN
}
model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(255)
body String
published Boolean @default(false)
authorId Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
author User @relation(fields: [authorId], references: [id])
tags Tag[] @relation("PostTags")
@@index([authorId])
@@index([published, createdAt(sort: Desc)])
}
model Tag {
id Int @id @default(autoincrement())
name String @unique
posts Post[] @relation("PostTags")
}
```
## Client Setup
```typescript
// lib/db.ts
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
// Single instance pattern prevents connection exhaustion in dev (Next.js HMR)
```
## Query Patterns
```typescript
// Find with relations (avoid N+1)
const posts = await db.post.findMany({
where: { published: true, author: { deletedAt: null } },
include: { author: { select: { id: true, name: true } }, tags: true },
orderBy: { createdAt: 'desc' },
take: 20,
skip: (page - 1) * 20,
})
// Upsert
const setting = await db.setting.upsert({
where: { userId_key: { userId, key } },
create: { userId, key, value },
update: { value },
})
// Transaction
const [user, post] = await db.$transaction([
db.user.update({ where: { id: userId }, data: { postCount: { increment: 1 } } }),
db.post.create({ data: { title, body, authorId: userId } }),
])
// Interactive transaction (for complex logic)
const result = await db.$transaction(async (tx) => {
const user = await tx.user.findUniqueOrThrow({ where: { id: userId } })
if (user.balance < amount) throw new Error('Insufficient balance')
await tx.user.update({ where: { id: userId }, data: { balance: { decrement: amount } } })
return tx.payment.create({ data: { userId, amount } })
})
// Raw SQL for complex queries
const stats = await db.$queryRaw<{ date: Date; count: bigint }[]>`
SELECT date_trunc('day', created_at) as date, COUNT(*) as count
FROM posts
WHERE created_at > ${thirtyDaysAgo}
GROUP BY 1 ORDER BY 1
`
```
## Migrations
```bash
# Dev workflow
npx prisma migrate dev --name add_user_role # Create + apply migration
npx prisma migrate dev --create-only # Create only, don't apply
npx prisma db push # Push schema without migration (prototyping)
npx prisma studio # GUI to inspect DB
# Production
npx prisma migrate deploy # Apply pending migrations
npx prisma generate # Regenerate client after schema change
```
## Soft Delete Pattern
```typescript
// Middleware to filter soft-deleted records globally
db.$use(async (params, next) => {
if (params.model === 'User') {
if (params.action === 'findMany' || params.action === 'findFirst') {
params.args.where = { ...params.args.where, deletedAt: null }
}
if (params.action === 'delete') {
params.action = 'update'
params.args.data = { deletedAt: new Date() }
}
}
return next(params)
})
```
## Rules
- Always use `select` to fetch only needed fields (never fetch passwords)
- Use `include` for relations instead of multiple queries (prevent N+1)
- Transactions for multi-table writes (consistency guarantee)
- `findUniqueOrThrow` / `findFirstOrThrow` to get automatic 404 behavior
- Add `@@index` for all foreign keys and frequent filter columns
- Never use `db.raw` with user input — always use parameterized `$queryRaw`
- Single PrismaClient instance (global pattern above) for connection pooling
More agent context in cohen-liel/hivemind
75 other files this repository gives its agents, the first 60 shown.
CLAUDE.md
Skill
- algorithmic-art.claude/skills/algorithmic-art/SKILL.md
- api-design.claude/skills/api-design/SKILL.md
- apple-notes.claude/skills/apple-notes/SKILL.md
- apple-reminders.claude/skills/apple-reminders/SKILL.md
- article-writing.claude/skills/article-writing/SKILL.md
- async-python.claude/skills/async-python/SKILL.md
- brand-guidelines.claude/skills/brand-guidelines/SKILL.md
- camsnap.claude/skills/camsnap/SKILL.md
- canvas-design.claude/skills/canvas-design/SKILL.md
- celery-tasks.claude/skills/celery-tasks/SKILL.md
- claude-api.claude/skills/claude-api/SKILL.md
- coding-agent.claude/skills/coding-agent/SKILL.md
- content-engine.claude/skills/content-engine/SKILL.md
- diffs.claude/skills/diffs/SKILL.md
- doc-coauthoring.claude/skills/doc-coauthoring/SKILL.md
- docker-deployment.claude/skills/docker-deployment/SKILL.md
- docx.claude/skills/docx/SKILL.md
- e2e-testing.claude/skills/e2e-testing/SKILL.md
- email-service.claude/skills/email-service/SKILL.md
- fastapi-backend.claude/skills/fastapi-backend/SKILL.md
- frontend-design.claude/skills/frontend-design/SKILL.md
- frontend-slides.claude/skills/frontend-slides/SKILL.md
- gh-issues.claude/skills/gh-issues/SKILL.md
- github.claude/skills/github/SKILL.md
- git-workflow.claude/skills/git-workflow/SKILL.md
- graphql-api.claude/skills/graphql-api/SKILL.md
- healthcheck.claude/skills/healthcheck/SKILL.md
- internal-comms.claude/skills/internal-comms/SKILL.md
- investor-materials.claude/skills/investor-materials/SKILL.md
- jwt-authentication.claude/skills/jwt-authentication/SKILL.md
- market-research.claude/skills/market-research/SKILL.md
- mcp-builder.claude/skills/mcp-builder/SKILL.md
- mermaid-diagrams.claude/skills/mermaid-diagrams/SKILL.md
- microservices.claude/skills/microservices/SKILL.md
- mobile-react-native.claude/skills/mobile-react-native/SKILL.md
- model-usage.claude/skills/model-usage/SKILL.md
- nextjs-fullstack.claude/skills/nextjs-fullstack/SKILL.md
- nodejs-express.claude/skills/nodejs-express/SKILL.md
- obsidian.claude/skills/obsidian/SKILL.md
- openai-whisper.claude/skills/openai-whisper/SKILL.md
- oracle.claude/skills/oracle/SKILL.md
- pdf.claude/skills/pdf/SKILL.md
- peekaboo.claude/skills/peekaboo/SKILL.md
- planning-with-files.claude/skills/planning-with-files/SKILL.md
- postgres-database.claude/skills/postgres-database/SKILL.md
- pptx.claude/skills/pptx/SKILL.md
- prose.claude/skills/prose/SKILL.md
- pytest-patterns.claude/skills/pytest-patterns/SKILL.md
- react-typescript.claude/skills/react-typescript/SKILL.md
- redis-caching.claude/skills/redis-caching/SKILL.md
- s3-file-storage.claude/skills/s3-file-storage/SKILL.md
- security-review.claude/skills/security-review/SKILL.md
- session-logs.claude/skills/session-logs/SKILL.md
- skill-creator.claude/skills/skill-creator/SKILL.md
- slack-gif-creator.claude/skills/slack-gif-creator/SKILL.md
- sqlalchemy-orm.claude/skills/sqlalchemy-orm/SKILL.md
- state-management.claude/skills/state-management/SKILL.md
- strategic-compact.claude/skills/strategic-compact/SKILL.md
- stripe-payments.claude/skills/stripe-payments/SKILL.md
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
No reports yet. Be the first to say whether it worked.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool public_context_discussion, action report. How to connect one.

