我需要現在當我的數據庫中某行發生變化時。我正在使用phoenix 1.2.4。我已經有觸發器使用postgres,但實際上我不知道我是否需要它們。在phoenix中獲取更新db(在postgres中)通知
你知道我該如何解決我的問題?
注意:數據庫不一定是從控制器更改,而是我有一個更新某些部分的cron。
我需要現在當我的數據庫中某行發生變化時。我正在使用phoenix 1.2.4。我已經有觸發器使用postgres,但實際上我不知道我是否需要它們。在phoenix中獲取更新db(在postgres中)通知
你知道我該如何解決我的問題?
注意:數據庫不一定是從控制器更改,而是我有一個更新某些部分的cron。
幾天前我看到了這個教程(Publish/subscribe with PostgreSQL and Phoenix Framework),它看起來好像包含了你想要的東西。
它設置來自數據庫的通知,然後廣播它。在你的情況下,你只需要通知部分,應該都很好。
我希望幫助:)
Postgrex.Notifications是將使用PostgreSQL聽/通知郵件傳遞到仙丹處理模塊。
一個簡單的例子:
defmodule MyListener do
use GenServer
def start_link(), do: GenServer.start_link(__MODULE__, [])
def init(_arg) do
{:ok, pid} = Postgrex.Notifications.start_link(MyRepo.config())
Postgrex.Notifications.listen(pid, "my_table")
{:ok, []}
end
def handle_info({:notification, _connection_pid, _ref, _channel, payload}, state) do
# ... do something with payload ...
{:noreply, state}
end
end
我有觸發器,但這些觸發器如何執行靈藥功能? –