Just added some DSL magic to WalEx. WalEx allows you to listen to change events on your Postgres tables then perform callback-like actions with the data. It includes a diff of the data as well so that you can match on specific changes.
The codebase was originally based off of Supabase’s excellent realtime library which I then later refactored to use Postgrex’s new Replication capabilities. The reason I pulled it out into it’s own library is I wanted to work with the data directly in Elixir as well as track diffs.
So now you can set up an event listener like this:
defmodule MyApp.UserEvent do
use WalEx.Event, name: MyApp
# any event
on_event(:user, fn {:ok, user} ->
IO.inspect(on_event: user)
# do something with user data (Event Struct)
end)
on_insert(:user, fn {:ok, user} ->
IO.inspect(on_insert: user)
end)
on_update(:user, fn {:ok, user} ->
IO.inspect(on_update: user)
end)
on_delete(:user, fn {:ok, user} ->
IO.inspect(on_delete: user)
end)
I’m also considering adding a configuration-only option so that non-Elixir folks can use the library to listen to Postgres changes and send them wherever they need. Feedback and ideas on how I can improve WalEx is much appreciated!