Skip to main content
Version: Current

projection

@rotorsoft/act-root


@rotorsoft/act-root / act/src / projection

Function: projection()

Call Signature​

projection<TEvents>(target): ProjectionBuilder<TEvents, string>

Defined in: libs/act/src/builders/projection-builder.ts:210

Creates a new projection builder for composing read-model update handlers, with a static target stream.

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. All handlers inherit the target resolver automatically, and .batch() is enabled for bulk event processing in a single transaction. Omit the target and use per-handler .to() when handlers route to different streams.

Type Parameters​

TEvents​

TEvents extends Schemas = { }

Parameters​

target​

string

Static target stream for all handlers

Returns​

ProjectionBuilder<TEvents, string>

Example​

Default target (all handlers routed to "tickets")

const TicketProjection = projection("tickets")
.on({ TicketOpened })
.do(async function openTicket({ stream, data }) {
await db.insert(tickets).values({ id: stream, ...data });
})
.on({ TicketClosed })
.do(async function closeTicket({ stream, data }) {
await db.update(tickets).set(data).where(eq(tickets.id, stream));
})
.build();

See​

Call Signature​

projection<TEvents>(target?): object

Defined in: libs/act/src/builders/projection-builder.ts:218

Creates a new projection builder without a default target.

Use per-handler .to() to route events to different streams.

Type Parameters​

TEvents​

TEvents extends Schemas = { }

Parameters​

target?​

undefined

Returns​

build​

build: () => Projection<TEvents>

Builds and returns the Projection data structure.

Returns​

Projection<TEvents>

events​

readonly events: EventRegister<TEvents>

The registered event schemas and their reaction maps.

on​

on: <TKey, TData>(entry) => object

Begins defining a projection handler for a specific event.

Pass a { EventName: schema } record — use shorthand { EventName } when the variable name matches the event name. The key becomes the event name, the value the Zod schema.

Type Parameters​
TKey​

TKey extends string

TData​

TData extends Schema

Parameters​
entry​

EventEntry<TKey, TData>

Returns​

object

do​

do: (handler) => DoResult<TEvents, TKey, TData, undefined>

Parameters​
handler​

(event, stream) => Promise<void>

Returns​

DoResult<TEvents, TKey, TData, undefined>