Serializing time event types from Dry::Struct events #1457
-
Hi, I implemented the event base class in my app on Dry::Struct as shown in some shared tutorials. One thing I'm disliking about it is how objects types like
In my mind it would be better to save them are iso8601 string because a) they could be consumed platform agnostic and b) also take up a lot less space (right now I'm running into errors because the data in one event is too large). Only obvious downside I see is that I lose the milliseconds. The biggest challenge I see at the moment is that in the load step I need to load the event class from the I saw the guides for custom mappers and serializers in the docs, so I was just wondering if a specific implementation for this issue already existed or if anyone has suggestions. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
The reason why you're seeing YAML in event This is the line responsible to set it: In ecommerce app, which runs on PostgreSQL, we chose to store So this side of the spectrum most of the time requires describing event schemas with Looking at this event class example from ecommerce, the attributes passed to initialize the instance of event class will be processed by its internal schema description. Coercing any ISO strings into Going with Protocol Buffers means mandatory event schema, because the serialized output is binary and schema is needed to decode it. This is the other side of the spectrum. Somewhere in the middle of the spectrum is the solution we'll be slowly introducing from next (v2.6.0) RES release. By introducing special transformation in the pipeline we have a way to transform particular objects before they even hit the serializer. To make it work in both directions, information about original type has to be preserved. In all approaches to transform objects into strings and back, information about desired type has to be preserved. In YAML that's encoded along data. In JSON it has to be stored aside, in code (event schema) or along event (metadata). |
Beta Was this translation helpful? Give feedback.
-
Btw. some time ago I've started "benchmarking" different approaches to event schemas — https://github.com/pawelpacana/res-event-schema-comparison |
Beta Was this translation helpful? Give feedback.
-
Wow, thanks so much for the in-depth response, that gives me a lot to study. YAML as a default choice makes total sense. For our project we use MySQL, so sadly don't have JSONB available. I suppose I could still use JSON (since our version is newer than 5.7.8), but then I would have to still include the json serializer, which doesn't sound too bad.
Yeah, since events are such an integral part of the system when using an event store it seems very irresponsible to me to not define a proper structure for them in code |
Beta Was this translation helpful? Give feedback.
-
One more thing to add: Since I was struggling with the size of the field in the event I thought a bit more about potential solutions and one idea I got was to use Zlib to deflate and inflate the data as part of the serialization. |
Beta Was this translation helpful? Give feedback.
-
Released: https://github.com/RailsEventStore/rails_event_store/releases/tag/v2.6.0 |
Beta Was this translation helpful? Give feedback.
The reason why you're seeing YAML in event
data
column inevent_store_events
tables is that we chose it as a safe default to serialize various data types. Mostly working without any additional configuration and supporting all database engines RES works on (not only PostgreSQL). Like every choice, it has its drawbacks too. YAML is one side of the spectrum.This is the line responsible to set it:
rails_event_store/rails_event_store/lib/rails_event_store/client.rb
Line 9 in 260de99
In ecommerce app, which runs on PostgreSQL, we chose to store
data
column withjsonb
d…