View Source Sorcery.EventStore.Behaviour behaviour (Sorcery v0.1.0)
Defines the behavior that event stores must implement.
An event store is responsible for persisting and retrieving events. Implementations of this behavior could store events in memory, in a database, or any other storage mechanism.
Example Implementation
defmodule MyApp.CustomStore do
use GenServer
@behaviour Sorcery.Stores.Behaviour
# Implement the required callbacks
@impl true
def init(opts) do
{:ok, initial_state}
end
@impl true
def append(state, events) do
# Store the events...
{:ok, new_state}
end
@impl true
def get_events(state, query, opts) do
# Retrieve events based on query...
{:ok, events, new_state}
end
endConfiguration
config :sorcery, :event_store,
store: MyApp.CustomStore,
store_opts: [...]
Summary
Callbacks
Appends one or more events to the store.
Retrieves events from the store based on query parameters.
Initializes the event store with the given options.
Types
@type query() :: %{ optional(:type) => String.t(), optional(:domain) => String.t(), optional(:instance_id) => String.t(), optional(:from) => DateTime.t(), optional(:to) => DateTime.t() }
Callbacks
@callback append(state :: term(), events :: Sorcery.Event.t() | [Sorcery.Event.t()]) :: {:ok, [Sorcery.Event.t()], state :: term()}
Appends one or more events to the store.
@callback get_events(state :: term(), query :: query() | nil, opts :: Keyword.t()) :: {:ok, [Sorcery.Event.t()], state :: term()}
Retrieves events from the store based on query parameters.
Query Parameters
:type- Filter by event type:domain- Filter by domain:instance_id- Filter by instance ID:from- Filter events after this timestamp:to- Filter events before this timestamp
The query parameter is optional. Passing nil or an empty map will return all events.
Options
:limit- Maximum number of events to return:offset- Number of events to skip
Initializes the event store with the given options.