Skip to content

giuseppe-trisciuoglio/developer-kit / plugins/developer-kit-typescript/skills/nestjs-best-practices

nestjs-best-practices

Verified

Provides comprehensive NestJS best practices including modular architecture, dependency injection scoping, exception filters, DTO validation with class-validator, and Drizzle ORM integration. Use when designing NestJS modules, implementing providers, creating exception filters, validating DTOs, or integrating Drizzle ORM within NestJS applications.

references/arch-module-boundaries.md

View on GitHub
# Rule: Module Boundaries and Encapsulation

## Context

NestJS modules define the boundaries of your application's domain features. Proper encapsulation prevents tight coupling and makes the codebase scalable and testable.

## Guidelines

### Module Organization
- Each domain feature must have its own `@Module()` (e.g., `OrderModule`, `UserModule`)
- Only export providers that other modules genuinely need — keep everything else private
- Import only the modules you depend on — avoid importing unrelated modules
- Use a `SharedModule` for cross-cutting utilities (logging, caching, configuration)

### Circular Dependencies
- **Avoid `forwardRef()` by default** — it is a code smell indicating poor module design
- If two modules depend on each other, extract shared logic into a third module
- Only use `forwardRef()` when architectural constraints make extraction impractical
- Document every `forwardRef()` usage with a comment explaining why it's necessary

### Module Registration
- Use `forRoot()` / `forRootAsync()` for global singleton modules (database, config)
- Use `forFeature()` for domain-specific module registration
- Use dynamic modules for configurable providers

## Examples

### ✅ Correct — Proper Module Encapsulation

```typescript
// order/order.module.ts
@Module({
  imports: [PaymentModule, UserModule],
  controllers: [OrderController],
  providers: [OrderService, OrderRepository],
  exports: [OrderService], // Only export what others need
})
export class OrderModule {}

// payment/payment.module.ts
@Module({
  controllers: [PaymentController],
  providers: [PaymentService, PaymentGateway],
  exports: [PaymentService], // Exported for OrderModule to use
})
export class PaymentModule {}
```

### ❌ Incorrect — God Module Anti-Pattern

```typescript
// app.module.ts — everything dumped in root module
@Module({
  controllers: [
    OrderController,
    UserController,
    PaymentController,
    ProductController,
  ],
  providers: [
    OrderService,
    UserService,
    PaymentService,
    ProductService,
    OrderRepository,
    UserRepository,
  ],
})
export class AppModule {} // No encapsulation, all providers are shared
```

### ❌ Incorrect — Unnecessary forwardRef

```typescript
// Instead of this circular dependency:
@Module({
  imports: [forwardRef(() => UserModule)],
  providers: [OrderService],
})
export class OrderModule {}

@Module({
  imports: [forwardRef(() => OrderModule)],
  providers: [UserService],
})
export class UserModule {}

// ✅ Extract shared logic into a third module:
@Module({
  providers: [UserOrderLinkService],
  exports: [UserOrderLinkService],
})
export class UserOrderModule {}
```
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall