WalEx - Listen to Postgres change events and do stuff with the data directly in Elixir

Just added some DSL magic :magic_wand: 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!

15 Likes

Now all end user needs to do is add their setting to a yaml config to send database change events to EventRelay (via WalEx): https://github.com/eventrelay/eventsql/blob/master/config.yaml

1 Like