nestjs-best-practices
NestJS best practices and architecture patterns for production apps
- License 许可
- MIT
- 所属仓库
- HigorAlves/orc
- 最近更新
- 2026-11-07
- Stars
- ★ 7
- Forks
- 0
01 前言 Front Matter
- name
- nestjs-best-practices
- license
- MIT
- metadata
- { "author": "Kadajett", "version": "1.1.0" }
- description
- NestJS best practices and architecture patterns for production apps
- when_to_use
- Use when writing, reviewing, or refactoring NestJS code — modules, dependency injection, security, and performance.
02 资源文件 51
- 参考文档.github/workflows/branch-protection.yml760 B
- 参考文档.github/workflows/deploy.yml1.2 KB
- 其他.gitignore237 B
- 参考文档AGENTS.md159.2 KB
- 参考文档README.md4.4 KB
- 参考文档rules/_sections.md2.1 KB
- 参考文档rules/_template.md631 B
- 参考文档rules/api-use-dto-serialization.md4.3 KB
- 参考文档rules/api-use-interceptors.md5.4 KB
- 参考文档rules/api-use-pipes.md5.3 KB
- 参考文档rules/api-versioning.md4.9 KB
- 参考文档rules/arch-avoid-circular-deps.md2.0 KB
- 参考文档rules/arch-feature-modules.md2.3 KB
- 参考文档rules/arch-module-sharing.md3.8 KB
- 参考文档rules/arch-single-responsibility.md2.9 KB
- 参考文档rules/arch-use-events.md3.0 KB
- 参考文档rules/arch-use-repository-pattern.md2.8 KB
- 参考文档rules/db-avoid-n-plus-one.md3.8 KB
- 参考文档rules/db-use-migrations.md3.7 KB
- 参考文档rules/db-use-transactions.md3.9 KB
- 参考文档rules/devops-graceful-shutdown.md5.8 KB
- 参考文档rules/devops-use-config-module.md4.3 KB
- 参考文档rules/devops-use-logging.md5.6 KB
- 参考文档rules/di-avoid-service-locator.md2.9 KB
- 参考文档rules/di-interface-segregation.md5.1 KB
- 参考文档rules/di-liskov-substitution.md7.1 KB
- 参考文档rules/di-prefer-constructor-injection.md2.3 KB
- 参考文档rules/di-scope-awareness.md2.6 KB
- 参考文档rules/di-use-interfaces-tokens.md2.5 KB
- 参考文档rules/error-handle-async-errors.md3.4 KB
- 参考文档rules/error-throw-http-exceptions.md3.1 KB
- 参考文档rules/error-use-exception-filters.md3.6 KB
- 参考文档rules/micro-use-health-checks.md5.8 KB
- 参考文档rules/micro-use-patterns.md5.0 KB
- 参考文档rules/micro-use-queues.md6.1 KB
- 参考文档rules/perf-async-hooks.md2.8 KB
- 参考文档rules/perf-lazy-loading.md3.7 KB
- 参考文档rules/perf-optimize-database.md3.2 KB
- 参考文档rules/perf-use-caching.md3.4 KB
- 参考文档rules/security-auth-jwt.md4.0 KB
- 参考文档rules/security-rate-limiting.md3.2 KB
- 参考文档rules/security-sanitize-output.md3.7 KB
- 参考文档rules/security-use-guards.md3.6 KB
- 参考文档rules/security-validate-all-input.md3.3 KB
- 参考文档rules/test-e2e-supertest.md4.7 KB
- 参考文档rules/test-mock-external-services.md4.5 KB
- 参考文档rules/test-use-testing-module.md4.3 KB
- 脚本scripts/build-agents.ts8.5 KB
- 脚本scripts/build.sh369 B
- 脚本scripts/package-lock.json8.5 KB
- 脚本scripts/package.json428 B
03 SKILL.md 正文
NestJS Best Practices
Comprehensive best practices guide for NestJS applications. Contains 40 rules across 10 categories, prioritized by impact to guide automated refactoring and code generation.
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
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Architecture | CRITICAL | arch- |
| 2 | Dependency Injection | CRITICAL | di- |
| 3 | Error Handling | HIGH | error- |
| 4 | Security | HIGH | security- |
| 5 | Performance | HIGH | perf- |
| 6 | Testing | MEDIUM-HIGH | test- |
| 7 | Database & ORM | MEDIUM-HIGH | db- |
| 8 | API Design | MEDIUM | api- |
| 9 | Microservices | MEDIUM | micro- |
| 10 | DevOps & Deployment | LOW-MEDIUM | devops- |
Quick Reference
1. Architecture (CRITICAL)
arch-avoid-circular-deps- Avoid circular module dependenciesarch-feature-modules- Organize by feature, not technical layerarch-module-sharing- Proper module exports/imports, avoid duplicate providersarch-single-responsibility- Focused services over "god services"arch-use-repository-pattern- Abstract database logic for testabilityarch-use-events- Event-driven architecture for decoupling
2. Dependency Injection (CRITICAL)
di-avoid-service-locator- Avoid service locator anti-patterndi-interface-segregation- Interface Segregation Principle (ISP)di-liskov-substitution- Liskov Substitution Principle (LSP)di-prefer-constructor-injection- Constructor over property injectiondi-scope-awareness- Understand singleton/request/transient scopesdi-use-interfaces-tokens- Use injection tokens for interfaces
3. Error Handling (HIGH)
error-use-exception-filters- Centralized exception handlingerror-throw-http-exceptions- Use NestJS HTTP exceptionserror-handle-async-errors- Handle async errors properly
4. Security (HIGH)
security-auth-jwt- Secure JWT authenticationsecurity-validate-all-input- Validate with class-validatorsecurity-use-guards- Authentication and authorization guardssecurity-sanitize-output- Prevent XSS attackssecurity-rate-limiting- Implement rate limiting
5. Performance (HIGH)
perf-async-hooks- Proper async lifecycle hooksperf-use-caching- Implement caching strategiesperf-optimize-database- Optimize database queriesperf-lazy-loading- Lazy load modules for faster startup
6. Testing (MEDIUM-HIGH)
test-use-testing-module- Use NestJS testing utilitiestest-e2e-supertest- E2E testing with Supertesttest-mock-external-services- Mock external dependencies
7. Database & ORM (MEDIUM-HIGH)
db-use-transactions- Transaction managementdb-avoid-n-plus-one- Avoid N+1 query problemsdb-use-migrations- Use migrations for schema changes
8. API Design (MEDIUM)
api-use-dto-serialization- DTO and response serializationapi-use-interceptors- Cross-cutting concernsapi-versioning- API versioning strategiesapi-use-pipes- Input transformation with pipes
9. Microservices (MEDIUM)
micro-use-patterns- Message and event patternsmicro-use-health-checks- Health checks for orchestrationmicro-use-queues- Background job processing
10. DevOps & Deployment (LOW-MEDIUM)
devops-use-config-module- Environment configurationdevops-use-logging- Structured loggingdevops-graceful-shutdown- Zero-downtime deployments
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.md
Each rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
04 同仓库更多 Skills
writing-plans
Write a comprehensive implementation plan of bite-sized TDD tasks that an engineer with zero codebase context could execute — exact files, code, tests, and verification steps
writing-for-agents
orc's doctrine layer for writing any document an agent consumes — skills, AGENTS.md/CLAUDE.md, reference docs
workspace-mode
Use when cwd is a parent dir holding multiple sibling git repos — defines repo auto-detection, --repos/--repo/--all-repos/--this-repo precedence, per-repo dispatch, and linked-PR mechanics.
wizard
Generate an interactive bash wizard walking a human through work only they can perform — credentials, secrets, one-off migrations
wait-what
Stop and re-pitch — the last message did not land; say it again simply, in the project's own vocabulary.
vitest
Vitest fast unit testing framework powered by Vite with Jest-compatible API