Phoenix.Sync (Phoenix.Sync v0.4.3)

View Source

Real-time sync for Postgres-backed Phoenix applications.

See the docs for more information.

Summary

Functions

Returns the required adapter configuration for your Phoenix Endpoint or Plug.Router.

Types

param_override()

@type param_override() ::
  {:namespace, String.t()}
  | {:table, String.t()}
  | {:where, String.t()}
  | {:columns, String.t()}

param_overrides()

@type param_overrides() :: [param_override()]

shape_definition()

@type shape_definition() :: String.t() | Ecto.Queryable.t() | shape_specification()

shape_specification()

@type shape_specification() :: [
  table: binary(),
  query: atom() | struct(),
  namespace: binary(),
  where: binary(),
  columns: [binary()],
  replica: term(),
  storage: map() | nil
]

Functions

client!()

See Phoenix.Sync.Client.new!/0.

plug_opts()

Returns the required adapter configuration for your Phoenix Endpoint or Plug.Router.

Phoenix

Configure your endpoint with the configuration at runtime by passing the phoenix_sync configuration to your endpoint in the Application.start/2 callback:

def start(_type, _args) do
  children = [
    # ...
    {MyAppWeb.Endpoint, phoenix_sync: Phoenix.Sync.plug_opts()}
  ]
end

Plug

Add the configuration to the Plug opts in your server configuration:

children = [
  {Bandit, plug: {MyApp.Router, phoenix_sync: Phoenix.Sync.plug_opts()}}
]

Your Plug.Router must be configured with copy_opts_to_assign and you should use the rele

defmodule MyApp.Router do
  use Plug.Router, copy_opts_to_assign: :options

  use Phoenix.Sync.Controller
  use Phoenix.Sync.Router

  plug :match
  plug :dispatch

  sync "/shapes/todos", Todos.Todo

  get "/shapes/user-todos" do
    %{"user_id" => user_id} = conn.params
    sync_render(conn, from(t in Todos.Todo, where: t.owner_id == ^user_id)
  end
end