Raxol.Architecture.EventSourcing.Event (Raxol v2.0.1)

View Source

Event structure for event sourcing implementation in Raxol.

Events represent facts about what has happened in the system. They are immutable and form the single source of truth for all state changes.

Event Design Principles

  1. Past Tense: Events represent things that have already happened
  2. Immutable: Once created, events cannot be changed
  3. Rich Information: Events should contain all relevant data
  4. Causality: Events should track what caused them
  5. Versioned: Events should support schema evolution

Usage

defmodule TerminalCreatedEvent do
  use Raxol.Architecture.EventSourcing.Event

  defstruct [
    :terminal_id,
    :user_id,
    :width,
    :height,
    :created_at,
    :metadata
  ]

  @type t :: %__MODULE__{
    terminal_id: String.t(),
    user_id: String.t(),
    width: pos_integer(),
    height: pos_integer(),
    created_at: integer(),
    metadata: map()
  }
end

Summary

Functions

Creates event correlation chain.

Checks if events are correlated.

Creates an event envelope with all required metadata.

Checks if an event is of a specific type.

Extracts the event data from an envelope.

Deserializes an event from JSON.

Creates an event from a map.

Gets the causation ID from an event.

Gets all events in a correlation chain.

Gets the correlation ID from an event.

Gets the event type from an envelope or event data.

Creates an event migration for schema evolution.

Serializes an event to JSON.

Converts an event to a map for storage.

Validates event envelope structure.

Types

event_data()

@type event_data() :: struct()

event_metadata()

@type event_metadata() :: %{
  correlation_id: String.t() | nil,
  causation_id: String.t() | nil,
  user_id: String.t() | nil,
  timestamp: integer(),
  version: non_neg_integer()
}

t()

@type t() :: %Raxol.Architecture.EventSourcing.Event{
  created_at: integer(),
  data: struct(),
  event_type: module(),
  id: String.t(),
  metadata: map(),
  position: non_neg_integer(),
  stream_name: String.t()
}

Functions

correlate_events(parent_event, child_event)

Creates event correlation chain.

correlated?(event1, event2)

Checks if events are correlated.

create_envelope(event_data, stream_name, opts \\ [])

Creates an event envelope with all required metadata.

event_type?(arg1, expected_type)

Checks if an event is of a specific type.

extract_data(event)

Extracts the event data from an envelope.

from_json(json)

Deserializes an event from JSON.

from_map(map)

Creates an event from a map.

get_causation_id(event)

Gets the causation ID from an event.

get_correlation_chain(events, correlation_id)

Gets all events in a correlation chain.

get_correlation_id(event)

Gets the correlation ID from an event.

get_event_type(arg1)

Gets the event type from an envelope or event data.

is_event_type?(event, expected_type)

This function is deprecated. Use event_type?/2 instead.

migrate_event(event, migration_fn)

Creates an event migration for schema evolution.

to_json(event)

Serializes an event to JSON.

to_map(event)

Converts an event to a map for storage.

validate_envelope(event)

Validates event envelope structure.