MessageStore.Message (message_store v3.1.0) View Source

A module for build, copy and folow message data.

Link to this section Summary

Link to this section Types

Specs

event_message() :: EventStore.EventData.t()

Specs

key() :: Map.key()

Specs

message() :: %{
  optional(:event_id) => uuid(),
  :type => message_type(),
  :data => map(),
  :metadata => map(),
  optional(:causation_id) => uuid(),
  optional(:correlation_id) => uuid()
}

Specs

message_type() :: String.t() | atom()

Specs

path() :: [key(), ...]

Specs

recorded_message() :: EventStore.RecordedEvent.t()

Specs

stream_name() :: String.t()

Specs

uuid() :: String.t()

Specs

Link to this section Functions

Specs

build(message()) :: event_message()

Create event data struct from message map.

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
}
Link to this function

copy(type, recorded_message, copy_list)

View Source

Specs

The copy function constructs a message from another message's data.

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

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.

Link to this function

follow(type, recorded_message, copy_list)

View Source

Specs

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

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

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"
}
Link to this macro

is_data_or_metadata(d)

View Source (macro)
Link to this macro

is_message_type(type)

View Source (macro)
Link to this macro

is_module_name(module)

View Source (macro)
Link to this macro

is_version(version)

View Source (macro)
Link to this function

type_from_module(module)

View Source

Specs

type_from_module(module()) :: String.t()
Link to this function

write(message_store, stream_name, message_or_messages, version \\ :any_version)

View Source

Specs