xiaohongshu
xiaohongshu
backend-prisma
Type-safe database ORM for TypeScript/Node.js. Use when you need database access with full TypeScript integration — auto-generated types from schema, migrations, and query builder. Best for PostgreSQL, MySQL, SQLite, MongoDB. Choose Prisma over raw SQL or Knex when type safety and developer exper...
Full skill instructions
Prisma is a next-generation ORM with auto-generated TypeScript types from your database schema. Define models in schema.prisma, run migrations, get fully typed queries.
Version: Prisma 6.x/7.x (2024-2025)
Prisma 7 Changes: ES module by default, prisma.config.ts, middleware deprecated
Key Benefit: Change schema → regenerate client → TypeScript errors show everywhere you need updates.
✅ Use Prisma when:
❌ Consider alternatives when:
$queryRaw)npm install prisma @prisma/client
npx prisma init
// prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_DATABASE_URL") // For migrations with pooler
}
generator client {
provider = "prisma-client-js"
}
model User {
id String @id @default(cuid())
email String @unique
name String?
role Role @default(USER)
posts Post[]
profile Profile?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([email])
}
model Post {
id String @id @default(cuid())
title String @db.VarChar(255)
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
createdAt DateTime @default(now())
@@index([authorId])
@@index([createdAt(sort: Desc)])
}
enum Role {
USER
ADMIN
}
Always use singleton in long-running servers:
// src/lib/prisma.ts
import { PrismaClient } from '@prisma/client';
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };
export const prisma = globalForPrisma.prisma || new PrismaClient({
log: process.env.NODE_ENV === 'development'
? ['query', 'error', 'warn']
: ['error'],
});
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
export default prisma;
// Create with relations
const user = await prisma.user.create({
data: {
email: '[email protected]',
posts: {
create: [{ title: 'First Post' }],
},
profile: {
create: { bio: 'Developer' },
},
},
include: { posts: true, profile: true },
});
// Find with filters
const posts = await prisma.post.findMany({
where: {
published: true,
author: { role: 'ADMIN' },
},
orderBy: { createdAt: 'desc' },
include: { author: { select: { name: true } } },
});
// Upsert
const user = await prisma.user.upsert({
where: { email: '[email protected]' },
update: { name: 'Updated Name' },
create: { email: '[email protected]', name: 'New User' },
});
// Delete
await prisma.user.delete({ where: { id: userId } });
const posts = await prisma.post.findMany({
take: 10,
cursor: lastId ? { id: lastId } : undefined,
orderBy: { createdAt: 'desc' },
skip: lastId ? 1 : 0, // Skip the cursor itself
});
// Interactive transaction
const transfer = await prisma.$transaction(async (tx) => {
const sender = await tx.account.update({
where: { id: senderId },
data: { balance: { decrement: amount } },
});
if (sender.balance < 0) throw new Error('Insufficient funds');
await tx.account.update({
where: { id: receiverId },
data: { balance: { increment: amount } },
});
return sender;
}, {
maxWait: 5000,
timeout: 10000,
});
// Include — return full related records
const user = await prisma.user.findUnique({
where: { id },
include: { posts: true }, // All post fields
});
// Select — pick specific fields (better performance)
const user = await prisma.user.findUnique({
where: { id },
select: {
name: true,
posts: { select: { title: true } }, // Only titles
},
});
# Development — create and apply migration
npx prisma migrate dev --name add_user_role
# Production — apply pending migrations
npx prisma migrate deploy
# Quick sync without history (prototyping only)
npx prisma db push
# Generate client after schema changes
npx prisma generate
# Reset database (dev only!)
npx prisma migrate reset
# View database in browser
npx prisma studio
import { Prisma } from '@prisma/client';
// Payload type with relations
type UserWithPosts = Prisma.UserGetPayload<{
include: { posts: true }
}>;
// Input types
type UserCreateInput = Prisma.UserCreateInput;
type UserUpdateInput = Prisma.UserUpdateInput;
// Where types
type UserWhereInput = Prisma.UserWhereInput;
// src/schemas/user.schema.ts
import { z } from 'zod';
export const UserSchema = z.object({
id: z.string().cuid(),
email: z.string().email(),
name: z.string().nullable(),
role: z.enum(['USER', 'ADMIN']),
createdAt: z.date(),
updatedAt: z.date(),
});
export const CreateUserSchema = UserSchema.omit({
id: true,
createdAt: true,
updatedAt: true,
}).extend({
password: z.string().min(8),
});
export type User = z.infer<typeof UserSchema>;
npm install -D zod-prisma-types
generator zod {
provider = "zod-prisma-types"
output = "../src/generated/zod"
}
include or select to avoid overfetchingdirectUrl for migrations with connection poolersprisma generate after schema changesdb push in productioninclude or relationLoadStrategy: 'join'$disconnect() in serverless (let runtime handle it)model Post {
id String @id @default(cuid())
deletedAt DateTime?
@@index([deletedAt])
}
// Always filter
const posts = await prisma.post.findMany({
where: { deletedAt: null },
});
model Comment {
id String @id @default(cuid())
content String
targetType String // "Post" | "Video"
targetId String
@@index([targetType, targetId])
}
"Types not updating":
→ Run: npx prisma generate
→ Restart TypeScript server
→ Check @prisma/client version matches prisma
"Migration failed":
→ Check DATABASE_URL is correct
→ For pooled connections, use directUrl for migrations
→ Try: npx prisma migrate reset (dev only!)
"Connection pool exhausted":
→ Use singleton pattern
→ Set connection_limit in DATABASE_URL
→ For serverless: npx prisma accelerate
"N+1 query performance":
→ Add include: { relation: true }
→ Use relationLoadStrategy: 'join' (Prisma 5+)
→ Use select instead of include
prisma/
├── schema.prisma # Database schema
├── migrations/ # Migration history
└── seed.ts # Seed script
src/lib/
└── prisma.ts # Singleton client
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience