NestJS Expert Developer
Expert guidance for building scalable, maintainable NestJS applications with TypeScript best practices.
Overview
This skill provides comprehensive coding standards and architectural patterns for NestJS development. It emphasizes constructor-based dependency injection, separation of concerns, modular design, and TypeScript best practices to build robust backend applications.
Instructions
Follow these principles and patterns when developing NestJS applications:
1. Key Principles
Write concise, technical TypeScript code with accurate examplesUse constructor injection for all dependenciesApply separation of concerns rigorously throughout the codebaseCreate separate services for each distinct piece of business logicPrefer iteration and modularization over code duplicationStructure files logically: use a `services` folder when a module contains more than one service2. Naming Conventions
Use lowercase with dashes for all directory names (e.g., `product-order`, `user-management`)Follow consistent naming patterns across the applicationName files descriptively to reflect their purpose and responsibility3. TypeScript Usage
Use TypeScript for all code without exceptionsPrefer `interface` over `type` for object shape definitionsCreate injectable services that implement TypeScript interfacesUse a `lib` directory and module for shared tools and utilities that can be reused across multiple projectsLeverage TypeScript's type system for compile-time safety4. Logic Implementation
Implement all application logic inside services, never in controllers or gatewaysKeep controllers clean and focused solely on HTTP request/response handlingKeep gateways clean and focused solely on WebSocket event handlingUse the `useClass` approach when registering modules that have configuration dependenciesDefine all configuration-related classes inside a dedicated `config` directory to improve modularity and maintainability5. Modular Design
Create separate modules for external packages or third-party tools that don't have native NestJS supportWrap external dependencies in NestJS modules to maintain consistent architectureDesign modules to be self-contained with clear boundariesExport only what's necessary from each module6. Dependency Injection
Always use constructor-based dependency injectionNever instantiate services manually with `new`Register all providers in the appropriate moduleUse custom providers (`useClass`, `useFactory`, `useValue`) when needed7. Service Organization
Create one service per logical concern or business domainKeep services focused on a single responsibilityPlace multiple related services in a `services` subdirectory within the moduleUse service composition to build complex functionality from simple services8. Code Structure Example
```
src/
├── modules/
│ ├── product-order/
│ │ ├── services/
│ │ │ ├── order.service.ts
│ │ │ ├── order-validation.service.ts
│ │ │ └── order-notification.service.ts
│ │ ├── config/
│ │ │ └── order.config.ts
│ │ ├── product-order.controller.ts
│ │ ├── product-order.module.ts
│ │ └── interfaces/
│ │ └── order.interface.ts
│ └── user-management/
│ ├── user.service.ts
│ ├── user.controller.ts
│ └── user.module.ts
└── lib/
├── database/
├── logger/
└── utilities/
```
9. Configuration Management
Isolate all configuration logic in dedicated classes within a `config` directoryUse NestJS ConfigModule for environment-based configurationValidate configuration at application startupType all configuration objects10. Best Practices Checklist
Before completing any task, verify:
[ ] All business logic is in services, not controllers or gateways[ ] Constructor injection is used for all dependencies[ ] Each service has a single, clear responsibility[ ] Directory names use lowercase with dashes[ ] TypeScript interfaces are defined for all data structures[ ] Configuration is isolated in dedicated classes[ ] External dependencies are wrapped in NestJS modules[ ] Code is modular and reusableAdditional Resources
Always refer to the project's Code of Conduct and linked documentation for team-specific conventions and standards.
Example Usage
When asked to create a new feature:
1. Design the module structure following the naming conventions
2. Define TypeScript interfaces for all data models
3. Create focused services with single responsibilities
4. Implement controllers that delegate to services
5. Register all providers in the module
6. Add configuration classes if needed
7. Ensure proper dependency injection throughout
This skill ensures consistent, maintainable, and scalable NestJS application development.