j4flmao/agent_skills_nodejs_nestjs

designing-nestjs-services

Designs NestJS application services as use-case classes that orchestrate domain logic and ports without leaking the framework.

ソースを見る

使い方

  1. インストールコマンドをコピーし、自分の環境へ導入する前にソースを確認します。
  2. 必要なツール、認証情報、対応環境を原文で確認します。
  3. 入力と成果物を指定し、出力を資料と照合します。
npx skills add https://github.com/j4flmao/agent_skills_nodejs_nestjs --skill designing-nestjs-services

開始前の確認

  • 導入にはモデル契約、API枠、ファイルへのアクセス許可は含まれません。
  • スター数や導入数は活動の指標であり、独立した品質・安全性試験ではありません。
リポジトリの原文

見出し、例、コード、表、リンク、参照画像を含む原文を表示しています。

Designing NestJS Services

When to use

  • Creating new use cases
  • Refactoring services that mix HTTP/DB/domain
  • Moving from anemic services to OOP

Core rules

  1. Application services are use-case classes, one per business intent
  2. Services orchestrate domain + ports, no framework imports (no @Injectable() in domain)
  3. Return Result<T,E> from use-cases, don't throw for expected failures
  4. Dependencies injected via constructor
  5. No HTTP/ORM imports in application services

Reference shape (TypeScript)

Use-Case Class

typescript
@Injectable()
export class CreateUserUseCase {
  constructor(
    @Inject('UserRepository') private readonly repo: UserRepository,
    private readonly hasher: PasswordHasher
  ) {}

  async execute(dto: CreateUserDto): Promise<Result<User, AppError>> {
    const existing = await this.repo.findByEmail(dto.email);
    if (existing) return err(new ConflictError(`User ${dto.email} already exists`));

    const user = User.create(dto.id, Email.create(dto.email));
    await this.repo.save(user);
    return ok(user);
  }
}

Examples — Do

typescript
// Single responsibility use-case
@Injectable()
export class SubmitOrderUseCase {
  constructor(
    @Inject('OrderRepository') private repo: OrderRepository,
    private paymentService: PaymentService
  ) {}
  async execute(orderId: string): Promise<Result<Order, AppError>> { ... }
}

Examples — Don't

typescript
// ❌ Anemic service with many methods
@Injectable()
export class UserService {
  create() { ... }
  update() { ... }
  delete() { ... }
  // Mixed responsibilities
}
// ❌ Framework leaking into application
import { InjectRepository } from '@nestjs/typeorm'; // Should be in infra

Checklist

  • [ ] Use-cases are single-responsibility classes
  • [ ] Return Result<T,E>
  • [ ] No framework imports in application layer
  • [ ] Dependencies injected via constructor

See reference/service-patterns.md for full patterns.

タスクに関連する項目

同じリポジトリから

関連する Skills

すべての Skills