Zurga

Zurga

EctoSync - sync changes from a cache

Subscribe to events emitted by EctoWatch, and cache the changed row. Use the sync/3 function to update an in-memory row or list of rows using the cached version of the changed row.

This library aims to solve the problem of tracking changes of database rows across processes without causing database stampedes. All possible update paths for each pair of schemas that are connected is determined to avoid brute-forcing through all preloaded associations.

Say you have a Post schema that has many Tags associated through a PostsTags schema and you want to automatically update the Tags for the loaded Posts in a LiveView:

appplication.ex


children: [...,
  {EctoSync, repo: TestRepo, watchers: EctoSync.watchers(Post, assocs: [:tags])},
  ...
  ]

PostLive.ex

alias MyApp.Posts.{Post, PostsTags}

def mount(params, session, socket) do
  posts = list_posts(preload: [:tags])
  if connected?(socket) do
    EctoSync.subscribe(Post, assocs: [:tags])
  end

  {:ok, assign(socket, posts: posts)}
end

def handle_info({{PostsTags, _event}, _} = sync_args, socket) do
  {:noreply, update(socket, :posts, &EctoSync.sync(&1, sync_args))}
end

If you want to add preloads to the changed row, this is possible by adding a Map like this:

EctoSync.sync(post, sync_args, preloads: %{Tag => [:posts]})

It is still in an early stage of development and has not been tested in production. I would love to get feedback on this package.

Package on hex.pm here:

Documentation:

Repo:

Most Liked

Zurga

Zurga

Version 0.2.0 has been released. It brings some changes and supports new features.

Features:

  • The algorithm updating the in-memory rows has been made more efficient by using a graph of the Schemas in the watchers and determining possible paths at application boot time.
  • :through is fully supported.
  • :where is supported on has_one and has_many.

Changes:

  • The messages sent by EctoSync are now a tuple of the following format: {EctoSync, {schema, event, identifiers, ref}}. This allows them to be distinguished from other events that are in the form of tuples.
  • The trigger names in Postgres are shortened automatically to make sure that the character limit is not surpassed.

Future work:

  • A library to provide utility functions to work with Phoenix LiveView
  • Batching system for events.

Where Next?

Popular in Announcing Top

BartOtten
Phoenix Live Favicon Favicon manipulation for Phoenix Live A lib enabling dynamic favicons in Phoenix Live View applications. To sho...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
MRdotB
Greetings Elixir community! Today, I’m thrilled to present you with resvg_nif, an open-source project that provides Elixir bindings for ...
New
frerich
An application wishing to store larger amounts of data typically has two options for doing so: A new column on some table can be introd...
New
fuelen
Hi all! Confispex is a tool which allows defining specs for runtime configuration, cast values according to specified types and inspect ...
New
sevensidedmarble
Announcing Live Toast: a replacement toast/flash component for Phoenix LiveView, heavily inspired by the look of Sonner (the amazing toas...
New
munksgaard
flakify is an igniter installer that allows you to quickly get a Nix flake-based development shell up and running for your Elixir/Phoenix...
New
wingyplus
I just did a dirty hack after seeing Zoi on x.com a few hours ago. Quick Introduction The zoi_defstruct is a library to help you generat...
New
corka149
A JSON patch is a way to define a sequence of manipulating operations on a JavaScript object. The IETF published the RFC 6902 - found he...
New
zoedsoupe
update (since 2025-07-24) the project got forked and rebranded to anubis-mcp, since i not on CloudWalk anymore and can’t ensure they will...
New

Other popular topics Top

freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

We're in Beta

About us Mission Statement