electric icon indicating copy to clipboard operation
electric copied to clipboard

Support mapping change stream to an Ecto.Schema embedded struct

Open magnetised opened this issue 9 months ago • 0 comments

Currently we only support db-backed ecto.schemas, but we can leverage the type casting powers of ecto by supporting embedded schemas and mapping any data to ecto schema structs

defmodule MyModel do
  use Ecto.Schema
  embedded_schema do
    field :name, :string
    field :value, :integer
  end
end

{:ok, client} = Electric.Client.new(base_url: "http://localhost:3000")
stream = Electric.Client.stream(client, "model", schema: MyModel)

This separates the pg table from the local data structures, which may not have a pg connection or any db-backed ecto schemas but allows for receiving nice %MyModel{} structs from the stream.

magnetised avatar Apr 03 '25 13:04 magnetised