Crate eventsourced
source ·Expand description
Event sourced entities.
EventSourced is inspired to a large degree by the amazing Akka Persistence library. It provides a framework for implementing Event Sourcing and CQRS.
The EventSourced trait defines types for commands, events, snapshot state and errors as well as methods for command handling, event handling and setting a snapshot state.
The EvtLog and SnapshotStore traits define a pluggable event log and a pluggable snapshot store respectively. For NATS and Postgres these are implemented in the respective crates.
The spawn extension method provides for creating entities – “running” instances of an EventSourced implementation, identifiable by a Uuid – for some event log and some snapshot store. Conversion of events and snapshot state to and from bytes happens via given Binarizer functions; for prost and serde_json these are already provided.
Calling spawn results in a cloneable EntityRef which can be used to pass commands to the spawned entity by invoking handle_cmd. Commands are handled by the command handler of the spawned entity. They can be rejected by returning an error. Valid commands produce an event with an optional tag which gets persisted to the EvtLog and then applied to the event handler of the respective entity. The event handler may decide to save a snapshot which is used to speed up future spawning.
Events can be queried from the event log by ID or by tag. These queries can be used to build read side projections.
Modules
Structs
- Collection of conversion functions from and to Bytes for events and snapshots.
- A handle for a spawned EventSourced entity which can be used to invoke its command handler.
- A SnapshotStore implementation that does nothing.
- Sequence number used for events by event log and snapshot store.
- Snapshot state along with its sequence number.
- An event and an optional tag. Typically not used direcly, but via IntoTaggedEvt or EvtExt.
- Error signaling that a sequence number must not be zero.
Enums
- Error from an EntityRef.
- Error from spawning an event sourced entity.
Traits
- Command and event handling for an event sourced entity.
- Extension methods for types implementing EventSourced.
- Provide a
with_tagextension method for events. Together with its blanket implementation for any event allows for callingwith_tagon any event type. - Persistence for events.
- Used in an EventSourced command handler as impl trait in return position. Together with its blanket implementation for any event allows for returning plain events without boilerplate.
- Persistence for snapshots.