/bett3r-pv3-ai-skills:create-aggregatefrom bett3r-pv3-ai-skills
Scaffold a new event-sourced aggregate with PV3 AggregateBuilder pattern. Use when adding a new aggregate to handle commands and emit events.
Ships in bett3r-pv3-ai-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Scaffold an event-sourced aggregate using PV3's AggregateBuilder.
Read ddd-patterns → AGGREGATES.md before writing the file. It is this skill's reference half: command-handler options, idempotency.check semantics, transactional side-writes and the UNIQUE-constraint lock, event-namespace coverage, system/admin-only and tenant-but-not-row-scoped aggregates, lifecycle status guards, and the idempotency-predicate null-safety bug. Changing an existing aggregate's fields also means SCHEMAS.md → Renaming a persisted field.
What the scaffolder does and does not do here. A new aggregate on the board is generated
as files: <serverPath>/src/modules/<subdomain>/<stem>.aggregate.ts
(AggregateBuilder( <Stem>AggregateSchema, <Stem>Events )), its domain files <stem>.events.ts,
<stem>.types.ts and <stem>-integration.types.ts with placeholder schemas marked
TODO(scaffold), and one guarded export line per file at the end of the subdomain's domain barrel.
The commands and events the design gives it are rendered into those files in the same run: a
commandBuilder() entry per command, an Event({…}) and a stub reducer per event. A new subdomain
also gets its domain barrel and module index.ts. scopeInvariant() is applied only when
domainUtilsPackageName is declared. A label without Aggregate, in a repo whose aggregates all
carry it, is blocked with the rename. A new command on an aggregate that already exists is a
fragment: the commandBuilder() block with its .withSchema(...) and .produces([...]), for you to
place inside .withCommands({…}). In both cases it deliberately does not guess the part that
matters most: the reducer bodies (each stub keeps the state unchanged), the handler body, the
invariants, the idempotency check, and any stream override. A generated command declaration that
emits its event unconditionally is correct only for the simplest case; read
AGGREGATES.md before assuming yours is one.
When the scaffold already wrote the aggregate, its domain files and the barrel line (the prose above), skip generating them. Keep every export name, label-derived name and subdomain it
wrote: they are id-exact, the identity the extractor reads back. The module folder src/modules/<subdomain>/ is the subdomain, so it is id-exact too: keep it. Work only the TODO(scaffold) markers and
the file's STILL OWED block, and delete each marker as you satisfy it. Placing a command fragment is the same: the block it emitted keeps its names. The rest of this
skill still governs the content you write and the Final Checklist.
Resolve these placeholders from the .blueprint.config.json at your repo root:
| Placeholder | .blueprint.config.json field |
|---|---|
<serverPath> | serverPath |
<domainEventsPackageName> | domainEventsPackageName |
<domainUtilsPackageName> | domainUtilsPackageName (optional; defaults to <domainEventsPackageName> + -utils) |
The framework packages @bett3r-dev/pv3-types, @bett3r-dev/jsonschema-definer, and the
ports module are PV3 framework — identical in every PV3 repo — and appear verbatim below.
import { AggregateBuilder, idempotency } from '@bett3r-dev/pv3';
import { MyEvents, MySchema, CreateCommandSchema, EditCommandSchema } from '<domainEventsPackageName>';
import { scopeInvariant } from '<domainUtilsPackageName>';
import { Ports } from 'ports';
export const MyAggregate = ( ports: Ports ) => {
return AggregateBuilder( MySchema, MyEvents )
.withCommandTemplate({ invariants: [scopeInvariant()] } as any)
.withEventReducers({
EntityCreated: ( _, event, metadata ) => ({ ...event, ownerId: metadata?.userId }),
EntityEdited: ( state, event ) => ({ ...state, ...event }),
EntityStatusChanged: ( state, event ) => ({ ...state, ...event })
})
.withCommands(({ commandBuilder }) => ({
CreateEntity: commandBuilder()
.withSchema( CreateCommandSchema )
.produces([ 'EntityCreated' ])
.withHandler( async( createEvent, state, data, { context }) => {
const accountId = data.accountId || context?.user?.accountId;
return [createEvent( 'EntityCreated', data, { metadata: { accountId }})];
}),
EditEntity: commandBuilder()
.withSchema( EditCommandSchema )
.produces([ 'EntityEdited' ])
.withHandler( async( createEvent, state, data, { context }) => {
const accountId = context?.user?.accountId;
return [createEvent( 'EntityEdited', data, { metadata: { accountId }})];
}),
ChangeStatus: commandBuilder()
.withSchema( ChangeStatusCommandSchema )
.produces([ 'EntityStatusChanged' ])
.withHandler( async( createEvent, state, data, { context }) => {
const accountId = context?.user?.accountId;
return [createEvent( 'EntityStatusChanged', {
status: data.status,
previousStatus: state?.status
}, { metadata: { accountId }})];
})
}));
};
<serverPath>/src/modules/<module-name>/<entity>.aggregate.ts
Just ONE file. No controller, no module, no service wrapper.
In the module's index.ts:
ports.eventsourcing.routeCommandHandler( MyAggregate( ports ));
When the command simply passes data through to the event:
AssignCategory: commandBuilder()
.withSchema( CategoryAssignedEventSchema )
.produces([ 'CategoryAssigned' ])
No .withHandler() — PV3 creates the event with data as payload automatically.
UpdateEntity: commandBuilder()
.withSchema( UpdateCommandSchema )
.withInvariant( invariants.stateExists() )
.withIdempotencyCheck( idempotency.check() )
.produces([ 'EntityUpdated' ])
.withHandler( async( createEvent, state, data, { context }) => {
return [createEvent( 'EntityUpdated', data, { metadata: { accountId: context?.user?.accountId }})];
})
withIdempotencyCheck( idempotency.check( value1Fn, op, value2Fn? ) ) — declarative idempotency that skips the command silently (no event emitted) when the check passes. Prefer this over inline handler checks.
// State exists — skip if aggregate already has state
.withIdempotencyCheck( idempotency.check(( state ) => state, 'exists' ))
// Value equality — skip if current value already matches target
.withIdempotencyCheck( idempotency.check(
({ state, data }) => state.warehouses[data.warehouseId]?.stock || 0,
'eq',
({ data }) => data.stock
))
Operators: 'exists' (value1 is truthy), 'eq' (value1 === value2)
Multi-stream exception: withIdempotencyCheck is all-or-nothing — it can't make per-item decisions. For multiStream commands, use inline checks in the handler instead:
SetMultipleItems: commandBuilder( multiStream( ports, 'itemId' ))
.withHandler( async ( createEvent, state, data ) => {
return data.flatMap(({ itemId, value }) => {
const current = ( state || {} )[`Stream-${itemId}`];
if ( current?.value === value ) return []; // per-item idempotency
return [createEvent( 'ItemSet', { value }, { stream: `Stream-${itemId}` })];
});
})
For spreadsheet-style bulk save flows, follow the unified pattern — N per-row events + 1 *BulkOperationCompleted event emitted in one event-store transaction, projector reacts only to the completion event:
ApplyBulkItemsChange: commandBuilder( multiStream( ports, 'itemId' ))
.withSchema( ApplyBulkItemsChangeCommandSchema ) // schema-cap maxItems: 2000
.produces([ 'ItemRegistered', 'ItemEdited', 'ItemDeleted', 'ItemsBulkOperationCompleted' ])
.withHandler( async ( createEvent, state, data, { context, metadata }) => {
const accountId = context?.user?.accountId;
const correlationId = metadata?.correlationId;
const events = data.rows.flatMap(({ itemId, intent, payload }) => {
const stream = `Items-${itemId}`;
// per-row branching using state[stream] for register vs edit, etc.
return [createEvent( intentToEventName( intent ), payload, { stream, metadata: { accountId }})];
});
// completion event on a per-correlation stream — single trigger for the projector
events.push( createEvent( 'ItemsBulkOperationCompleted', { count: data.rows.length, accountId }, {
stream: `ItemsBulkOperationCompleted-${correlationId}`,
metadata: { accountId, correlationId }
}));
return events;
})
Pair with a ReducerNOOP entry for the completion event (it lands on a per-correlation stream the aggregate doesn't reduce — see "Event namespace coverage" note (2) in ddd-patterns → AGGREGATES.md).
Two multi-stream shapes — pick by what state the handler needs:
commandBuilder( multiStream( ports, 'itemId' )) (e.g. items, stock). PV3 pre-loads N per-row snapshots (one batched query) because the handler diffs each row against its own per-row state (state[Stream-${itemId}]). Use when per-row state drives the emitted events..withCommitFunction( multiStreamCommit ) (e.g. a bulk change whose decision needs shared parent metadata such as a currency or locale). Keep PV3's default aggregate load (one cheap parent-stream read, e.g. Parent-${id}, for shared metadata + ownership) and override only the commit so per-event stream overrides survive into the event store. Use when the bulk decision needs parent metadata, not per-row state.ApplyBulkChildChange: commandBuilder()
.withSchema( ApplyBulkChildChangeCommandSchema ) // maxItems: 2000
.withInvariant( scopeInvariant() as any )
.withInvariant( invariants.stateExists()) // real now — state is loaded
.withCommitFunction( multiStreamCommit ) // overrides commit only; default load preserved
.produces([ 'ChildItemChanged', 'ChildItemRemoved', 'BulkOperationCompleted' ])
.withHandler( async ( createEvent, state, data, { metadata }) => {
const currencyIsoCode = state!.currency; // strongly-consistent parent metadata
// …emit per-row events on `${id}::${itemId}` streams + one completion event…
})
multiStreamCommit is a ~30-line function (group events by per-event stream, appendToMultipleStreams( streams, handlerState.transaction )); copy it from an existing parent-metadata bulk aggregate in your repo. It composes with the default load because PV3 ignores withLoadFunction for aggregates — only the commit is swapped.scopeInvariant()/stateExists() to be real (not stateless no-ops), the creation reducer must project the scoping fields onto state, e.g. ParentCreated: ( _, event, metadata ) => ({ ...event, ownerId: metadata?.userId, accountId: metadata?.accountId }) — the aggregate state schema declares them optional.executeCommand( Aggregate( ports ), 'ApplyBulkChildChange', tx ) — multiStreamCommit honors handlerState.transaction.A keyed recorder (RecorderBuilder( events, { keyed: true } ) in a .recorder.ts file) is a possible substrate for bulk, but is usually the wrong choice. Prefer aggregate-side commands for bulk editors. Reach for a keyed recorder only for a genuinely stateless bulk case with no foreseeable invariants — but note that the "zero state-load" advantage is moot for bulk (one metadata load, not N per row) and is outweighed the moment any consumer needs read-your-writes state. Promotion trap (pv3 ADR-036): a recorder writes only version-free streams, and turning it into an aggregate is silent — pv3 neither refuses nor notices — so a recorder that gains a rule needs its history migrated (unkeyed) or re-validated (keyed) by hand; if a rule may ever appear, start as an aggregate.
PlaceOrder: commandBuilder()
.withSchema( PlaceOrderCommandSchema )
.produces([ 'OrderPlaced', 'OrderEdited', 'OrderStatusChanged' ])
.withHandler( async( createEvent, state, data, { context }) => {
const events: any[] = [];
const accountId = data.accountId || context?.user?.accountId;
if ( state ) {
// Existing entity — emit edit events
const diff = deepDiff( state, data );
if ( Object.keys( diff ).length > 0 ) {
events.push( createEvent( 'OrderEdited', diff, { metadata: { accountId }}));
}
} else {
// New entity — emit created event
events.push( createEvent( 'OrderPlaced', data, { metadata: { accountId }}));
}
return events;
})
Command handlers receive a transaction parameter (4th arg) for ACID-transactional side-writes alongside event emission:
DoSomething: commandBuilder()
.withSchema( DoSomethingCommandSchema )
.produces([ 'SomethingDone' ])
.withHandler( async( createEvent, state, data, { context, transaction }) => {
// Atomic side-write — rolls back if events roll back
const col = transaction.getCollection( 'my_unique_constraints' );
await col.upsert( data.email, { entityId: data.id });
return [createEvent( 'SomethingDone', data, { metadata: { accountId: context?.user?.accountId }})];
})
When to use: Uniqueness constraints, counters, scheduling state — any data requiring atomicity with the event store (not eventual consistency via readmodel).
Reference: Look at an existing aggregate with transactional side-writes under <serverPath>/src/modules/ in your repo — e.g. a unique-email constraint, a scheduling nextRunAt side-write, or a monotonic sequence counter emitted via atomicIncrement on a dedicated counter collection.
Anti-pattern: Redis INCR for monotonic document numbers. A Redis counter is NOT transactional with the event store — under appendfsync everysec a Redis crash can regress the counter and reissue an already-used number. Gaps (a discarded draft burning a number) are tolerable; duplicates are not. For any "must-be-unique" sequence (e.g. document numbers, fiscal/legal numbering, receipt numbers), use the transactional DB counter pattern above. Counter increment lives in the handler that emits the consumption event (the one that finalizes/publishes the document, not the one that creates a draft) so discarded intermediates don't burn numbers.
All user-facing aggregates MUST include scope enforcement:
import { scopeInvariant } from '<domainUtilsPackageName>';
AggregateBuilder( MySchema, MyEvents )
.withCommandTemplate({ invariants: [scopeInvariant()] } as any)
This applies scopeInvariant() to ALL commands in the aggregate. It reads context.user.permissionScopes — a SET of ScopeLevels — and OR-s the per-scope predicates against the aggregate state. The command passes if any granted scope authorizes it; throws ForbiddenError only when every scope in the set fails.
ownerIdField='ownerId'context.user is missing (system/policy calls)When to skip: Only skip for internal/system aggregates (auth sync, global config, reference data).
Creation event reducers MUST capture ownerId from event metadata:
EntityCreated: ( _, event, metadata ) => ({ ...event, ownerId: metadata?.userId }),
Note the third metadata parameter in the reducer — this is how PV3 passes event metadata to reducers.
For aggregates that already have .withCommandTemplate( snapshot(...) ), merge the invariants:
.withCommandTemplate({ ...snapshot( ports, collection, N ), invariants: [scopeInvariant()] })
Reducers transform state based on events:
| Pattern | Use When |
|---|---|
( _, event, metadata ) => ({ ...event, ownerId: metadata?.userId }) | Creation event (captures owner) |
( state, event ) => ({ ...state, ...event }) | Merge event data into state |
deepMerge | Deep merge (import from @bett3r-dev/bett3r-utils) |
( state ) => state | Event doesn't change state (side-effect only) |
.build() call — PV3 AggregateBuilder doesn't need it.withStreamIdPrefix() — PV3 handles this differently.withInitialState() — PV3 starts with null by default.withDependencies() — Use ports closure instead@AggregateController, no *.controller.ts@Module, no *.module.ts*.service.ts wrapping the aggregate[createEvent(...)], even for single eventsmetadata: { accountId } for multi-tenant trackingimport { AggregateBuilder, idempotency } from '@bett3r-dev/pv3';
import { scopeInvariant } from '<domainUtilsPackageName>';
import { omit } from 'ramda';
import { deepDiff, deepMerge } from '@bett3r-dev/bett3r-utils';
Aggregate and command schemas are defined in the domain package (<domainEventsPackageName>, in *-integration.types.ts files):
// CORRECT — import directly from the domain package in server modules
import { MyAggregateSchema } from '<domainEventsPackageName>';
Look at an existing aggregate under <serverPath>/src/modules/ in your repo for a working reference — including one with multi-stream / bulk commands if you are scaffolding a bulk editor.
<entity>.aggregate.tsAggregateBuilder( Schema, Events ) pattern( ports: Ports ) — never a second constructor argument. The MDU/lift loader instantiates every artifact as factory( ports ); an injected arg is undefined in any lifted deployment unit. Single-consumer infra → a lazy library singleton reached via getX( ports ), never the global Ports type. See the ddd-patterns skill → "Artifact Constructor Signature — the MDU/Lift Contract"..withCommandTemplate({ invariants: [scopeInvariant()] }) applied (unless internal/system aggregate)ownerId: metadata?.userIdcommandBuilder() API.produces([]) lists all possible eventsaccountId.build(), no controller, no module, no servicerouteCommandHandler() in index.tsWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub bett3r-dev/bett3r-ai-workflow --plugin bett3r-pv3-ai-skills11 skills in this plugin
/create-integration-testSkill: Create Integration TestScaffold a full-pipeline in-process integration test suite for a server module using createIntegrationHarness. Use when you want to exercise the real command → aggregate → eventstore → outbox → readmodel pipeline for a subdomain (choreography, policies, gateway callbacks), not mocked unit tests./create-moduleSkill: Create ModuleOrchestrate complete DDD module creation including schemas, aggregate, policies, read models, and tests. Use when scaffolding a new bounded context or feature module./create-