nestjs-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNestJS Best Practices
NestJS 最佳实践
Comprehensive best practices guide for NestJS applications. Contains 40 rules across 10 categories, prioritized by impact to guide automated refactoring and code generation.
面向NestJS应用的全面最佳实践指南。包含10个类别下的40条规则,按影响程度排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new NestJS modules, controllers, or services
- Implementing authentication and authorization
- Reviewing code for architecture and security issues
- Refactoring existing NestJS codebases
- Optimizing performance or database queries
- Building microservices architectures
在以下场景中参考本指南:
- 编写新的NestJS模块、控制器或服务
- 实现身份验证与授权功能
- 评审代码的架构与安全问题
- 重构现有NestJS代码库
- 优化性能或数据库查询
- 构建微服务架构
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Architecture | CRITICAL | |
| 2 | Dependency Injection | CRITICAL | |
| 3 | Error Handling | HIGH | |
| 4 | Security | HIGH | |
| 5 | Performance | HIGH | |
| 6 | Testing | MEDIUM-HIGH | |
| 7 | Database & ORM | MEDIUM-HIGH | |
| 8 | API Design | MEDIUM | |
| 9 | Microservices | MEDIUM | |
| 10 | DevOps & Deployment | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 架构 | 关键 | |
| 2 | 依赖注入 | 关键 | |
| 3 | 错误处理 | 高 | |
| 4 | 安全 | 高 | |
| 5 | 性能 | 高 | |
| 6 | 测试 | 中高 | |
| 7 | 数据库与ORM | 中高 | |
| 8 | API设计 | 中 | |
| 9 | 微服务 | 中 | |
| 10 | DevOps与部署 | 低中 | |
Quick Reference
快速参考
1. Architecture (CRITICAL)
1. 架构(关键)
- - Avoid circular module dependencies
arch-avoid-circular-deps - - Organize by feature, not technical layer
arch-feature-modules - - Proper module exports/imports, avoid duplicate providers
arch-module-sharing - - Focused services over "god services"
arch-single-responsibility - - Abstract database logic for testability
arch-use-repository-pattern - - Event-driven architecture for decoupling
arch-use-events
- - 避免模块循环依赖
arch-avoid-circular-deps - - 按功能而非技术层组织代码
arch-feature-modules - - 正确进行模块导出/导入,避免重复提供者
arch-module-sharing - - 专注于单一职责的服务,避免“上帝服务”
arch-single-responsibility - - 抽象数据库逻辑以提升可测试性
arch-use-repository-pattern - - 使用事件驱动架构实现解耦
arch-use-events
2. Dependency Injection (CRITICAL)
2. 依赖注入(关键)
- - Avoid service locator anti-pattern
di-avoid-service-locator - - Interface Segregation Principle (ISP)
di-interface-segregation - - Liskov Substitution Principle (LSP)
di-liskov-substitution - - Constructor over property injection
di-prefer-constructor-injection - - Understand singleton/request/transient scopes
di-scope-awareness - - Use injection tokens for interfaces
di-use-interfaces-tokens
- - 避免服务定位器反模式
di-avoid-service-locator - - 遵循接口隔离原则(ISP)
di-interface-segregation - - 遵循里氏替换原则(LSP)
di-liskov-substitution - - 优先使用构造函数注入而非属性注入
di-prefer-constructor-injection - - 理解单例/请求/瞬态作用域
di-scope-awareness - - 为接口使用注入令牌
di-use-interfaces-tokens
3. Error Handling (HIGH)
3. 错误处理(高)
- - Centralized exception handling
error-use-exception-filters - - Use NestJS HTTP exceptions
error-throw-http-exceptions - - Handle async errors properly
error-handle-async-errors
- - 集中式异常处理
error-use-exception-filters - - 使用NestJS HTTP异常
error-throw-http-exceptions - - 正确处理异步错误
error-handle-async-errors
4. Security (HIGH)
4. 安全(高)
- - Secure JWT authentication
security-auth-jwt - - Validate with class-validator
security-validate-all-input - - Authentication and authorization guards
security-use-guards - - Prevent XSS attacks
security-sanitize-output - - Implement rate limiting
security-rate-limiting
- - 安全的JWT身份验证
security-auth-jwt - - 使用class-validator进行验证
security-validate-all-input - - 使用身份验证与授权守卫
security-use-guards - - 防止XSS攻击
security-sanitize-output - - 实现速率限制
security-rate-limiting
5. Performance (HIGH)
5. 性能(高)
- - Proper async lifecycle hooks
perf-async-hooks - - Implement caching strategies
perf-use-caching - - Optimize database queries
perf-optimize-database - - Lazy load modules for faster startup
perf-lazy-loading
- - 正确使用异步生命周期钩子
perf-async-hooks - - 实现缓存策略
perf-use-caching - - 优化数据库查询
perf-optimize-database - - 懒加载模块以加快启动速度
perf-lazy-loading
6. Testing (MEDIUM-HIGH)
6. 测试(中高)
- - Use NestJS testing utilities
test-use-testing-module - - E2E testing with Supertest
test-e2e-supertest - - Mock external dependencies
test-mock-external-services
- - 使用NestJS测试工具
test-use-testing-module - - 使用Supertest进行端到端测试
test-e2e-supertest - - 模拟外部依赖
test-mock-external-services
7. Database & ORM (MEDIUM-HIGH)
7. 数据库与ORM(中高)
- - Transaction management
db-use-transactions - - Avoid N+1 query problems
db-avoid-n-plus-one - - Use migrations for schema changes
db-use-migrations
- - 事务管理
db-use-transactions - - 避免N+1查询问题
db-avoid-n-plus-one - - 使用迁移进行 schema 变更
db-use-migrations
8. API Design (MEDIUM)
8. API设计(中)
- - DTO and response serialization
api-use-dto-serialization - - Cross-cutting concerns
api-use-interceptors - - API versioning strategies
api-versioning - - Input transformation with pipes
api-use-pipes
- - DTO与响应序列化
api-use-dto-serialization - - 处理横切关注点
api-use-interceptors - - API版本控制策略
api-versioning - - 使用管道进行输入转换
api-use-pipes
9. Microservices (MEDIUM)
9. 微服务(中)
- - Message and event patterns
micro-use-patterns - - Health checks for orchestration
micro-use-health-checks - - Background job processing
micro-use-queues
- - 消息与事件模式
micro-use-patterns - - 用于编排的健康检查
micro-use-health-checks - - 后台任务处理
micro-use-queues
10. DevOps & Deployment (LOW-MEDIUM)
10. DevOps与部署(低中)
- - Environment configuration
devops-use-config-module - - Structured logging
devops-use-logging - - Zero-downtime deployments
devops-graceful-shutdown
- - 环境配置
devops-use-config-module - - 结构化日志
devops-use-logging - - 零停机部署
devops-graceful-shutdown
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/arch-avoid-circular-deps.md
rules/security-validate-all-input.md
rules/_sections.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单个规则文件获取详细说明与代码示例:
rules/arch-avoid-circular-deps.md
rules/security-validate-all-input.md
rules/_sections.md每个规则文件包含:
- 简要说明该规则的重要性
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景信息与参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则详细内容的完整指南:
AGENTS.md