NFIBrokerage / spear

A sharp EventStoreDB v20+ client backed by Mint :yum:
https://hex.pm/packages/spear
Apache License 2.0
85 stars 14 forks source link

How to Pass Event Metadata #89

Closed yordis closed 1 year ago

yordis commented 1 year ago

Reading: https://developers.eventstore.com/server/v21.10/streams.html#event-metadata

How do I pass $correlationId or $causationId to the events?

I am not sure if I suppose to pass it as part of custome_metadata or if I suppose to update the metadata key under the Spear.Event struct based on the following code snippet: https://github.com/NFIBrokerage/spear/blob/40d5f2f12eae2090e90d0d5e5cddf50329ff1222/lib/spear/event.ex#L162-L165

I appreciate any help you can provide.

the-mikedavis commented 1 year ago

Yeah the custom_metadata field should be used for this: any extra keys in metadata are ignored when writing a %Spear.Event{}. For example:

correlation_id = Spear.Event.uuid_v4()

Spear.Event.new("hello", %{}, custom_metadata: Jason.encode!(%{"$correlationId" => correlation_id, "$causationId" => Spear.Event.uuid_v4()}))
|> List.wrap()
|> Spear.append(conn, "mystreamname")

If the builtin $by_correlation_id projection is running it should create a new $bc-${correlation_id} stream for that event.