Packages
commanded
1.4.2
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
lib/commanded/event/mapper.ex
defmodule Commanded.Event.Mapper do
@moduledoc """
Map events to/from the structs used for persistence.
## Example
Map domain event structs to `Commanded.EventStore.EventData` structs in
preparation for appending to the configured event store:
events = [%ExampleEvent1{}, %ExampleEvent2{}]
event_data = Commanded.Event.Mapper.map_to_event_data(events)
:ok = Commanded.EventStore.append_to_stream("stream-1234", :any_version, event_data)
"""
alias Commanded.EventStore.TypeProvider
alias Commanded.EventStore.{EventData, RecordedEvent}
@type event :: struct
@doc """
Map a domain event (or list of events) to an
`Commanded.EventStore.EventData` struct (or list of structs).
Optionally, include the `causation_id`, `correlation_id`, and `metadata`
associated with the event(s).
## Examples
event_data = Commanded.Event.Mapper.map_to_event_data(%ExampleEvent{})
event_data =
Commanded.Event.Mapper.map_to_event_data(
[
%ExampleEvent1{},
%ExampleEvent2{}
],
causation_id: Commanded.UUID.uuid4(),
correlation_id: Commanded.UUID.uuid4(),
metadata: %{"user_id" => user_id}
)
"""
def map_to_event_data(events, fields \\ [])
@spec map_to_event_data(list(event), Keyword.t()) :: list(EventData.t())
def map_to_event_data(events, fields) when is_list(events) do
Enum.map(events, &map_to_event_data(&1, fields))
end
@spec map_to_event_data(struct, Keyword.t()) :: EventData.t()
def map_to_event_data(event, fields) do
%EventData{
causation_id: Keyword.get(fields, :causation_id),
correlation_id: Keyword.get(fields, :correlation_id),
event_type: TypeProvider.to_string(event),
data: event,
metadata: Keyword.get(fields, :metadata, %{})
}
end
@doc """
Map a list of `Commanded.EventStore.RecordedEvent` structs to their event data.
"""
@spec map_from_recorded_events(list(RecordedEvent.t())) :: [event]
def map_from_recorded_events(recorded_events) when is_list(recorded_events) do
Enum.map(recorded_events, &map_from_recorded_event/1)
end
@doc """
Map an `Commanded.EventStore.RecordedEvent` struct to its event data.
"""
@spec map_from_recorded_event(RecordedEvent.t()) :: event
def map_from_recorded_event(%RecordedEvent{data: data}), do: data
end