agentleFS
Hive-AcademyHive-Academy/ptah-extension/.claude/skills/resilient-nestjs-patterns/SKILL.md

resilient-nestjs-patterns

Skill29 starsChanged 2 months agoMITView on GitHub

NestJS service patterns for orchestration and resilience. Use when splitting a complex service, layering controller, service and db service, adding retries or fallbacks, wiring EventEmitter events or SSE, or designing forRoot/forTesting dynamic modules.

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 3 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this decision tree to determine which pattern applies to your current problem.

    line 34
  2. Distinctive:

    When refactoring an existing codebase, adopt patterns in this order:

    line 202
  3. Distinctive:

    Do not adopt all five at once.

    line 210

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Resilient NestJS Patterns

Five interconnected architectural patterns that take a NestJS backend from prototype to production. These patterns address the structural and reliability concerns that emerge once basic CRUD, auth, and third-party integration are in place.

Activation scope

This skill activates per-module during Stage B of the SaaS bootstrap — typically when implementing a Resilience & Events roadmap item or hardening a specific domain module. It is NOT bundled into the Stage A foundation scaffold; saas-workspace-initializer deliberately leaves orchestration, retries, SSE, and dynamic-module wiring out of the foundation so each pattern lands with real domain context.

When You Need These Patterns

  • A service file has grown past 500 lines and handles unrelated concerns
  • Controllers contain business logic mixed with HTTP plumbing
  • External API calls fail silently or crash the request pipeline
  • Side-effects (emails, cache invalidation, audit logs) are tangled into core business methods
  • Module configuration is duplicated across environments and test suites

Pattern Quick Reference

PatternUse CaseReference
Service OrchestrationComplex domain with multiple cooperating servicesservice-orchestration.md
Domain Service LayeringClean separation of HTTP, business, and data concernsdomain-service-layering.md
Retry and FallbackResilient external service communicationretry-and-fallback.md
Event-Driven ArchitectureAsync side-effects and real-time updates via SSEevent-driven-architecture.md
Dynamic ModulesConfigurable, testable module design with forRoot/forTestingdynamic-modules.md

Pattern Selection Guide

Use this decision tree to determine which pattern applies to your current problem.

Is the problem about code organization?

Yes, a single service does too much:

  • Methods group into 3+ unrelated clusters -> Service Orchestration
  • The service is hard to test because mocking requires 8+ dependencies -> Service Orchestration

Yes, controllers contain business logic:

  • Controllers call Prisma directly -> Domain Service Layering
  • Business rules live alongside HTTP response mapping -> Domain Service Layering
  • Transaction management is scattered across controllers -> Domain Service Layering

Is the problem about reliability?

External API calls fail unpredictably:

  • Transient errors (timeouts, 503s) should be retried -> Retry and Fallback (retry with backoff)
  • A local data source can serve as backup -> Retry and Fallback (fallback strategy)
  • High call volume with potential provider downtime -> Retry and Fallback (circuit breaker)

Is the problem about coupling?

Side-effects are tangled into business methods:

  • Sending emails after subscription creation -> Event-Driven Architecture
  • Invalidating caches after data updates -> Event-Driven Architecture
  • Broadcasting real-time updates to connected clients -> Event-Driven Architecture (SSE)

Is the problem about configuration?

Module setup varies across environments:

  • Production uses real SDK, tests need mocks -> Dynamic Modules (forRoot/forTesting)
  • Provider selection depends on environment variables -> Dynamic Modules (environment-driven)
  • A module is imported everywhere with the same config -> Dynamic Modules (global modules)

How Patterns Compose

These patterns are not isolated. In a real module, they layer together:

Controller (thin HTTP layer)                    [Domain Service Layering]
  |
  v
BusinessService (orchestrator)                  [Service Orchestration]
  |-- DbService (data access, transactions)     [Domain Service Layering]
  |-- ExternalApiService (retry + fallback)     [Retry and Fallback]
  |-- EventEmitter (async side-effects)         [Event-Driven Architecture]
        |
        v
      EventListener -> SSE broadcast            [Event-Driven Architecture]

Composed Example: Subscription Domain

// Controller: thin HTTP, delegates everything
@Controller('subscriptions')
export class SubscriptionController {
  constructor(private readonly subscriptionService: SubscriptionService) {}

  @Post()
  async create(@Body() dto: CreateSubscriptionDto, @CurrentUser() user: JwtPayload) {
    return this.subscriptionService.createSubscription(user.sub, dto);
  }
}

