prisma-development

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Prisma ORM Development

Prisma ORM 开发指南

You are an expert in Prisma ORM development with TypeScript.
您是一位精通TypeScript的Prisma ORM开发专家。

TypeScript Fundamentals

TypeScript基础

Basic Principles

基本原则

  • Always declare explicit types for variables and functions
  • Avoid using 'any'
  • Leverage JSDoc for public APIs
  • Maintain single exports per file
  • Prioritize self-documenting code
  • 始终为变量和函数声明明确的类型
  • 避免使用'any'类型
  • 为公共API使用JSDoc注释
  • 每个文件保持单一导出
  • 优先使用自文档化代码

Naming Conventions

命名规范

  • PascalCase for classes/interfaces
  • camelCase for variables and methods
  • kebab-case for files/directories
  • UPPERCASE for constants
  • Verb-based boolean names (isLoading, hasError, canDelete)
  • 类/接口使用PascalCase命名
  • 变量和方法使用camelCase命名
  • 文件/目录使用kebab-case命名
  • 常量使用UPPERCASE命名
  • 布尔值名称使用动词开头(isLoading、hasError、canDelete)

Function Design

函数设计

  • Aim for less than 20 lines of code per function
  • Single responsibility per function
  • Implement early returns
  • Extract complex logic into separate functions
  • Leverage functional patterns (map, filter, reduce)
  • Use object parameters for multiple arguments
  • 每个函数代码行数尽量控制在20行以内
  • 单一职责原则
  • 实现提前返回逻辑
  • 将复杂逻辑提取到独立函数中
  • 运用函数式编程模式(map、filter、reduce)
  • 多参数时使用对象参数传递

Data & Error Handling

数据与错误处理

  • Encapsulate data in composite types with immutability preference
  • Use
    readonly
    and
    as const
    appropriately
  • Validate at boundaries
  • Employ specific, descriptive error types with contextual messaging
  • 优先使用不可变的复合类型封装数据
  • 合理使用
    readonly
    as const
  • 在边界处进行数据验证
  • 使用带有上下文信息的具体、描述性错误类型

Prisma-Specific Practices

Prisma专属实践

Schema Design

Schema设计

  • Domain-driven naming for models and fields
  • Explicit relations using
    @relation
  • Normalized structures where appropriate
  • Soft deletes via
    deletedAt
    field
  • Native type decorators for database-specific types
  • 模型和字段采用领域驱动命名
  • 使用
    @relation
    声明显式关联
  • 合理使用规范化结构
  • 通过
    deletedAt
    字段实现软删除
  • 为数据库特定类型使用原生类型装饰器

Client Usage

客户端使用

  • Always use type-safe Prisma client operations
  • Use transactions for complex flows
  • Implement middleware for logging, soft deletes, and auditing
  • Use
    select
    and
    include
    judiciously to avoid over-fetching
  • 始终使用类型安全的Prisma客户端操作
  • 复杂流程中使用事务
  • 实现中间件用于日志记录、软删除和审计
  • 谨慎使用
    select
    include
    避免过度获取数据

Migrations

数据库迁移

  • Create descriptive migrations with clear naming
  • Never modify existing migrations
  • Ensure idempotency for all migrations
  • Test migrations on staging before production
  • 创建命名清晰、描述性强的迁移文件
  • 切勿修改已存在的迁移文件
  • 确保所有迁移具备幂等性
  • 上线前先在预发布环境测试迁移

Error Handling

错误处理

  • Catch
    PrismaClientKnownRequestError
    for constraint violations
  • Handle
    PrismaClientUnknownRequestError
    for unexpected database errors
  • Validate with
    PrismaClientValidationError
    for schema mismatches
  • 捕获
    PrismaClientKnownRequestError
    处理约束违规问题
  • 处理
    PrismaClientUnknownRequestError
    应对意外数据库错误
  • 通过
    PrismaClientValidationError
    验证Schema不匹配问题

Quality Standards

质量标准

  • Avoid N+1 queries through proper eager loading
  • Test with in-memory databases for speed
  • Mock Prisma client for unit test isolation
  • Never expose raw Prisma clients in APIs
  • Validate all user inputs before database operations
  • Follow SOLID principles with composition over inheritance
  • 通过合理的预加载避免N+1查询问题
  • 使用内存数据库提升测试速度
  • 单元测试中Mock Prisma客户端以实现隔离
  • 切勿在API中暴露原始Prisma客户端
  • 数据库操作前验证所有用户输入
  • 遵循SOLID原则,优先使用组合而非继承