AshCommanded.Commanded.Snapshot (AshCommanded v0.1.0)

View Source

Represents 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

t()

@type t() :: %AshCommanded.Commanded.Snapshot{
  created_at: DateTime.t(),
  source_type: module(),
  source_uuid: String.t(),
  source_version: integer(),
  state: map(),
  version: integer()
}

Functions

new(aggregate, source_type, version, source_version \\ 1)

@spec new(map(), module(), integer(), integer()) :: t()

Creates a new snapshot from an aggregate state.

Parameters

  • aggregate - The aggregate state
  • source_type - The aggregate module
  • version - 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]
}

source_uuid(snapshot)

@spec source_uuid(t()) :: String.t()

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"

state(snapshot)

@spec state(t()) :: map()

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"}

version(snapshot)

@spec version(t()) :: integer()

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