Skillshub nestjs-best-practices
NestJS best practices and architecture patterns for building production-ready applications. This skill should be used when writing, reviewing, or refactoring NestJS code to ensure proper patterns for modules, dependency injection, security, and performance.
install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/Harmeet10000/skills/nestjs-best-practices" ~/.claude/skills/comeonoliver-skillshub-nestjs-best-practices && rm -rf "$T"
manifest:
skills/Harmeet10000/skills/nestjs-best-practices/SKILL.mdsource content
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 | |
| 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 | |
Quick Reference
1. Architecture (CRITICAL)
- Avoid circular module dependenciesarch-avoid-circular-deps
- Organize by feature, not technical layerarch-feature-modules
- Proper module exports/imports, avoid duplicate providersarch-module-sharing
- Focused services over "god services"arch-single-responsibility
- Abstract database logic for testabilityarch-use-repository-pattern
- Event-driven architecture for decouplingarch-use-events
2. Dependency Injection (CRITICAL)
- Avoid service locator anti-patterndi-avoid-service-locator
- Interface Segregation Principle (ISP)di-interface-segregation
- Liskov Substitution Principle (LSP)di-liskov-substitution
- Constructor over property injectiondi-prefer-constructor-injection
- Understand singleton/request/transient scopesdi-scope-awareness
- Use injection tokens for interfacesdi-use-interfaces-tokens
3. Error Handling (HIGH)
- Centralized exception handlingerror-use-exception-filters
- Use NestJS HTTP exceptionserror-throw-http-exceptions
- Handle async errors properlyerror-handle-async-errors
4. Security (HIGH)
- Secure JWT authenticationsecurity-auth-jwt
- Validate with class-validatorsecurity-validate-all-input
- Authentication and authorization guardssecurity-use-guards
- Prevent XSS attackssecurity-sanitize-output
- Implement rate limitingsecurity-rate-limiting
5. Performance (HIGH)
- Proper async lifecycle hooksperf-async-hooks
- Implement caching strategiesperf-use-caching
- Optimize database queriesperf-optimize-database
- Lazy load modules for faster startupperf-lazy-loading
6. Testing (MEDIUM-HIGH)
- Use NestJS testing utilitiestest-use-testing-module
- E2E testing with Supertesttest-e2e-supertest
- Mock external dependenciestest-mock-external-services
7. Database & ORM (MEDIUM-HIGH)
- Transaction managementdb-use-transactions
- Avoid N+1 query problemsdb-avoid-n-plus-one
- Use migrations for schema changesdb-use-migrations
8. API Design (MEDIUM)
- DTO and response serializationapi-use-dto-serialization
- Cross-cutting concernsapi-use-interceptors
- API versioning strategiesapi-versioning
- Input transformation with pipesapi-use-pipes
9. Microservices (MEDIUM)
- Message and event patternsmicro-use-patterns
- Health checks for orchestrationmicro-use-health-checks
- Background job processingmicro-use-queues
10. DevOps & Deployment (LOW-MEDIUM)
- Environment configurationdevops-use-config-module
- Structured loggingdevops-use-logging
- Zero-downtime deploymentsdevops-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.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