Hume.EventStore behaviour (hume v0.0.17)

Copy Markdown View Source

Behaviour module for event stores.

An event store is responsible for storing and retrieving events in a stream. Each event has a sequence number that is strictly increasing within a stream. Events must be appended in order.

Summary

Callbacks

Append a single event to the given stream.

Get all events from the stream starting from the given sequence number (exclusive).

Functions

Appends an event to the specified stream in the given event store.

Validates that the given module implements the required functions of the Hume.EventStore behaviour.

Types

event()

@type event() :: {seq(), payload()}

payload()

@type payload() :: term()

seq()

@type seq() :: non_neg_integer()

stream()

@type stream() :: term()

Callbacks

append(stream, payload, expect)

@callback append(stream(), payload(), expect :: seq() | nil) ::
  {:ok, seq()} | {:error, term()}

Append a single event to the given stream.

Parameters

  • stream - The stream identifier where the event will be appended.
  • payload - The event payload to be stored.
  • expect - The expected last sequence number in the stream, or nil to skip the check. If a sequence number is provided, the implementation must only append the event if the current last sequence number in the stream equals expect. Otherwise it must return an error.

The implementation must assign the next strictly increasing sequence number and return it in the {:ok, seq()} tuple on success.

events(stream, from)

@callback events(stream(), from :: seq()) :: Enumerable.t(event())

Get all events from the stream starting from the given sequence number (exclusive).

The events are returned in strictly ascending order by sequence number.

Functions

append(event_store, stream, payload, expect_seq \\ nil)

@spec append(module(), stream(), payload(), expect_seq :: seq() | nil) ::
  {:ok, seq()} | {:error, term()}

Appends an event to the specified stream in the given event store.

Parameters

  • event_store: The module implementing the event store (must use Hume.EventStore).
  • stream: The stream identifier where the event will be appended.
  • payload: The event payload to be appended.
  • expect_seq: Optional expected sequence number for optimistic concurrency control. If provided, the append will only succeed if the current last sequence number in the stream matches this value.

validate(mod)

@spec validate(module()) :: :ok | {:error, :invalid_module}

Validates that the given module implements the required functions of the Hume.EventStore behaviour.