elixir-sqlite / exqlite

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

Add set_update_hook/2 #260

Closed greg-rychlewski closed 1 year ago

greg-rychlewski commented 1 year ago

Closes https://github.com/elixir-sqlite/exqlite/issues/221

I took a stab at this based on how the erlang sqlite library is handling it.

The implementation is pretty straight forward. The user supplies a connection and a pid and we create a callback that sends a message to the pid of the form {:insert | :update | :delete, db_name, table, row_id} (that's all the information SQLite exposes about the change. For example, you could create a GenServer to listen to the notifications.

Right now, there can only be one pid per database connection. But I think it can probably be extended later to work with a list of pids.

greg-rychlewski commented 1 year ago

This is exposed on the driver level but I think if someone really wanted to use it with Ecto they could use the after_connect configuration option and supply {Exqlite.Sqlite3, :set_update_hook, [genserver_name]}.

I believe this will work and also be called on reconnects but haven't tested. That's why I didn't advertise it in the docs yet.

greg-rychlewski commented 1 year ago

Oh I was looking into whether Ecto passes the repo into the connect function. Would you be interested in a separate PR for that if it works?

warmwaffles commented 1 year ago

Yea I think that would be fine. I have a feeling it may be a breaking api change. Not entirely sure.