Cursor 中使用 Prisma 的最佳实践
Cursor 是一款 AI 驱动的代码编辑器,旨在通过自动化重复性编码任务来提高生产力。当与 Prisma(一个用于数据库工作流程的强大且类型安全的工具包)结合使用时,它将成为管理和优化数据库 schema、查询和数据填充的强大解决方案。
本指南提供了有关如何有效结合使用 Prisma 和 Cursor 的详细说明,以实现以下目标:
- 使用
.cursorrules
定义项目特定的最佳实践。 - 使用 Cursor 的上下文感知能力。
- 生成针对您的数据库量身定制的 schema、查询和种子数据。
虽然本指南侧重于 Cursor,但这些模式应该适用于任何 AI 编辑器。如果您希望我们为您首选的工具创建指南,请在 X 上告诉我们!
使用 .cursorrules
定义项目特定规则
Cursor 中的 .cursorrules
文件 允许您实施针对您的 Prisma 项目量身定制的最佳实践和开发标准。通过定义清晰且一致的规则,您可以确保 Cursor 生成干净、可维护且项目特定的代码,并最大限度地减少手动调整。
要实施这些规则,请在项目的根目录中创建一个 .cursorrules
文件。以下是一个示例配置
.cursorrules
文件示例
.cursorrules
文件示例You are a senior TypeScript/JavaScript programmer with expertise in Prisma, clean code principles, and modern backend development.
Generate code, corrections, and refactorings that comply with the following guidelines:
TypeScript General Guidelines
Basic Principles
- Use English for all code and documentation.
- Always declare explicit types for variables and functions.
- Avoid using "any".
- Create precise, descriptive types.
- Use JSDoc to document public classes and methods.
- Maintain a single export per file.
- Write self-documenting, intention-revealing code.
Nomenclature
- Use PascalCase for classes and interfaces.
- Use camelCase for variables, functions, methods.
- Use kebab-case for file and directory names.
- Use UPPERCASE for environment variables and constants.
- Start function names with a verb.
- Use verb-based names for boolean variables:
- isLoading, hasError, canDelete
- Use complete words, avoiding unnecessary abbreviations.
- Exceptions: standard abbreviations like API, URL
- Accepted short forms:
- i, j for loop indices
- err for errors
- ctx for contexts
Functions
- Write concise, single-purpose functions.
- Aim for less than 20 lines of code.
- Name functions descriptively with a verb.
- Minimize function complexity:
- Use early returns.
- Extract complex logic to utility functions.
- Leverage functional programming techniques:
- Prefer map, filter, reduce.
- Use arrow functions for simple operations.
- Use named functions for complex logic.
- Use object parameters for multiple arguments.
- Maintain a single level of abstraction.
Data Handling
- Encapsulate data in composite types.
- Prefer immutability.
- Use readonly for unchanging data.
- Use as const for literal values.
- Validate data at the boundaries.
Error Handling
- Use specific, descriptive error types.
- Provide context in error messages.
- Use global error handling where appropriate.
- Log errors with sufficient context.
Prisma-Specific Guidelines
Schema Design
- Use meaningful, domain-driven model names.
- Leverage Prisma schema features:
- Use @id for primary keys.
- Use @unique for natural unique identifiers.
- Utilize @relation for explicit relationship definitions.
- Keep schemas normalized and DRY.
- Use meaningful field names and types.
- Implement soft delete with deletedAt timestamp.
- Use Prisma's native type decorators.
Prisma Client Usage
- Always use type-safe Prisma client operations.
- Prefer transactions for complex, multi-step operations.
- Use Prisma middleware for cross-cutting concerns:
- Logging
- Soft delete
- Auditing
- Handle optional relations explicitly.
- Use Prisma's filtering and pagination capabilities.
Database Migrations
- Create migrations for schema changes.
- Use descriptive migration names.
- Review migrations before applying.
- Never modify existing migrations.
- Keep migrations idempotent.
Error Handling with Prisma
- Catch and handle Prisma-specific errors:
- PrismaClientKnownRequestError
- PrismaClientUnknownRequestError
- PrismaClientValidationError
- Provide user-friendly error messages.
- Log detailed error information for debugging.
Testing Prisma Code
- Use in-memory database for unit tests.
- Mock Prisma client for isolated testing.
- Test different scenarios:
- Successful operations
- Error cases
- Edge conditions
- Use factory methods for test data generation.
- Implement integration tests with actual database.
Performance Considerations
- Use select and include judiciously.
- Avoid N+1 query problems.
- Use findMany with take and skip for pagination.
- Leverage Prisma's distinct for unique results.
- Profile and optimize database queries.
Security Best Practices
- Never expose raw Prisma client in APIs.
- Use input validation before database operations.
- Implement row-level security.
- Sanitize and validate all user inputs.
- Use Prisma's built-in protections against SQL injection.
Coding Style
- Keep Prisma-related code in dedicated repositories/modules.
- Separate data access logic from business logic.
- Create repository patterns for complex queries.
- Use dependency injection for Prisma services.
Code Quality
- Follow SOLID principles.
- Prefer composition over inheritance.
- Write clean, readable, and maintainable code.
- Continuously refactor and improve code structure.
Development Workflow
- Use version control (Git).
- Implement comprehensive test coverage.
- Use continuous integration.
- Perform regular code reviews.
- Keep dependencies up to date.
此文件确保一致且可维护的代码生成,减少手动干预,同时提高项目质量。
使用 Cursor 的上下文感知能力
Cursor 的 上下文感知 能力使您可以添加特定的网站、文件、文件夹或文档,以增强其对您项目的理解。通过将您的 schema.prisma
文件添加为上下文,您可以使 Cursor 能够根据您的数据库 schema 生成更准确的查询、测试和种子数据。
添加 Prisma 文档 llm.txt
文件作为 @Docs
上下文
为了提高 Cursor 对您项目中 Prisma 相关建议的理解,请包含 /llms.txt
markdown 文件作为上下文。该文件提供了简洁的概述、有用的指导以及指向详细 Prisma 文档的链接,所有这些都针对 LLM 处理进行了优化。只需导航到 url 并在您的 Cursor 配置中将其添加为 @Docs
资源。
添加其他 Prisma 文档
Cursor 已经包含了来自 Prisma 文档的内置上下文,因此您无需添加任何内容即可使用我们的文档!您可以在此处查看 Cursor 使用的现有资源。
为了保持与最新更改同步或整合其他上下文,请将这些资源添加为 @Docs
上下文
- Prisma 更新日志,了解更新和新功能。
- Prisma 博客,获取实用指南和最佳实践。
要了解如何添加和管理文档上下文,请访问 Cursor 文档。
使用 schema 作为上下文
在使用请求查询、测试或种子脚本之前,使用 @Files
语法将您的 schema.prisma
文件(或整个代码库)添加为上下文。这使 Cursor 能够生成精确的、schema 驱动的输出,例如查询、测试和种子数据。例如,在 Cursor Chat 中,您可以通过键入 @Files
,然后添加您的 schema.prisma
文件来包含您的 schema。
生成 Prisma Schema
Cursor 可以从高级描述生成 Prisma schema,使您能够快速为您的数据库 schema 建立坚实的基础。通过提供清晰而详细的提示,Cursor 可以创建根据您的需求量身定制的 Prisma schema。无论您需要通用的基础 schema 还是针对详细用例的高度特定的 schema,Cursor 都可以精确地交付。这是一个示例提示及其对应的结果
如果您运行以下提示,您可能会收到不同的输出,因为 LLM 会产生非确定性响应。
- 生成 Prisma schema 的提示
- 结果
"Create a Prisma schema for a SaaS app using PostgreSQL as a provider with `User`, `Organization`, and `Subscription` models, ensuring all models include `createdAt` and `updatedAt` DateTime fields with defaults, a soft-delete `deletedAt` field, and proper relationships between entities."
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
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
organization Organization @relation(fields: [organizationId], references: [id])
organizationId Int
role UserRole @default(MEMBER)
}
model Organization {
id Int @id @default(autoincrement())
name String
slug String @unique
subscription Subscription?
users User[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
}
model Subscription {
id Int @id @default(autoincrement())
organization Organization @relation(fields: [organizationId], references: [id])
organizationId Int @unique
plan SubscriptionPlan
status SubscriptionStatus @default(ACTIVE)
startDate DateTime
endDate DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
}
enum UserRole {
ADMIN
MEMBER
}
enum SubscriptionPlan {
FREE
BASIC
PREMIUM
ENTERPRISE
}
enum SubscriptionStatus {
ACTIVE
CANCELED
EXPIRED
PAST_DUE
}
生成 Prisma 查询
无论您是需要基本数据检索还是复杂的优化操作,Cursor 都可以帮助您创建针对您的 Prisma schema 量身定制的查询。为了获得最佳结果,请专注于编写清晰、具体的提示,以定义您的需求,例如要包含的字段、要应用的条件以及要遍历的关系。这确保 Cursor 生成准确、高性能且类型安全的查询。这是一个示例提示及其生成的查询
- 生成数据库查询的提示
- 结果
"Generate a query to retrieve all User records associated with an Organization that has an `active` Subscription, while excluding any records where the deletedAt field is set."
const activeUsers = await prisma.user.findMany({
where: {
deletedAt: null,
organization: {
deletedAt: null,
subscription: {
deletedAt: null,
status: 'ACTIVE'
}
}
},
include: {
organization: {
include: {
subscription: true
}
}
},
orderBy: {
createdAt: 'desc'
},
skip: 0,
take: 10
});
使用 Cursor 创建种子文件
编写种子脚本可能是一个繁琐的过程,但 Cursor 通过快速生成结构化且有组织的种子代码来简化此过程。通过提供清晰的说明,您可以指导 Cursor 创建脚本,以使用真实数据填充您的数据库,包括处理您的 schema 中定义的关系和复杂结构。这是一个示例提示及其生成的种子脚本
- 填充数据库的提示
- 结果
"Generate code to populate the Prisma schema with realistic data for the User, Organization, and Subscription models, ensuring all necessary fields are included."
import {
PrismaClient,
UserRole,
SubscriptionPlan,
SubscriptionStatus,
} from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
// Clear existing data
await prisma.user.deleteMany();
await prisma.subscription.deleteMany();
await prisma.organization.deleteMany();
const organizations = [
{
name: "Tech Innovators",
slug: "tech-innovators",
subscription: {
plan: SubscriptionPlan.ENTERPRISE,
status: SubscriptionStatus.ACTIVE,
startDate: new Date("2024-01-01"),
},
users: [
{
name: "Emma Thompson",
email: "[email protected]",
role: UserRole.ADMIN,
password: "password123",
},
{
name: "Michael Chen",
email: "[email protected]",
role: UserRole.MEMBER,
password: "password123",
},
],
},
{
name: "Digital Solutions",
slug: "digital-solutions",
subscription: {
plan: SubscriptionPlan.PREMIUM,
status: SubscriptionStatus.ACTIVE,
startDate: new Date("2024-01-15"),
},
users: [
{
name: "Sarah Wilson",
email: "[email protected]",
role: UserRole.ADMIN,
password: "password123",
},
{
name: "James Miller",
email: "[email protected]",
role: UserRole.MEMBER,
password: "password123",
},
],
},
{
name: "Cloud Systems",
slug: "cloud-systems",
subscription: {
plan: SubscriptionPlan.BASIC,
status: SubscriptionStatus.ACTIVE,
startDate: new Date("2024-02-01"),
},
users: [
{
name: "David Garcia",
email: "[email protected]",
role: UserRole.ADMIN,
password: "password123",
},
{
name: "Lisa Wang",
email: "[email protected]",
role: UserRole.MEMBER,
password: "password123",
},
],
},
{
name: "Data Analytics Co",
slug: "data-analytics",
subscription: {
plan: SubscriptionPlan.PREMIUM,
status: SubscriptionStatus.ACTIVE,
startDate: new Date("2024-01-10"),
},
users: [
{
name: "Alex Johnson",
email: "[email protected]",
role: UserRole.ADMIN,
password: "password123",
},
{
name: "Rachel Kim",
email: "[email protected]",
role: UserRole.MEMBER,
password: "password123",
},
],
},
{
name: "Smart Solutions",
slug: "smart-solutions",
subscription: {
plan: SubscriptionPlan.FREE,
status: SubscriptionStatus.ACTIVE,
startDate: new Date("2024-02-15"),
},
users: [
{
name: "Daniel Brown",
email: "[email protected]",
role: UserRole.ADMIN,
password: "password123",
},
{
name: "Maria Rodriguez",
email: "[email protected]",
role: UserRole.MEMBER,
password: "password123",
},
],
},
];
for (const org of organizations) {
const createdOrg = await prisma.organization.create({
data: {
name: org.name,
slug: org.slug,
subscription: {
create: {
plan: org.subscription.plan,
status: org.subscription.status,
startDate: org.subscription.startDate,
},
},
},
});
for (const user of org.users) {
await prisma.user.create({
data: {
name: user.name,
email: user.email,
password: user.password,
role: user.role,
organizationId: createdOrg.id,
},
});
}
}
console.log("Seed data created successfully");
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
其他资源
总而言之,将 Cursor 与 Prisma 结合使用简化了您的工作流程,从生成 schema 和查询到编写种子脚本。通过遵循本指南,您可以节省时间、减少错误并专注于构建您的应用程序。
在其官方文档中了解更多关于 Cursor 的信息。
有关 Prisma 的更多信息和更新