AshCommanded.Commanded.Snapshot (AshCommanded v0.1.0)
View SourceRepresents a snapshot of an aggregate's state at a specific point in time.
Snapshots are used as a performance optimization to avoid replaying all events from the beginning of the aggregate's history. Instead, a snapshot represents the aggregate state as of a specific version, allowing the system to load the snapshot and only replay events that occurred after the snapshot was taken.
Summary
Functions
Creates a new snapshot from an aggregate state.
Gets the source UUID (aggregate ID) from a snapshot.
Extracts the aggregate state from a snapshot.
Gets the aggregate version from a snapshot.
Types
Functions
Creates a new snapshot from an aggregate state.
Parameters
aggregate
- The aggregate statesource_type
- The aggregate moduleversion
- The aggregate version (event number)source_version
- The snapshot schema version
Returns
A new snapshot structure
Examples
iex> aggregate = %MyApp.UserAggregate{id: "123", name: "John"}
iex> AshCommanded.Commanded.Snapshot.new(aggregate, MyApp.UserAggregate, 5, 1)
%AshCommanded.Commanded.Snapshot{
source_uuid: "123",
source_type: MyApp.UserAggregate,
source_version: 1,
state: %MyApp.UserAggregate{id: "123", name: "John"},
version: 5,
created_at: ~U[2023-01-01 00:00:00Z]
}
Gets the source UUID (aggregate ID) from a snapshot.
Parameters
snapshot
- The snapshot to get the UUID from
Returns
The source UUID
Examples
iex> snapshot = %AshCommanded.Commanded.Snapshot{source_uuid: "123"}
iex> AshCommanded.Commanded.Snapshot.source_uuid(snapshot)
"123"
Extracts the aggregate state from a snapshot.
Parameters
snapshot
- The snapshot to extract state from
Returns
The aggregate state
Examples
iex> snapshot = %AshCommanded.Commanded.Snapshot{state: %{id: "123", name: "John"}}
iex> AshCommanded.Commanded.Snapshot.state(snapshot)
%{id: "123", name: "John"}
Gets the aggregate version from a snapshot.
Parameters
snapshot
- The snapshot to get the version from
Returns
The aggregate version
Examples
iex> snapshot = %AshCommanded.Commanded.Snapshot{version: 5}
iex> AshCommanded.Commanded.Snapshot.version(snapshot)
5