eventsourcing

Types

pub type AggregateId =
  String

An EventEnvelop is a wrapper around your domain events used by the Event Stores. You can use this type constructor if the event store provides a load_events function.

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)
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 main function of the package. Run execute with your event_sourcing instance and the command you want to apply. It will return a Result with Ok(Nil) or Error(your domain error) if the command failed.

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))
pub fn load_aggregate(
  eventsourcing eventsourcing: EventSourcing(a, b, c, d, e, f),
  aggregate_id aggregate_id: String,
) -> b
pub fn load_events(
  eventsourcing eventsourcing: EventSourcing(a, b, c, d, e, f),
  aggregate_id aggregate_id: String,
) -> List(EventEnvelop(d))
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