Outbox
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.
Classes
Section titled “Classes”OutboxError
Section titled “OutboxError”Defined in: src/Outbox.ts:83
Publishing failed, which the outbox retries.
Extends
Section titled “Extends”object&YieldableError<this>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new OutboxError(…
args):OutboxError
Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Schema.d.ts:10979
Parameters
Section titled “Parameters”…[props: object, options?: MakeOptions]
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Schema.TaggedError<OutboxError>()("OutboxError", { outbox: Schema.String, cause: Schema.Defect() }).constructor
Properties
Section titled “Properties”cause:
unknown
Defined in: src/Outbox.ts:85
Inherited from
Section titled “Inherited from”Schema.TaggedError<OutboxError>()("OutboxError", { outbox: Schema.String, cause: Schema.Defect() }).cause
outbox
Section titled “outbox”
readonlyoutbox:string=Schema.String
Defined in: src/Outbox.ts:84
Inherited from
Section titled “Inherited from”Schema.TaggedError<OutboxError>()("OutboxError", { outbox: Schema.String, cause: Schema.Defect() }).outbox
Outboxes
Section titled “Outboxes”Defined in: src/Outbox.ts:142
Extends
Section titled “Extends”Shape<"effect-cqrs/Outboxes", {status:Effect<readonlyStatus[]>;published(position?):Effect<void,EventStoreError|OutboxError>; },this>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Outboxes(
_):Outboxes
Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Context.d.ts:121
Parameters
Section titled “Parameters”never
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”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
Interfaces
Section titled “Interfaces”Mapped
Section titled “Mapped”Defined in: src/Outbox.ts:57
What a mapping makes of an event. undefined publishes nothing for it.
Properties
Section titled “Properties”payload
Section titled “payload”
readonlypayload:unknown
Defined in: src/Outbox.ts:59
readonlytype:string
Defined in: src/Outbox.ts:58
Message
Section titled “Message”Defined in: src/Outbox.ts:40
An integration event, what a publisher sends.
Properties
Section titled “Properties”aggregateId
Section titled “aggregateId”
readonlyaggregateId:string
Defined in: src/Outbox.ts:46
aggregateName
Section titled “aggregateName”
readonlyaggregateName:string
Defined in: src/Outbox.ts:45
causationId
Section titled “causationId”
readonlycausationId:string|null
Defined in: src/Outbox.ts:53
correlationId
Section titled “correlationId”
readonlycorrelationId:string|null
Defined in: src/Outbox.ts:52
readonlyid: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.
payload
Section titled “payload”
readonlypayload:unknown
Defined in: src/Outbox.ts:44
position
Section titled “position”
readonlyposition:number
Defined in: src/Outbox.ts:50
The position of the event in the store.
timestamp
Section titled “timestamp”
readonlytimestamp:Utc
Defined in: src/Outbox.ts:51
readonlytype:string
Defined in: src/Outbox.ts:43
version
Section titled “version”
readonlyversion:number
Defined in: src/Outbox.ts:48
The version of the event in its stream.
Outbox
Section titled “Outbox”Defined in: src/Outbox.ts:69
Type Parameters
Section titled “Type Parameters”Name extends string
E
R
Properties
Section titled “Properties”aggregates
Section titled “aggregates”
readonlyaggregates: readonlyAny[]
Defined in: src/Outbox.ts:71
batchSize
Section titled “batchSize”
readonlybatchSize:number
Defined in: src/Outbox.ts:74
linger
Section titled “linger”
readonlylinger:Duration
Defined in: src/Outbox.ts:75
mappings
Section titled “mappings”
readonlymappings:Record<string, (event,meta) =>Mapped|undefined>
Defined in: src/Outbox.ts:72
readonlyname:Name
Defined in: src/Outbox.ts:70
publish
Section titled “publish”
readonlypublish: (messages) =>Effect<void,E,R>
Defined in: src/Outbox.ts:73
Parameters
Section titled “Parameters”messages
Section titled “messages”readonly Message[]
Returns
Section titled “Returns”Effect<void, E, R>
Type Aliases
Section titled “Type Aliases”Any =
Outbox<string,any,any>
Defined in: src/Outbox.ts:78
Context
Section titled “Context”Context<
O> =OextendsOutbox<any,any, infer R> ?R:never
Defined in: src/Outbox.ts:80
Type Parameters
Section titled “Type Parameters”O extends Any
Mappings
Section titled “Mappings”Mappings<
Event> = { readonly [Tag in Event[“_tag”]]?: (event: Extract<Event, { _tag: Tag }>, meta: EventMeta) => Mapped | undefined }
Defined in: src/Outbox.ts:62
Type Parameters
Section titled “Type Parameters”Event extends Tagged
Functions
Section titled “Functions”layer()
Section titled “layer()”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.
Type Parameters
Section titled “Type Parameters”Os extends readonly Any[]
Parameters
Section titled “Parameters”outboxes
Section titled “outboxes”Os
Returns
Section titled “Returns”Layer<Outboxes, EventStoreError, EventStore | Checkpoints | Context<Os[number]>>
make()
Section titled “make()”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.
Type Parameters
Section titled “Type Parameters”Name extends string
Aggregates
Section titled “Aggregates”Aggregates extends readonly Any[]
E = never
R = never
Parameters
Section titled “Parameters”options
Section titled “options”aggregates
Section titled “aggregates”Aggregates
batchSize?
Section titled “batchSize?”number
The most messages in one call to publish. Defaults to 100.
linger?
Section titled “linger?”Input
How long a batch waits for more messages before it is published. Defaults to 10 milliseconds.
messages?
Section titled “messages?”Mappings<Event<Aggregates[number]>>
Name
publish
Section titled “publish”(messages) => Effect<void, E, R>
Returns
Section titled “Returns”Outbox<Name, E, R>