Claude Code के साथ Mastering Prisma ORM की Complete Guide
Claude Code का उपयोग करके mastering prisma orm की comprehensive guide, practical examples और best practices के साथ।
Prisma ORM क्या है
Prisma ORMはTypeScript के लिएのअगला世代ORM है।型safeなdatabaseアクセス、直感的なスkeyマ定義、自動migrationを提供し है।Claude Code के साथ combineれば、database設計 सेimplementation तकefficiently進められ है।
スkeyマ設計
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
email String @unique
name String
avatar String?
posts Post[]
comments Comment[]
profile Profile?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([email])
}
model Profile {
id String @id @default(cuid())
bio String?
url String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String @unique
}
model Post {
id String @id @default(cuid())
title String
content String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
categories Category[]
comments Comment[]
publishedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([authorId])
@@index([published, publishedAt])
}
model Category {
id String @id @default(cuid())
name String @unique
slug String @unique
posts Post[]
}
model Comment {
id String @id @default(cuid())
content String
author User @relation(fields: [authorId], references: [id])
authorId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
postId String
createdAt DateTime @default(now())
@@index([postId])
}
CRUD操作
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
// create
async function createPost(data: {
title: string;
content: string;
authorId: string;
categoryIds: string[];
}) {
return prisma.post.create({
data: {
title: data.title,
content: data.content,
author: { connect: { id: data.authorId } },
categories: {
connect: data.categoryIds.map((id) => ({ id })),
},
},
include: {
author: { select: { id: true, name: true } },
categories: true,
},
});
}
// search(pageネーション付き)
async function getPosts(params: {
page?: number;
perPage?: number;
category?: string;
search?: string;
}) {
const { page = 1, perPage = 20, category, search } = params;
const where = {
published: true,
...(category && {
categories: { some: { slug: category } },
}),
...(search && {
OR: [
{ title: { contains: search, mode: "insensitive" as const } },
{ content: { contains: search, mode: "insensitive" as const } },
],
}),
};
const [posts, total] = await Promise.all([
prisma.post.findMany({
where,
skip: (page - 1) * perPage,
take: perPage,
orderBy: { publishedAt: "desc" },
include: {
author: { select: { id: true, name: true, avatar: true } },
categories: true,
_count: { select: { comments: true } },
},
}),
prisma.post.count({ where }),
]);
return {
posts,
pagination: {
total,
page,
perPage,
totalPages: Math.ceil(total / perPage),
},
};
}
トランザクション
async function publishPost(postId: string) {
return prisma.$transaction(async (tx) => {
const post = await tx.post.findUnique({
where: { id: postId },
});
if (!post) throw new Error("Post not found");
if (post.published) throw new Error("Already published");
const updated = await tx.post.update({
where: { id: postId },
data: {
published: true,
publishedAt: new Date(),
},
});
// 通知 create
await tx.notification.create({
data: {
type: "POST_PUBLISHED",
message: `「${post.title}」が公開されました`,
userId: post.authorId,
},
});
return updated;
});
}
migration
# スkeyマ変更बादのmigration
npx prisma migrate dev --name add_profile_table
# 本番環境へのdeploy
npx prisma migrate deploy
# スkeyマのsync(development用)
npx prisma db push
Claude Code सेのutilizationプロンプト
Prismaを使ったdevelopmentをClaude Code को requestする例 है।basic use करने का तरीकाはClaude Codeintroduction guide、他のORMとの比較はDrizzle ORMutilization法もदेखें。
Prismaでブlogのdatabaseスkeyマを設計して。
- user、記事、カテゴリ、コメントのモデル
- appropriateなリレーションとインデックス
- pageネーション付きのsearchquery
- トランザクションを使った公開processing
- migrationfileもgenerateして
Prismaके details के लिएPrismaofficial documentationをदेखें。Claude Codeのutilization法はofficial documentationでconfirmでき है।
Summary
Prisma ORMは型safeなdatabaseアクセスを提供し、development体験を大幅に向ऊपरさせ है।Claude Codeにスkeyマ設計 सेqueryoptimization तक依頼すれば、robustなdata層をefficientlybuild किया जा सकता है。
Related Posts
Non-Engineers के लिए Claude Code: AI-Powered Development का No-Code Approach
बिना किसी programming experience के Claude Code कैसे use करें, यह सीखें। Basic terminal commands से लेकर websites बनाने तक, यह beginner-friendly guide आपको step by step सब कुछ सिखाएगी।
Claude Code क्या है? Installation से पहले Use तक Complete Guide
Claude Code install और use करने की beginner-friendly guide। आज ही इस terminal-based AI coding assistant के साथ शुरुआत करें।
Claude Code Pricing Plans और ROI के बारे में कैसे सोचें
Claude Code की pricing structure का clear breakdown। Plans compare करें, cost optimization strategies सीखें, और concrete numbers के साथ real ROI समझें।