nestjs-best-practices
NestJS best practices and architecture patterns for production applications. Use when writing, reviewing, or refactoring NestJS code covering modules, dependency injection, security, and performance.
- License 许可
- MIT
- 所属仓库
- carterdea/dots
- 最近更新
- 2026-11-15
- Stars
- ★ 4
- Forks
- 3
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 applications. Use when writing, reviewing, or refactoring NestJS code covering modules, dependency injection, security, and performance.
02 资源文件 44
- 参考文档evals/eval_queries.json909 B
- 参考文档evals/evals.json4.3 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
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
04 同仓库更多 Skills
test-audit
Writing/changing/reviewing/sweeping tests: reject duplicate or implementation-coupled proof; remove test-only production seams.
untangle-codebase
Untangle a whole codebase in one pass, so agents working in it read less per change, break less without noticing, and debug faster.
write-swift
How to write modern Swift well — modeling with value types, Swift 6 data-race safety and approachable concurrency (@concurrent, main-actor-by-default, actors, task groups), protocols and generics (some vs any), API design, performance and ARC, Swift Testing, macros, and the modern language features agents don't know about yet. Use when writing, reviewing, or migrating Swift, or when a concurrency error, a hang, a data race, a retain cycle, or a performance problem needs fixing.
stageshow
Build faux product UI that looks like a real product, as a still illustration or an animated demo with a cursor. Browser and app windows, dashboards, phones, notifications, terminals, bento grids and cursor walkthroughs, framed, masked and cropped the way a marketing page, feature card or docs hero needs them. Use whenever someone wants a fake or mocked UI, a product demo or walkthrough animation, a hero or feature-card illustration of an app, a product shot in the style of a polished marketing site, or an animated cursor doing things in an interface, even if they never say "faux UI". React and plain CSS.
review-animations
Reviews animation and motion code against a high craft bar derived from Emil Kowalski's design engineering philosophy. Default to flagging; approval is earned.
remove-animations
Find the animations an interface doesn't need and remove them — motion on keyboard actions, high-frequency surfaces, functional data, and decoration with no purpose — then clean up what they leave behind. Edits code, but only to subtract; it never adds or retunes motion. Use when the user says the app "feels slow", "has too much animation", "feels over-animated", wants to "tone down the motion", or asks what animations to remove. For finding places that should animate, use find-animation-opportunities; for fixing animations that should stay, use improve-animations or review-animations.