AgentSkillsCN

nestjs-best-practices

NestJS 的动态模块、自定义异常、Mongoose 数据访问,以及功能模块结构的规范。适用于编写或重构 NestJS 代码,使其与这一风格相匹配时使用。

SKILL.md
--- frontmatter
name: nestjs-best-practices
description: NestJS conventions for dynamic modules, custom exceptions, Mongoose data access, and feature-module structure. Use when writing or refactoring NestJS code to match this style.
license: MIT
metadata:
  version: "1.0.0"

NestJS Best Practices

Conventions for NestJS applications using configurable dynamic modules, custom domain exceptions, Mongoose, and feature-based module organization.

When to Apply

  • Writing new NestJS modules, services, or controllers
  • Refactoring existing NestJS code to follow these patterns
  • Implementing authentication, validation, or data access
  • Organizing code by feature (not by technical layer)

Rule Categories

Architecture

  • arch-dynamic-modules — Use ConfigurableModuleBuilder, *-module-definition.ts, and static register() for dynamic modules. See rules/arch-dynamic-modules.md.
  • arch-feature-modules — Organize by feature under src/modules/<feature>/ with *.module.ts, *.module-definition.ts, and index.ts. See rules/arch-feature-modules.md.

Error Handling

Database & Data Access

  • db-mongoose-data-access — Inject Mongoose Connection via custom decorator (e.g. @InjectPrimaryMongoose()), use connection.model(Schema.name). See rules/db-mongoose-data-access.md.

Dependency Injection

API & Validation

  • api-dto-validation — Define DTOs with class-validator decorators (@IsEmail(), @IsString(), etc.) for input validation. See rules/api-dto-validation.md.

Security

  • security-guards — Use Throttler, JWT, and TOTP guards; configure ThrottlerModule with Redis storage where needed. See rules/security-guards.md.

How to Use

Read individual rule files under rules/ for incorrect vs correct examples and detailed guidance.

For the full compiled document with all rules in one file: AGENTS.md (generated by scripts/build-agents.ts).