Spear.Event.new

You're seeing just the function new, go back to Spear.Event module for more information.
Link to this function

new(type, body, opts \\ [])

View Source (since 0.1.0)

Specs

new(String.t(), term(), Keyword.t()) :: t()

Creates an event struct

This function does not append the event to a stream on its own, but can provide events to Spear.append/4 which will append events to a stream.

type is any string used to declare how the event is typed. This is very arbitrary and may coincide with struct names or may be hard-coded per event.

Options

  • :id - (default: Spear.Event.uuid_v4()) the event's ID. See the section on event IDs below.
  • :content_type - (default: "application/json") the encoding used to turn the event's body into binary data. If the content-type is "application/json", the EventStoreDB and Spear (in Spear.Event.from_read_response/2)
  • :custom_metadata - (default: "") an event field outside of the body meant as a bag for storing custom attributes about an event. Usage of this field is not obligatory: leaving it blank is perfectly normal.

Event IDs

EventStoreDB uses event IDs to provide an idempotency feature. Any event written to the EventStoreDB with an already existing ID will be not be duplicated.

iex> event = Spear.Event.new("grpc-client", %{"languages" => ["typescript", "javascript"], "runtime" => "NodeJS"})
%Spear.Event{
  body: %{"languages" => ["typescript", "javascript"], "runtime" => "NodeJS"},
  id: "1e654b2a-ff04-4af8-887f-052442edcd83",
  metadata: %{content_type: "application/json", custom_metadata: ""},
  type: "grpc-client"
}
iex> [event] |> Spear.append(conn, "idempotency_test")
:ok
iex> [event] |> Spear.append(conn, "idempotency_test")
:ok
iex> Spear.stream!(conn, "idempotency_test") |> Enum.to_list()
[
  %Spear.Event{
    body: %{"languages" => ["typescript", "javascript"], "runtime" => "NodeJS"},
    id: "1e654b2a-ff04-4af8-887f-052442edcd83",
    metadata: %{
      commit_position: 18446744073709551615,
      content_type: "application/json",
      created: ~U[2021-04-07 21:53:40.395681Z],
      custom_metadata: "",
      prepare_position: 18446744073709551615,
      stream_name: "idempotency_test",
      stream_revision: 0
    },
    type: "grpc-client"
  }
]

Examples

File.stream!("data.csv")
|> MyCsvParser.parse_stream()
|> Stream.map(fn [id, type, amount] ->
  Spear.Event.new("ChargeDeclared",
    %{id: id, type: type, amount: amount}
  )
end)
|> Spear.append(conn, "ChargesFromCsvs", batch_size: 20)