Skip to main content
Version: 2.17.0

Event serialization formats

By default RailsEventStore will use YAML as a serialization format. The reason is that YAML is available out of box and can serialize and deserialize data types which are not easily handled in other formats.

However, if you don't like YAML or you have different needs you can choose to use different serializers or even replace mappers entirely.

Configuring a different serializer

You can pass a different serializer as a dependency when instantiating the client.

Here is an example on how to configure RailsEventStore to serialize events' data and metadata using Marshal.

# config/environments/*.rb

Rails.application.configure do
config.to_prepare do
Rails.configuration.event_store = RailsEventStore::Client.new(
repository: RailsEventStoreActiveRecord::EventRepository.new(serializer: Marshal)
)
end
end

The provided serializer must respond to load and dump.

Serialization is needed not only when writing to and reading from storage, but also when scheduling events for background processing by async handlers:

Rails.configuration.event_store = RailsEventStore::Client.new(
message_broker: RubyEventStore::Broker.new(
dispatcher: RubyEventStore::ComposedDispatcher.new(
RailsEventStore::AfterCommitAsyncDispatcher.new(scheduler: ActiveJobScheduler.new(serializer: Marshal)),
RubyEventStore::Dispatcher.new
)
)
)
class SomeHandler < ActiveJob::Base
include RailsEventStore::AsyncHandler.with(serializer: Marshal)

def perform(event)
# ...
end
end

Configuring for Postgres JSON/B data type

In Postgres database, you can store your events data and metadata in json or jsonb format.

To generate migration containing event table schemas run

$ rails generate rails_event_store_active_record:migration --data-type=jsonb

Next, configure your event store client to the JSON client:

Rails.configuration.event_store = RailsEventStore::JSONClient.new

If you need additional configuration beyond the included JSON client, continue from here. In your RailsEventStore::Client initialization, set repository serialization to RailsEventStoreActiveRecord::EventRepository.new(serializer: JSON)

# config/environments/*.rb

Rails.application.configure do
config.to_prepare do
Rails.configuration.event_store = RailsEventStore::Client.new(
repository: RailsEventStoreActiveRecord::EventRepository.new(serializer: JSON)
)
end
end