View Source MessageStore.Message (message_store v3.2.0)
A module for build, copy and folow message data.
Link to this section Summary
Functions
Create event data struct from message map.
The copy function constructs a message from another message's data.
Constructing a message from a preceding message.
Link to this section Types
@type event_message() :: EventStore.EventData.t()
@type key() :: Map.key()
@type path() :: [key(), ...]
@type recorded_message() :: EventStore.RecordedEvent.t()
@type stream_name() :: String.t()
@type uuid() :: String.t()
@type version() :: EventStore.expected_version()
Link to this section Functions
@spec build(message()) :: event_message()
Create event data struct from message map.
examples
Examples
iex> message = %{type: "Foo", data: %{foo: 1}, metadata: %{bar: 2}}
iex> Message.build(message)
%EventStore.EventData{
event_id: nil,
event_type: "Foo",
data: %{foo: 1},
metadata: %{bar: 2},
causation_id: nil,
correlation_id: nil
}
@spec copy(message_type(), recorded_message(), [path() | key()]) :: event_message()
The copy function constructs a message from another message's data.
examples
Examples
iex> recorded_message = %EventStore.RecordedEvent{
...> causation_id: nil,
...> correlation_id: "abcd1234",
...> created_at: ~U[2021-07-15 12:15:07.379908Z],
...> data: %{foo: "bazinga"},
...> event_id: "b9fcdccd-a495-4df3-889a-4b38f35a2618",
...> event_number: 935,
...> event_type: "Test",
...> metadata: %{bar: "baz", moo: 1},
...> stream_uuid: "test-123",
...> stream_version: 935
...> }
iex> Message.copy("Foo", recorded_message, [:data, [:metadata, :bar]])
%EventStore.EventData{
event_id: nil,
event_type: "Foo",
data: %{foo: "bazinga"},
metadata: %{bar: "baz"},
causation_id: nil,
correlation_id: nil
}
warning
Warning
Copying the metadata should be used with extreme caution, and has no practical use in everyday applicative logic. Except for certain testing and infrastructural scenarios, copying the identifying metadata from one message to another can result in significant malfunctions if the copied message is then written to a stream and processed.
@spec follow(message_type(), recorded_message(), [path() | key()]) :: event_message()
Constructing a message from a preceding message.
Following a message has almost identical behavior to a message copy/3
method.
The follow message leverages the implementation of copy/3
to fulfill its purpose.
message-workflows
Message Workflows
Messages frequently represent subsequent steps or stages in a process. Subsequent messages follow after preceding messages. Selected data or metadata from the preceding message is copied to the subsequent messages.
examples
Examples
iex> recorded_message = %EventStore.RecordedEvent{
...> causation_id: nil,
...> correlation_id: "abcd1234",
...> created_at: ~U[2021-07-15 12:15:07.379908Z],
...> data: %{foo: "bazinga"},
...> event_id: "b9fcdccd-a495-4df3-889a-4b38f35a2618",
...> event_number: 935,
...> event_type: "Test",
...> metadata: %{bar: "baz", moo: 1},
...> stream_uuid: "test-123",
...> stream_version: 935
...> }
iex> Message.follow("Foo", recorded_message, [:data, [:metadata, :bar]])
%EventStore.EventData{
event_id: nil,
event_type: "Foo",
data: %{foo: "bazinga"},
metadata: %{bar: "baz"},
causation_id: "b9fcdccd-a495-4df3-889a-4b38f35a2618",
correlation_id: "abcd1234"
}
write(message_store, stream_name, message_or_messages, version \\ :any_version)
View Source@spec write(module(), stream_name(), event_message() | [event_message()], version()) :: Result.t(any(), any())