eventsourcing

Types

pub type AggregateId =
  String

An EventEnvelop is a wrapper around your domain events used by the Event Stores.

pub type EventEnvelop(event) {
  MemoryStoreEventEnvelop(
    aggregate_id: AggregateId,
    sequence: Int,
    payload: event,
    metadata: List(#(String, String)),
  )
  SerializedEventEnvelop(
    aggregate_id: AggregateId,
    sequence: Int,
    payload: event,
    metadata: List(#(String, String)),
    event_type: String,
    event_version: String,
    aggregate_type: String,
  )
}

Constructors

  • MemoryStoreEventEnvelop(
      aggregate_id: AggregateId,
      sequence: Int,
      payload: event,
      metadata: List(#(String, String)),
    )
  • SerializedEventEnvelop(
      aggregate_id: AggregateId,
      sequence: Int,
      payload: event,
      metadata: List(#(String, String)),
      event_type: String,
      event_version: String,
      aggregate_type: String,
    )

The main record of the library. It holds everything together and serves as a reference point for other functions such as execute, load_aggregate_entity, and load_events

pub opaque type EventSourcing(
  eventstore,
  entity,
  command,
  event,
  error,
  aggregatecontext,
)
pub type EventSourcingError(domainerror) {
  DomainError(domainerror)
  ImplementationError
  EntityNotFound
}

Constructors

  • DomainError(domainerror)
  • ImplementationError
  • EntityNotFound

Wrapper around the event store implementations

pub type EventStore(eventstore, entity, command, event, error) {
  EventStore(
    eventstore: eventstore,
    load_aggregate: fn(eventstore, AggregateId) ->
      AggregateContext(entity, command, event, error),
    load_events: fn(eventstore, AggregateId) ->
      List(EventEnvelop(event)),
    commit: fn(
      eventstore,
      AggregateContext(entity, command, event, error),
      List(event),
      List(#(String, String)),
    ) ->
      List(EventEnvelop(event)),
  )
}

Constructors

  • EventStore(
      eventstore: eventstore,
      load_aggregate: fn(eventstore, AggregateId) ->
        AggregateContext(entity, command, event, error),
      load_events: fn(eventstore, AggregateId) ->
        List(EventEnvelop(event)),
      commit: fn(
        eventstore,
        AggregateContext(entity, command, event, error),
        List(event),
        List(#(String, String)),
      ) ->
        List(EventEnvelop(event)),
    )

Functions

pub fn add_query(
  eventsouring eventsourcing: EventSourcing(a, b, c, d, e, f),
  query query: fn(String, List(EventEnvelop(d))) -> Nil,
) -> EventSourcing(a, b, c, d, e, g)

Add a query to the EventSourcing instance.

Queries are functions that run when events are committed. They can be used for things like updating read models or sending notifications.

pub fn execute(
  event_sourcing event_sourcing: EventSourcing(a, b, c, d, e, f),
  aggregate_id aggregate_id: String,
  command command: c,
) -> Result(Nil, EventSourcingError(e))

Execute the given command on the event sourcing instance.

This function loads the aggregate, handles the command, applies the resulting events, commits them to the event store, and runs any registered queries.

pub fn execute_with_metadata(
  event_sourcing event_sourcing: EventSourcing(a, b, c, d, e, f),
  aggregate_id aggregate_id: String,
  command command: c,
  metadata metadata: List(#(String, String)),
) -> Result(Nil, EventSourcingError(e))

Execute the given command with metadata on the event sourcing instance.

This function works similarly to execute, but additionally allows passing metadata for the events.

pub fn load_aggregate(
  eventsourcing eventsourcing: EventSourcing(a, b, c, d, e, f),
  aggregate_id aggregate_id: String,
) -> b

Load the aggregate entity for a given aggregate ID.

pub fn load_events(
  eventsourcing eventsourcing: EventSourcing(a, b, c, d, e, f),
  aggregate_id aggregate_id: String,
) -> List(EventEnvelop(d))

Load the events for a given aggregate ID.

pub fn new(
  event_store: EventStore(a, b, c, d, e),
  queries: List(fn(String, List(EventEnvelop(d))) -> Nil),
) -> EventSourcing(a, b, c, d, e, f)

Create a new EventSourcing instance providing an Event Store and a list of queries you want run whenever events are commited.

Search Document