Events
A mutation emits an event, and its listeners run after commit.
An event records something that happened in a mutation so listeners can react to it.
export const const confirmed: EventDef<{
bookingId: string;
}, {
bookingId: string;
}, undefined, z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>>
confirmed = const event: <z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>, undefined>(def: {
id?: undefined;
payload: z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>;
}) => EventDef<{
bookingId: string;
}, {
bookingId: string;
}, undefined, z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>>
Something that happened. Emitted by mutations, delivered after commit.event({ payload: z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>
payload: import zz.function object<{
bookingId: z.ZodUUID;
}>(shape?: {
bookingId: z.ZodUUID;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>
object({ bookingId: z.ZodUUIDbookingId: import zz.function uuid(params?: string | z.core.$ZodUUIDParams): z.ZodUUIDuuid() }) });
export const const confirm: Fn<"mutation", {
bookingId: string;
}, {
id: string;
}, false, {
scope: Scope;
services: Services;
}, z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>, undefined>
confirm = const mutation: <z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>, undefined, {
id: string;
}, Policy<Scope>, never, undefined>(def: Def<{
scope: Scope;
services: Services;
}, "mutation", z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>, undefined, {
id: string;
}, Policy<Scope>, never, undefined>) => Fn<"mutation", {
bookingId: string;
}, {
id: string;
}, false, {
scope: Scope;
services: Services;
}, z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>, undefined>
Changes data inside one transaction. Can run queries and mutations (same transaction) and emit events.mutation({
input?: z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip> | undefined
What callers pass, validated on every call. Leave it out when the function takes nothing.input: import zz.function object<{
bookingId: z.ZodUUID;
}>(shape?: {
bookingId: z.ZodUUID;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>
object({ bookingId: z.ZodUUIDbookingId: import zz.function uuid(params?: string | z.core.$ZodUUIDParams): z.ZodUUIDuuid() }),
async handler: (ctx: MutationCtx<{
scope: Scope;
services: Services;
}, Scope>, input: {
bookingId: string;
}) => Promise<{
id: string;
}>
handler(ctx: MutationCtx<{
scope: Scope;
services: Services;
}, Scope>
ctx, { bookingId: stringbookingId }) {
const const booking: {
id: string;
}
booking = await function confirmBooking(ctx: unknown, bookingId: string): Promise<{
id: string;
}>
confirmBooking(ctx: MutationCtx<{
scope: Scope;
services: Services;
}, Scope>
ctx, bookingId: stringbookingId);
ctx: MutationCtx<{
scope: Scope;
services: Services;
}, Scope>
ctx.MutationCtx<{ scope: Scope; services: Services; }, Scope>.emit<{
bookingId: string;
}>(event: EventDef<{
bookingId: string;
}, unknown, string | undefined, Schema<any, any>>, payload: {
bookingId: string;
}): EmittedEvent<{
bookingId: string;
}>
Queue an event. Delivered to listeners only after the transaction commits. Returns what it queued: the id, name and
time its listeners see as `ctx.event`, so a row you write in this transaction can carry `event.id` and a listener
finds it by `ctx.event.id`.emit(const confirmed: EventDef<{
bookingId: string;
}, {
bookingId: string;
}, undefined, z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>>
confirmed, { bookingId: stringbookingId: const booking: {
id: string;
}
booking.id: stringid });
return const booking: {
id: string;
}
booking;
},
});
export const const sendConfirmation: Listener<{
bookingId: string;
}, {
scope: Scope;
services: Services;
}>
sendConfirmation = const listener: <{
bookingId: string;
}>(def: {
id?: string;
event: () => EventDef<unknown, {
bookingId: string;
}, string | undefined, Schema<any, any>>;
attempts?: number;
delay?: number;
when?: ((payload: {
bookingId: string;
}) => boolean) | undefined;
key?: ((payload: {
bookingId: string;
}) => string) | undefined;
unique?: ((payload: {
bookingId: string;
}) => string) | undefined;
scope?: ((payload: {
bookingId: string;
}) => Scope) | undefined;
timeout?: number;
queue?: string;
meta?: undefined;
handler: (ctx: ListenerCtx<{
scope: Scope;
services: Services;
}, {
bookingId: string;
}>, payload: {
bookingId: string;
}) => Promise<unknown>;
retry?: (error: AppError) => boolean;
failed?: ((ctx: ListenerCtx<...>, payload: {
bookingId: string;
}, error: unknown) => Promise<unknown>) | undefined;
}) => Listener<...>
React to an event after commit, like an action. Must be idempotent.
`listener({ event: () => bookings.confirmed, async handler(ctx, payload) {…} })`listener({
event: () => EventDef<unknown, {
bookingId: string;
}, string | undefined, Schema<any, any>>
The event it reacts to, as a thunk so modules can import each other's barrels without cycles.event: () => const confirmed: EventDef<{
bookingId: string;
}, {
bookingId: string;
}, undefined, z.ZodObject<{
bookingId: z.ZodUUID;
}, z.core.$strip>>
confirmed,
async handler: (ctx: ListenerCtx<{
scope: Scope;
services: Services;
}, {
bookingId: string;
}>, payload: {
bookingId: string;
}) => Promise<unknown>
handler(ctx: ListenerCtx<{
scope: Scope;
services: Services;
}, {
bookingId: string;
}>
ctx, { bookingId: stringbookingId }) {
await ctx: ListenerCtx<{
scope: Scope;
services: Services;
}, {
bookingId: string;
}>
ctx.ActionCtx<{ scope: Scope; services: Services; }, Scope>.services: Servicesservices.mailer: {
sendConfirmation: (id: string) => Promise<void>;
}
mailer.sendConfirmation: (id: string) => Promise<void>sendConfirmation(bookingId: stringbookingId);
},
});
- Emit (1). Pass plain JSON.
ctx.emittakes a snapshot that the registry validates before commit. - Prepare (2). The registry makes a job per matching listener. See
when. - Deliver (3). After commit, the listener gets the parsed payload. Each mutation it runs has its own transaction.
- Registration. Export both definitions. Use
event: () => confirmedto defer the reference. - Idempotency. Record processed
ctx.event.idvalues per listener. All listeners of one event share its ID. - Failures. Production reports listener failures to
onError. The call still succeeds. Inline tests make it throw.
Event records
ctx.emit returns { id, name, payload, occurredAt }. Save the ID in the mutation to match a row to the event.
EventOf<typeof registry> uses unparsed payload types. Reuse the event’s payload schema to check stored rows. Upgrade old payloads in your app.
Serverless
Set waitUntil on the registry to continue delivery after the response. See Frameworks.
Durability and loops
Use a durable adapter to keep jobs if the process crashes after commit.
jobs.maxEventDepth defaults to eight hops. Events and due-now calls count. Delayed calls start a new chain. The registry reports and skips work past the limit.