AGENT SKILL

ts-ddd-events

ACTIVATE when implementing domain events, event store, outbox pattern, or reliable event publishing in TypeScript. ACTIVATE for 'domain event', 'outbox', 'event store', 'event-driven', 'reliable publishing'. Covers: domain event structure and naming, event store (append-only), outbox pattern for reliable publishing (no dual-write), consumer pattern. DO NOT use for: aggregate modeling (see ddd-ts-fp), general async patterns, message queue configuration.

License 许可
MIT
最近更新
2026-10-13
Stars
★ 9
Forks
2

01 前言 Front Matter

name
ts-ddd-events
description
ACTIVATE when implementing domain events, event store, outbox pattern, or reliable event publishing in TypeScript. ACTIVATE for 'domain event', 'outbox', 'event store', 'event-driven', 'reliable publishing'. Covers: domain event structure and naming, event store (append-only), outbox pattern for reliable publishing (no dual-write), consumer pattern. DO NOT use for: aggregate modeling (see ddd-ts-fp), general async patterns, message queue configuration.

02 资源文件 6

03 SKILL.md 正文

Domain Events & Event Publishing

Domain Event

A business fact that occurred in the past. Named in past tense, immutable:

type ReceiptGenerated = {
  readonly receiptId: string;
  readonly tenantId: string;
  readonly landlordId: string;
  readonly period: { year: number; month: number };
  readonly amount: number;
  readonly generatedAt: Date;
};

Naming Conventions

Element Convention Example
Type PascalCase past tense ReceiptGenerated, LeaseTerminated
File kebab-case.event.ts receipt-generated.event.ts
Directory domain/events/ Co-located with the aggregate

Event Anatomy

type DomainEvent<T> = {
  readonly id: string;           // UUID unique
  readonly type: string;         // 'receipt.generated'
  readonly payload: T;
  readonly createdAt: Date;
};

Event Store

Persist events as source of truth. Append-only, never delete.

Outbox Pattern

Guarantees reliable event publishing. The event is persisted in an outbox table atomically with the state change, then published asynchronously:

Command -> Domain Logic -> Transaction {
  1. Persist aggregate state
  2. Insert event in outbox table  <- atomic
}

Worker -> Poll outbox -> Publish event -> Mark as processed

Status flow: created -> in_progress -> processed / failed

When implementing event store persistence, outbox tables, or outbox publishers, read references/event-implementation-patterns.md for complete Drizzle schemas and publisher implementations.

When emitting events from handlers or writing consumers, read references/event-implementation-patterns.md for atomic transaction patterns and consumer examples.

When to Use What

Pattern Use case
Domain Event Business fact to communicate between bounded contexts
Event Store Audit trail, replay, event sourcing
Outbox Reliable publishing without dual-write
Consumer Asynchronous reaction to an event

Event, Outbox & CQRS Conventions

  1. Two vocabularies of event must never be confused: the transport envelope Event<T> (id/type/timestamp/version/metadata/data) and the domain event DomainEvent<U> (id/type/payload/createdAt). No file imports both. Measure: zero files import both a type named Event and a type named DomainEvent.
  2. The event is created in the handler, never in the aggregate: without a class, the aggregate cannot carry an event buffer. Measure: zero methods on a domain/ aggregate file are named emit* or raise*.
  3. Dispatch is a domain port typed as a bare function type, whose adapter is a Record<type, listener> table built by a make*Mapper factory. Cost: one make*Mapper factory per bounded context, never one per event type.
  4. Emit either by returning the event in Result's success, or by calling the injected dispatcher: both are used, with no rule between them. Measure: a handler uses exactly one of the two mechanisms; none returns the event in success and also calls the dispatcher.
  5. This outbox is INBOUND: it stages messages received from SQS for local consumption. It publishes nothing, so it is not the reliable publishing pattern. Cost: naming it outbox without this note costs a reader the reliable-publishing assumption; the note is the one-line fix.
  6. The read model is not fed by events: both sides share one database and collection, so a write is immediately visible on read. Measure: zero event listeners write to the read collection; only the write side's own handlers do.
  7. Only one codebase forbids Command importing Query by lint. Neither violates the rule, but the read side freely reaches into the write side. Measure: exactly one of the two codebases declares a Command-cannot-import-Query ESLint zone; the other declares none.
  8. Domain purity holds even where CQRS does not: a single exception, a concrete logger, in the gap the zones do not cover. Measure: exactly one concrete infrastructure import, the logger, crosses the domain boundary.

Quick Reference

