Zurga

Zurga

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:

https://github.com/Zurga/ecto_sync

Showing Posts 1 to 3

Zurga

Zurga OP

Just updated to 0.1.4!

Changes:

  • Has :where filters are now also supported.
  • :insert events are broadcast whenever an association has moved to another parent. So if %Post{person_id: 1} was updated to %Post{person_id: 2}, an insert event is published for all processes listening to {{Post, :inserted}, {person_id: 2}}

Working on tests for HasThrough, should be supported soon.

Zurga

Zurga OP

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.
Zurga

Zurga OP

Version 0.5.0 has been released!

It now features support for reading the WAL in Postgres and publishing sync events from there. There is a Repo module that can be used as well that will prevent the process making the changes to the database from receiving a message about that change. To support insert you will need to use binary_id for the primary key.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
quatermain
Hello, I’m sharing my plugin here in forum after some time so it has time to mature and proof yourself. I use Claude Code daily on a pr...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New
LostKobrakai
I’ve recently created a small library phoenix_vite integrating the vite build tooling with phoenix. It provides an igniter.installer t...
New
danschultzer
I needed something like premailer for rails, and after searching, I decided to make an easy-to-use library to handle inline styles and co...
New
kraleppa
If you’ve ever had to debug a BEAM node you know it can be a painful process. Parsing raw data, or dealing with clutter often gets in the...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews