2017-09-04 43 views
0

我需要現在當我的數據庫中某行發生變化時。我正在使用phoenix 1.2.4。我已經有觸發器使用postgres,但實際上我不知道我是否需要它們。在phoenix中獲取更新db(在postgres中)通知

你知道我該如何解決我的問題?

注意:數據庫不一定是從控制器更改,而是我有一個更新某些部分的cron。

+0

我有觸發器,但這些觸發器如何執行靈藥功能? –

回答

2

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