.cursorrules TypeScript Prisma Backend

Prisma + PostgreSQL

Prisma + PostgreSQLのcursorrules。スキーマ設計、マイグレーション、クエリ最適化、型安全なデータベースアクセスのガイド。

.cursorrules · 97 lines
You are an expert database engineer using Prisma ORM with PostgreSQL.

## Schema Design

```prisma
model User {
  id        String    @id @default(cuid())
  email     String    @unique
  name      String?
  posts     Post[]
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt

  @@index([email])
  @@map("users")       // snake_case table names
}

model Post {
  id        String   @id @default(cuid())
  title     String
  content   String?
  published Boolean  @default(false)
  authorId  String
  author    User     @relation(fields: [authorId], references: [id])

  @@index([authorId])
  @@index([published, createdAt(sort: Desc)])
}
```

## Query Patterns

```typescript
// Select with relations
const user = await prisma.user.findUnique({
  where: { id: userId },
  include: {
    posts: {
      where: { published: true },
      orderBy: { createdAt: 'desc' },
      take: 10,
    },
  },
});

// Pagination
const posts = await prisma.post.findMany({
  skip: (page - 1) * pageSize,
  take: pageSize,
  cursor: cursor ? { id: cursor } : undefined,
  orderBy: { createdAt: 'desc' },
});

// Transaction
await prisma.$transaction(async (tx) => {
  const user = await tx.user.create({ data: { email } });
  await tx.profile.create({ data: { userId: user.id } });
});
```

## Migration Workflow

```bash
# Development: auto-apply and regenerate client
npx prisma migrate dev --name add_user_index

# Production: generate SQL and apply manually
npx prisma migrate deploy

# Reset dev database
npx prisma migrate reset

# Inspect db schema
npx prisma db pull
```

## Performance

- Use `select` to fetch only needed fields.
- `findUnique` over `findFirst` when possible (uses index).
- Avoid N+1: use `include` or `findMany` with `in` filter.
- Raw queries via `prisma.$queryRaw` for complex aggregations.
- Connection pooling with PgBouncer or Prisma Data Proxy.

## Type Safety

```typescript
import { Prisma } from '@prisma/client';

// Input types
type CreateUserInput = Prisma.UserCreateInput;

// Return types
type UserWithPosts = Prisma.UserGetPayload<{
  include: { posts: true };
}>;
```
Share on X

こちらもおすすめ

Backend カテゴリの他のルール

もっとルールを探す

CLAUDE.md、.cursorrules、AGENTS.md、Image Prompts の全 223 ルールをチェック。