// Service: orchestrates business logic
@Injectable()
export class SubscriptionService {
  constructor(
    private readonly dbService: SubscriptionDbService, // Data layer
    private readonly paymentService: PaymentService, // External (retry/fallback)
    private readonly eventEmitter: EventEmitter2, // Async side-effects
  ) {}

  async createSubscription(userId: string, dto: CreateSubscriptionDto) {
    const user = await this.dbService.findUserOrFail(userId);

    // External call with retry built into PaymentService
    const payment = await this.paymentService.createSubscription({
      customerId: user.paymentCustomerId,
      priceId: dto.priceId,
    });

    // Transactional database write
    const subscription = await this.dbService.createWithTransaction(userId, payment);

    // Fire-and-forget side-effects
    this.eventEmitter.emit('subscription.created', {
      userId,
      subscriptionId: subscription.id,
      plan: dto.priceId,
    });

    return subscription;
  }
}

// DbService: pure data access
@Injectable()
export class SubscriptionDbService {
  constructor(private readonly prisma: PrismaService) {}

  async createWithTransaction(userId: string, payment: PaymentResult) {
    return this.prisma.$transaction(async (tx) => {
      const subscription = await tx.subscription.create({
        data: { userId, externalId: payment.id, status: 'ACTIVE', plan: payment.plan },
      });
      await tx.user.update({
        where: { id: userId },
        data: { subscriptionId: subscription.id },
      });
      return subscription;
    });
  }
}

// Event listener: async side-effect
@Injectable()
export class SubscriptionEventListener {
  constructor(
    private readonly emailService: EmailService,
    private readonly eventsService: EventsService,
  ) {}

  @OnEvent('subscription.created')
  async handleSubscriptionCreated(payload: SubscriptionCreatedEvent) {
    await this.emailService.sendWelcomeEmail(payload.userId);
    this.eventsService.broadcast(payload.userId, {
      type: 'subscription.created',
      data: { subscriptionId: payload.subscriptionId },
    });
  }
}

Module Wiring with Dynamic Module

@Module({})
export class SubscriptionModule {
  static forRoot(): DynamicModule {
    return {
      module: SubscriptionModule,
      imports: [PaymentModule.forRoot(), EventEmitterModule.forRoot()],
      controllers: [SubscriptionController, SubscriptionSseController],
      providers: [SubscriptionService, SubscriptionDbService, SubscriptionEventListener, EventsService],
      exports: [SubscriptionService],
    };
  }

  static forTesting(): DynamicModule {
    return {
      module: SubscriptionModule,
      imports: [PaymentModule.forTesting()],
      providers: [SubscriptionService, SubscriptionDbService, SubscriptionEventListener, { provide: EventsService, useValue: { broadcast: jest.fn() } }],
      exports: [SubscriptionService],
    };
  }
}

Pattern Adoption Order

When refactoring an existing codebase, adopt patterns in this order:

  1. Domain Service Layering first -- separate controllers from business logic and data access. This is the foundation that makes everything else possible.
  2. Service Orchestration second -- once layering is in place, decompose large services into focused units with an orchestrator.
  3. Retry and Fallback third -- wrap external calls with resilience. This is independent of the others and can be adopted at any time.
  4. Event-Driven Architecture fourth -- extract side-effects into event listeners. Requires clean service boundaries to know what events to emit.
  5. Dynamic Modules fifth -- formalize module configuration once the module structure has stabilized.

Do not adopt all five at once. Each pattern should be justified by a real problem in the current codebase.

Anti-Patterns to Avoid

Anti-PatternProblemCorrect Pattern
Fat controllerBusiness logic in controller methodsDomain Service Layering
God serviceSingle service with 20+ methods across concernsService Orchestration
Silent failureExternal API errors swallowed with empty catchRetry and Fallback
Synchronous side-effectsEmail sending blocks HTTP responseEvent-Driven Architecture
Copy-paste module configSame providers array in 5 modulesDynamic Modules
Premature decompositionSplitting a 100-line service into 5 filesDo nothing (YAGNI)

References

Load these for detailed implementation guidance:

  • service-orchestration.md - Decomposing complex services into focused units with orchestrators
  • domain-service-layering.md - Controller, Service, DbService separation pattern
  • retry-and-fallback.md - Exponential backoff, fallback strategies, circuit breakers
  • event-driven-architecture.md - NestJS EventEmitter, listeners, and SSE broadcasting
  • dynamic-modules.md - forRoot/forTesting, environment-driven providers, DI tokens

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.