Skip to content

Publishes events to other systems, such as Kafka, NATS or SQS, as integration events.

const TodoOutbox = Outbox.make({
name: "todo-outbox",
aggregates: [Todo],
messages: {
TodoCreated: ({ title }) => ({ type: "todo.created.v1", payload: { title } })
},
publish: (messages) => producer.send(messages)
})
const Publishing = Outbox.layer([TodoOutbox])

An outbox is a view model: it reads the store from its checkpoint, so it publishes what was stored while the process was down, and nothing is lost between storing an event and publishing it. Delivery is at least once. After a crash, events published since the last checkpoint are published again, so consumers deduplicate on Message.id, which stays the same.

Messages are published in batches of up to batchSize. A batch goes out when it is full or linger after its first message, and the checkpoint never moves past a message that wasn’t published. A failed batch is retried, in order, until it succeeds.

Defined in: src/Outbox.ts:83

Publishing failed, which the outbox retries.

  • object & YieldableError<this>

new OutboxError(…args): OutboxError

Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Schema.d.ts:10979

…[props: object, options?: MakeOptions]

OutboxError

Schema.TaggedError<OutboxError>()("OutboxError", { outbox: Schema.String, cause: Schema.Defect() }).constructor

cause: unknown

Defined in: src/Outbox.ts:85

Schema.TaggedError<OutboxError>()("OutboxError", { outbox: Schema.String, cause: Schema.Defect() }).cause

readonly outbox: string = Schema.String

Defined in: src/Outbox.ts:84

Schema.TaggedError<OutboxError>()("OutboxError", { outbox: Schema.String, cause: Schema.Defect() }).outbox


Defined in: src/Outbox.ts:142

new Outboxes(_): Outboxes

Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Context.d.ts:121

never

Outboxes

Context.Service<Outboxes, { /** * Waits until every event up to `position`, by default the latest, is * published. Mostly for tests. */ published(position?: number): Effect.Effect<void, EventStoreError | OutboxError> /** Where each outbox is, and the failure it is retrying, see `ViewModel.Status`. */ readonly status: Effect.Effect<ReadonlyArray<ViewModel.Status>> }>()(“effect-cqrs/Outboxes”).constructor

Defined in: src/Outbox.ts:57

What a mapping makes of an event. undefined publishes nothing for it.

readonly payload: unknown

Defined in: src/Outbox.ts:59

readonly type: string

Defined in: src/Outbox.ts:58


Defined in: src/Outbox.ts:40

An integration event, what a publisher sends.

readonly aggregateId: string

Defined in: src/Outbox.ts:46

readonly aggregateName: string

Defined in: src/Outbox.ts:45

readonly causationId: string | null

Defined in: src/Outbox.ts:53

readonly correlationId: string | null

Defined in: src/Outbox.ts:52

readonly id: string

Defined in: src/Outbox.ts:42

The id of the event it comes from, the same however often it is published: todo/a@1.

readonly payload: unknown

Defined in: src/Outbox.ts:44

readonly position: number

Defined in: src/Outbox.ts:50

The position of the event in the store.

readonly timestamp: Utc

Defined in: src/Outbox.ts:51

readonly type: string

Defined in: src/Outbox.ts:43

readonly version: number

Defined in: src/Outbox.ts:48

The version of the event in its stream.


Defined in: src/Outbox.ts:69

Name extends string

E

R

readonly aggregates: readonly Any[]

Defined in: src/Outbox.ts:71

readonly batchSize: number

Defined in: src/Outbox.ts:74

readonly linger: Duration

Defined in: src/Outbox.ts:75

readonly mappings: Record<string, (event, meta) => Mapped | undefined>

Defined in: src/Outbox.ts:72

readonly name: Name

Defined in: src/Outbox.ts:70

readonly publish: (messages) => Effect<void, E, R>

Defined in: src/Outbox.ts:73

readonly Message[]

Effect<void, E, R>

Any = Outbox<string, any, any>

Defined in: src/Outbox.ts:78


Context<O> = O extends Outbox<any, any, infer R> ? R : never

Defined in: src/Outbox.ts:80

O extends Any


Mappings<Event> = { readonly [Tag in Event[“_tag”]]?: (event: Extract<Event, { _tag: Tag }>, meta: EventMeta) => Mapped | undefined }

Defined in: src/Outbox.ts:62

Event extends Tagged

layer<Os>(outboxes): Layer<Outboxes, EventStoreError, EventStore | Checkpoints | Context<Os[number]>>

Defined in: src/Outbox.ts:233

Runs the outboxes in the background for as long as the layer lives. Each one keeps a checkpoint under its name in Checkpoints, which should be durable: with an in-memory checkpoint an outbox publishes everything again on restart.

Os extends readonly Any[]

Os

Layer<Outboxes, EventStoreError, EventStore | Checkpoints | Context<Os[number]>>


make<Name, Aggregates, E, R>(options): Outbox<Name, E, R>

Defined in: src/Outbox.ts:99

Defines an outbox. messages maps events, by tag, to integration events, and only those are published. Without it every event of the aggregates is published, typed <aggregate>.<tag>, with its JSON form as payload.

Mapping is where domain events become a contract with other teams, and where personal data is left out: events reach it decrypted, see PersonalData.

Name extends string

Aggregates extends readonly Any[]

E = never

R = never

Aggregates

number

The most messages in one call to publish. Defaults to 100.

Input

How long a batch waits for more messages before it is published. Defaults to 10 milliseconds.

Mappings<Event<Aggregates[number]>>

Name

(messages) => Effect<void, E, R>

Outbox<Name, E, R>