Rule Convention
Naming Past tense (ReceiptGenerated)
Structure { id, type, payload, createdAt }
Event Store Append-only, domain_events table
Outbox Atomic with state change
Status created -> in_progress -> processed / failed
Retries numberOfRetries + maxNumberOfRetries
Consumer Idempotent, null-safe

04 同仓库更多 Skills

FabienSalles
FabienSalles/claude-marketplace

vitest-test-conventions

ACTIVATE when writing Vitest tests, creating test files, using vi.fn()/vi.mock()/vi.spyOn(), or test factories. ACTIVATE for 'Vitest', 'it.each', 'NestJS testing module', 'supertest', 'vi.fn', 'vi.mock', 'spyOn'. Provides Vitest/NestJS-specific testing patterns; cross-language testing principles (DAMP, AAA, spy over mock, what NOT to test) live in craft:testing-principles. DO NOT use for: node:test files (a file importing node:test) or a project whose tests run under node --test (see node-test:node-test-conventions), TDD workflow/iterations (see vitest:vitest-tdd-workflow), PHP/PHPUnit tests (see phpunit:php-test-conventions).

测试与质量
★ 9
FabienSalles
FabienSalles/claude-marketplace

vitest-tdd-workflow

ACTIVATE when building new features, endpoints, or user stories using TDD with Vitest in a TypeScript/NestJS context. ACTIVATE for '/feature-tdd-dev', 'TDD', 'red-green-refactor' in TS context. Provides Vitest/NestJS-specific TDD examples and commands; cross-language TDD workflow (cross-layer iterations, RED-GREEN-REFACTOR, working app, bug-fix-first-test, mocks-hiding-bugs) lives in craft:tdd-workflow-principles. DO NOT use for: node:test files (a file importing node:test) or a project whose tests run under node --test (see node-test:node-test-conventions), test writing conventions (see vitest:vitest-test-conventions), PHP TDD (see phpunit:php-tdd-workflow).

Skill与智能体开发
★ 9
FabienSalles
FabienSalles/claude-marketplace

ts-refactoring

ACTIVATE when refactoring TypeScript code. ACTIVATE for 'refactor', 'extract', 'redesign', 'simplify', or 'clean up' in a TypeScript / Node / NestJS context. This skill provides TS-specific examples for the cross-language refactoring rules defined in craft:refactoring-principles. DO NOT use for: writing new features from scratch, general OOP patterns (see ts-oop).

★ 9
FabienSalles
FabienSalles/claude-marketplace

ts-ports-adapters

ACTIVATE when a TypeScript codebase with no dependency-injection container has to declare a port and wire its adapter — a repository, a clock, an id generator, an event dispatcher the domain depends on. ACTIVATE for 'port', 'adapter', 'SPI', 'dependency inversion' or 'composition root' in a codebase that wires its dependencies by hand. Covers: the port as a type of functions in the domain layer, the adapter as an object literal annotated with it, the handler that takes its dependencies first and its data second, the composition root in the infrastructure layer, and the in-memory stub that substitutes for the adapter in a unit test. DO NOT use for: NestJS, or any container-based DI with decorators and tokens (see nest-ddd-conventions), aggregate operations and smart constructors (see ddd-ts-fp), the Result type itself (see ts-functional).

测试与质量
★ 9
FabienSalles
FabienSalles/claude-marketplace

ts-oop

ACTIVATE when designing TypeScript classes, and the value objects or collections modelled as classes, or when the user asks about object design, encapsulation, Tell Don't Ask, or Symbol.iterator. Provides TS-specific examples for the cross-language OOP rules defined in craft:oop-principles, plus TS-specific patterns (Symbol.iterator, branded types for primitive identifiers). DO NOT use for: a value object or an aggregate modelled as an immutable record rather than a class (see ddd-ts-fp), refactoring methodology (see ts-refactoring).

★ 9
FabienSalles
FabienSalles/claude-marketplace

ts-layer-boundaries

ACTIVATE when wiring a Worker entry point, a Router, a controller, or a domain event consumer in a TypeScript CQRS codebase, or when deciding where a port belongs. ACTIVATE for 'composition root', 'layer boundary', 'ESLint zones', 'domain must not import infrastructure'. Covers: the *Worker.ts composition root, the curried consumer, Router wiring of command endpoints, the five-gesture controller, the domain-to-infrastructure import boundary enforced by ESLint, infrastructure-only ports, in-memory doubles for every port, the read-can-reach-write asymmetry, and read endpoints going through a domain handler. DO NOT use for: the port's own shape and adapter binding (see ts-ports-adapters), the Result type (see ts-functional), domain event structure (see ts-ddd-events).

★ 9