1---2name: backend-nestjs3description: Build, maintain, and debug NestJS backends (TypeScript, REST/GraphQL).4---56# Backend - NestJS78## When to use this skill9- Creating or updating NestJS modules, controllers, services, DTOs, and pipes.10- Adding REST or GraphQL endpoints, validation, authentication, or persistence.11- Improving logging, error handling, testing, or configuration management.1213## Quick start141. Install deps: `npm ci` (or project-standard package manager).152. Env: copy `.env.example` -> `.env`; set database URL, JWT secrets, external service keys.163. Dev server: `npm run start:dev`; prod build: `npm run build` and `npm run start:prod`.174. Tests: `npm test` (unit) and `npm run test:e2e` (e2e); lint: `npm run lint`.1819## Core patterns20- Modules group related providers/controllers; keep dependencies minimal.21- DTOs define input/output shapes; use `class-validator` and `class-transformer` for validation and transformation.22- Controllers handle transport; services contain business logic; repositories/ORM clients manage data access.23- Use pipes for validation/transformation, filters for cross-cutting error shaping, guards for auth.2425## Coding principles26- Keep controllers thin; put business logic in services; keep data access in repositories/ORM layers.27- Validate and transform all inputs via DTOs; avoid leaking domain errors—normalize with filters/interceptors.28- Use config module (no direct `process.env` reads); validate env with Joi.29- Log with correlation IDs; avoid logging secrets; prefer structured logging.30- Enforce lint/format/tests/type-check/build before merge (run `scripts/dev-check.sh`).3132## Patterns and snippets33- DTO, controller/service, config, and error filter templates live in references/snippets.md.34- Prefer guards for authz, strategies for authn; keep transactions in services.35- Map ORM errors to HTTP-friendly responses; keep domain errors typed.3637## Authentication and authorization38- JWT or session strategies via `@nestjs/passport` and `PassportStrategy`.39- Guards for role/permission checks; apply globally or per-route.40- Hash secrets with bcrypt/argon2; never log secrets.4142## Error handling and logging43- Global `HttpExceptionFilter` to normalize errors.44- Use Nest logger or a structured logger (pino/winston) with request correlation IDs.4546## Testing47- Unit: test services with in-memory fakes; use `TestingModule` to inject dependencies.48- E2E: spin app via `Test.createTestingModule`, apply pipes/filters, hit HTTP endpoints; reset database between tests.4950## Bundled resources51- scripts/dev-check.sh: run pre-commit/PR to verify lint, format, unit/e2e tests, type-check, and build with the detected package manager.52- references/coding-standards.md: quick guardrails for controller/service boundaries, validation, config, errors, logging, and testing.53- references/best-practices.md: deeper guidance on module design, DTOs, error shaping, auth, logging, and testing conventions.54- references/snippets.md: templates for DTOs, controllers, config, and error filters.55- assets/pr-template.md: PR checklist covering testing, migrations, and API contract updates.56- assets/migration-checklist.md: use when adding schema changes to ensure migrations are safe and tested.5758## Delivery checklist59- Lint, tests, and type checks pass.60- DTOs validate inputs; guards cover protected routes; errors are normalized.61- Config validated; secrets kept out of repo; migrations applied; health endpoint present if required.