nestjs-best-practices

Applies 40 prioritized NestJS architecture, security, and performance rules when writing or reviewing code.

Updated Jul 25, 2026
One-click install
npx skills add https://github.com/kaannakiin/turborepo_template --skill nestjs-best-practices-kaannakiin
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: nestjs-best-practices
Source: https://github.com/kaannakiin/turborepo_template/tree/main/.agents/skills/nestjs-best-practices
Command: npx skills add https://github.com/kaannakiin/turborepo_template --skill nestjs-best-practices-kaannakiin

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? NestJS codebases frequently suffer from circular dependencies, god services, N+1 queries, missing input validation, and inconsistent error handling. This Skill provides a prioritized rule set of 40 best practices across 10 categories so AI-assisted code generation and review follows proven NestJS patterns instead of producing fragile, hard-to-test code. ## Core Features & Use Cases - Prioritized Rule Catalog: 40 rules organized by impact (CRITICAL architecture and dependency injection first, then error handling, security, performance, testing, database, API design, microservices, and DevOps), each with incorrect and correct TypeScript code examples. - Code Review Guidance: Reference specific rules like arch-avoid-circular-deps, security-validate-all-input, or db-avoid-n-plus-one when reviewing or refactoring NestJS modules, controllers, and services. - Production Patterns: Covers JWT authentication, class-validator input validation, TypeORM transactions and migrations, caching, event-driven decoupling, structured logging, and graceful shutdown. - Use Case: When asked to add a new orders feature, the Skill guides creation of a feature module with constructor injection, DTO validation pipes, transactional saves, and proper module exports instead of a tangled god service. ## Quick Start Review my NestJS users module against the architecture and dependency injection rules and refactor any violations you find.

Frequently Asked Questions about nestjs-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix circular dependencies between NestJS modules?

Circular dependencies are resolved by extracting shared logic into a third module that both modules import, or by using event-driven communication with @nestjs/event-emitter so modules react to events without direct imports. Forward references work but indicate architectural problems.

How to prevent N+1 query problems in NestJS with TypeORM?

Use the relations option in find queries or QueryBuilder joins to eager-load related entities in a single query instead of lazy-loading inside loops. For GraphQL, use DataLoader to batch and cache per-request queries.

Should I use constructor injection or property injection in NestJS?

Constructor injection is preferred because dependencies are explicit, required at instantiation, and easy to mock in tests. Property injection hides dependencies and complicates testing with the NestJS testing module.

Does NestJS support API versioning for breaking changes?

Yes, NestJS has built-in versioning via app.enableVersioning() supporting URI, header, and media type strategies. Apply @Version() decorators per controller or route, and use VERSION_NEUTRAL for routes shared across versions.

Why is synchronize true dangerous in TypeORM production?

The synchronize option auto-alters the database schema to match entities, which can drop columns or data in production. Use migrations instead for versioned, reversible schema changes with explicit up and down methods.

When should I use the repository pattern in NestJS?

Use custom repositories when services accumulate complex query logic. Encapsulating queries in a repository keeps services focused on business logic, simplifies mocking in unit tests, and isolates database implementation details.