elixir-sqlite / exqlite

An SQLite3 driver for Elixir
https://hexdocs.pm/exqlite
MIT License
217 stars 48 forks source link

Feature request: Add support for Data Change Notification Callbacks #221

Closed OldhamMade closed 1 year ago

OldhamMade commented 2 years ago

It would be great if exqlite supported Data Change Notification Callbacks, eg. set_update_hook, similar to mmzeeman/esqlite. esqlite is great, however it is very Erlang-focused and the original Elixir wrapper library for it has been abandoned. It would be very useful to be able to act on hooks within a project that is using ecto_sqlite3.

warmwaffles commented 2 years ago

I've contemplated adding that functionality. Although I am not sure how to make that jive with how ecto works.

I think on the adapter level, we could add it, but the main issue is how to handle it when the DbConnection pools the connections to the database.

Can you provide me a concrete use case that this would fit in to?

ruslandoga commented 1 year ago

@warmwaffles I wonder if it can be a separate connection behaviour like postgrex/replication_connection.ex

children = [
  App.DataChangeConnection, # something like Postgrex.ReplicationConnection with some user defined callbacks
  App.Repo, # ecto_sqlite3 repo
  # ...
]

Supervisor.start_link(children, ...)

Hm, by sqlite3_update_hook docs it appears it's limited to only the current connection changes. Then this approach won't work as App.DataChangeConnection won't receive any updates.

warmwaffles commented 1 year ago

I'm not entirely sure how this would function because the update hooks would only work on the currently connected db connection / exqlite connection.

Definitely an interesting use case that doesn't fit the current paradigm that DbConnection and Ecto have defined.

OldhamMade commented 1 year ago

Can you provide me a concrete use case that this would fit in to?

It would allow someone using a SQLite database to get close to mimicking the LISTEN/NOTIFY functionality from Postgres, which is my specific use-case: I'd like to act on table updates caused by DB triggers from within Elixir code.

Although I am not sure how to make that jive with how ecto works.

I'm not sure that it needs to. I think it is more akin to Postgrex.Notifications, a useful extra.

warmwaffles commented 1 year ago

I didn't realize Postgrex had that ability. I'll take a look at how they resolved the LISTEN / NOTIFY. We may be able to utilize a similar functionality.

mdwaud commented 1 year ago

+1 for interest in this capability