projection
@rotorsoft/act-root / act/src / projection
Function: projection()
projection<
TEvents>(target?,events?):ProjectionBuilder<TEvents>
Defined in: libs/act/src/projection-builder.ts:136
Creates a new projection builder for composing read-model update handlers.
Projections enable separation of read-model concerns from command handling.
Each .on({ Event }).do(handler) call registers a handler that updates
a projection (database table, cache, etc.) in response to events.
Pass a target stream name to projection("target") so every handler
inherits that resolver automatically. Omit it and use per-handler
.to() / .void() when handlers route to different streams.
Type Parameters
TEvents
TEvents extends Schemas = { }
Parameters
target?
string
Optional default target stream for all handlers
events?
EventRegister<TEvents> = ...
Returns
ProjectionBuilder<TEvents>
Examples
const TicketProjection = projection("tickets")
.on({ TicketOpened })
.do(async ({ stream, data }) => {
await db.insert(tickets).values({ id: stream, ...data });
})
.on({ TicketClosed })
.do(async ({ stream, data }) => {
await db.update(tickets).set(data).where(eq(tickets.id, stream));
})
.build();
const MultiProjection = projection()
.on({ OrderPlaced })
.do(async (event) => { ... })
.to("orders")
.on({ PaymentReceived })
.do(async (event) => { ... })
.to("payments")
.build();
See
- ProjectionBuilder for builder methods
- Projection for the output type