evoq_event_upcaster behaviour (evoq v1.14.1)

View Source

Event upcaster behavior for schema evolution.

Upcasters transform old event versions to the current version, enabling backwards-compatible schema changes.

Usage

1. Create an upcaster module for each event type that needs transformation 2. Implement upcast/2 to transform the event 3. Register the upcaster with evoq_type_provider

Example

  -module(account_created_v1_upcaster).
  -behaviour(evoq_event_upcaster).
 
  -export([upcast/2, version/0]).
 
  version() -> 1.
 
  upcast(#{event_type := <<"AccountCreated">>, data := Data} = Event, _Meta) ->
      %% Add default email if missing (v1 -> v2)
      NewData = maps:put(email, <<"unknown@example.com">>, Data),
      {ok, Event#{data := NewData}}.

Summary

Functions

Apply a chain of upcasters to an event. Upcasters are applied in order. Each upcaster transforms the event for the next one in the chain.

Apply a single upcaster to an event.

Callbacks

upcast/2

-callback upcast(Event :: map(), Metadata :: map()) ->
                    {ok, TransformedEvent :: map()} |
                    {ok, TransformedEvent :: map(), NewEventType :: binary()} |
                    skip.

version/0

(optional)
-callback version() -> pos_integer().

Functions

chain_upcasters(Rest, Event, Metadata)

-spec chain_upcasters([atom()], map(), map()) -> {ok, map()} | {ok, map(), binary()} | skip.

Apply a chain of upcasters to an event. Upcasters are applied in order. Each upcaster transforms the event for the next one in the chain.

upcast(UpcasterModule, Event, Metadata)

-spec upcast(atom(), map(), map()) -> {ok, map()} | {ok, map(), binary()} | skip.

Apply a single upcaster to an event.