-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically populate event_type
in EventStore.EventData
#116
Comments
The Typically you'd create a module per event you persist: events = [
%EventData{
event_type: "Elixir.ExampleEvent",
data: %ExampleEvent{..},
metadata: %{"user" => "user@example.com"}
}
]
:ok = EventStore.append_to_stream(source_stream_uuid, 0, events) The defmodule EventStore.RecordedEvent do
def deserialize(%RecordedEvent{} = recorded_event, serializer) do
%RecordedEvent{data: data, metadata: metadata, event_type: event_type} = recorded_event
%RecordedEvent{
recorded_event
| data: serializer.deserialize(data, type: event_type),
metadata: serializer.deserialize(metadata, [])
}
end
end The example JSON serializer, using the Poison library, deserializes events using the defmodule EventStore.JsonSerializer do
@behaviour EventStore.Serializer
def serialize(term) do
Poison.encode!(term)
end
def deserialize(binary, config) do
type =
case Keyword.get(config, :type, nil) do
nil -> []
type -> type |> String.to_existing_atom() |> struct
end
Poison.decode!(binary, as: type)
end
end Populating the |
I was thinking about something along the lines of defp map_to_recorded_event(
%EventData{
data: %{__struct__: event_type},
event_type: nil
} = event,
created_at,
serializer
) do
%{event | event_type: Atom.to_string(event_type)}
|> map_to_recorded_event(created_at, serializer)
end in That way you can remove I'm happy to file a PR for this. |
Right, I understand what you're proposing thanks to the code example. Yes, it would be useful to allow the |
Hey there,
This is really a neat project! While I am digging into the codebase and the usage of this I was wondering why I need to set
event_type
inEventStore.EventData
especially whendata
is a Struct. The library could internally callAtom.to_string/1
and only optionally allow an overwrite via settingevent_type
.Would you folks be open to a PR?
The text was updated successfully, but these errors were encountered: