Skip to main content

EventStore

Trait EventStore 

Source
pub trait EventStore: Send + Sync {
    type Id: Clone + Send + Sync + 'static;
    type Position: Clone + PartialEq + Debug + Send + Sync + 'static;
    type Error: Error + Send + Sync + 'static;
    type Metadata: Send + Sync + 'static;
    type Data: Clone + Send + Sync + 'static;

    // Required methods
    fn decode_event<E>(
        &self,
        stored: &StoredEvent<Self::Id, Self::Position, Self::Data, Self::Metadata>,
    ) -> Result<E, Self::Error>
       where E: DomainEvent + DeserializeOwned;
    fn stream_version<'a>(
        &'a self,
        aggregate_kind: &'a str,
        aggregate_id: &'a Self::Id,
    ) -> impl Future<Output = Result<Option<Self::Position>, Self::Error>> + Send + 'a;
    fn commit_events<'a, E>(
        &'a self,
        aggregate_kind: &'a str,
        aggregate_id: &'a Self::Id,
        events: NonEmpty<E>,
        metadata: &'a Self::Metadata,
    ) -> impl Future<Output = Result<Committed<Self::Position>, CommitError<Self::Error>>> + Send + 'a
       where E: EventKind + Serialize + Send + Sync + 'a,
             Self::Metadata: Clone;
    fn commit_events_optimistic<'a, E>(
        &'a self,
        aggregate_kind: &'a str,
        aggregate_id: &'a Self::Id,
        expected_version: Option<Self::Position>,
        events: NonEmpty<E>,
        metadata: &'a Self::Metadata,
    ) -> impl Future<Output = Result<Committed<Self::Position>, OptimisticCommitError<Self::Position, Self::Error>>> + Send + 'a
       where E: EventKind + Serialize + Send + Sync + 'a,
             Self::Metadata: Clone;
    fn load_events<'a>(
        &'a self,
        filters: &'a [EventFilter<Self::Id, Self::Position>],
    ) -> impl Future<Output = Result<Vec<StoredEvent<Self::Id, Self::Position, Self::Data, Self::Metadata>>, Self::Error>> + Send + 'a;
}
Expand description

Abstraction over the persistence layer for event streams.

This trait supports both stream-based and type-partitioned storage implementations. Stores own event serialisation/deserialisation.

Associated types allow stores to customise their behaviour:

  • Id: Aggregate identifier type
  • Position: Ordering strategy (() for stream-based, u64 for global ordering)
  • Metadata: Infrastructure metadata type (timestamps, causation tracking, etc.)
  • Data: Serialised event payload type (e.g., serde_json::Value for JSON)

Required Associated Types§

Source

type Id: Clone + Send + Sync + 'static

Aggregate identifier type.

This type must be clonable so repositories can reuse IDs across calls. Common choices: String, Uuid, or custom ID types.

Source

type Position: Clone + PartialEq + Debug + Send + Sync + 'static

Position type used for ordering events and version checking.

Must be Clone + PartialEq to support optimistic concurrency. Use () if ordering is not needed.

Source

type Error: Error + Send + Sync + 'static

Store-specific error type.

Source

type Metadata: Send + Sync + 'static

Metadata type for infrastructure concerns.

Source

type Data: Clone + Send + Sync + 'static

Serialised event payload type.

This is the format used to store event data internally. Common choices:

  • serde_json::Value for JSON-based stores
  • Vec<u8> for binary stores

Required Methods§

Source

fn decode_event<E>( &self, stored: &StoredEvent<Self::Id, Self::Position, Self::Data, Self::Metadata>, ) -> Result<E, Self::Error>

Decode a stored event into a concrete event type.

Deserialises the data field of a StoredEvent back into a domain event.

§Errors

Returns an error if deserialisation fails.

Source

fn stream_version<'a>( &'a self, aggregate_kind: &'a str, aggregate_id: &'a Self::Id, ) -> impl Future<Output = Result<Option<Self::Position>, Self::Error>> + Send + 'a

Get the current version (latest position) for an aggregate stream.

Returns None for streams with no events.

§Errors

Returns a store-specific error when the operation fails.

Source

fn commit_events<'a, E>( &'a self, aggregate_kind: &'a str, aggregate_id: &'a Self::Id, events: NonEmpty<E>, metadata: &'a Self::Metadata, ) -> impl Future<Output = Result<Committed<Self::Position>, CommitError<Self::Error>>> + Send + 'a
where E: EventKind + Serialize + Send + Sync + 'a, Self::Metadata: Clone,

Commit events to an aggregate stream without version checking.

Events are serialised and persisted atomically. No conflict detection is performed (last-writer-wins).

§Errors

Returns CommitError::Serialization if an event fails to serialise, or CommitError::Store if persistence fails.

Source

fn commit_events_optimistic<'a, E>( &'a self, aggregate_kind: &'a str, aggregate_id: &'a Self::Id, expected_version: Option<Self::Position>, events: NonEmpty<E>, metadata: &'a Self::Metadata, ) -> impl Future<Output = Result<Committed<Self::Position>, OptimisticCommitError<Self::Position, Self::Error>>> + Send + 'a
where E: EventKind + Serialize + Send + Sync + 'a, Self::Metadata: Clone,

Commit events to an aggregate stream with optimistic concurrency control.

Events are serialised and persisted atomically. The commit fails if:

  • expected_version is Some(v) and the current version differs from v
  • expected_version is None and the stream already has events (new aggregate expected)
§Errors

Returns OptimisticCommitError::Serialization if an event fails to serialise, OptimisticCommitError::Conflict if the version check fails, or OptimisticCommitError::Store if persistence fails.

Source

fn load_events<'a>( &'a self, filters: &'a [EventFilter<Self::Id, Self::Position>], ) -> impl Future<Output = Result<Vec<StoredEvent<Self::Id, Self::Position, Self::Data, Self::Metadata>>, Self::Error>> + Send + 'a

Load events matching the specified filters.

Each filter describes an event kind and optional aggregate identity:

The store optimises based on its storage model and returns events merged by position (if positions are available).

§Errors

Returns a store-specific error when loading fails.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<Id, M> EventStore for Store<Id, M>
where Id: Clone + Eq + Hash + Send + Sync + 'static, M: Clone + Send + Sync + 'static,