Phoenix.Sync.Client (Phoenix.Sync v0.3.4)

View Source

Summary

Functions

Create a new sync client based on the :phoenix_sync configuration.

Create a sync client using the given options.

Create a new sync client based on the application configuration or raise if the config is invalid.

Create a new sync client based on the given opts or raise if the config is invalid.

Return a sync stream for the given shape.

Functions

new()

Create a new sync client based on the :phoenix_sync configuration.

new(opts)

Create a sync client using the given options.

If the integration mode is set to :embedded and Electric is installed then this will configure the client to retrieve data using the internal Elixir APIs.

For the :http mode, then you must also configure a URL specifying an Electric API server:

config :phoenix_sync,
  mode: :http,
  url: "https://api.electric-sql.cloud"

This client can then generate streams for use in your Elixir applications:

{:ok, client} = Phoenix.Sync.Client.new()
stream = Electric.Client.stream(client, Todos.Todo)
for msg <- stream, do: IO.inspect(msg)

Alternatively use stream/1 which wraps this functionality.

new!()

Create a new sync client based on the application configuration or raise if the config is invalid.

client = Phoenix.Sync.Client.new!()

See new/0.

new!(opts)

Create a new sync client based on the given opts or raise if the config is invalid.

client = Phoenix.Sync.Client.new!(mode: :embedded)

See new/1.

stream(shape, stream_opts \\ [])

Return a sync stream for the given shape.

Examples

# stream updates for the Todo schema
stream = Phoenix.Sync.Client.stream(MyApp.Todos.Todo)

# stream the results of an ecto query
stream = Phoenix.Sync.Client.stream(from(t in MyApp.Todos.Todo, where: t.completed == true))

# create a stream based on a shape definition
stream = Phoenix.Sync.Client.stream(
  table: "todos",
  where: "completed = false",
  columns: ["id", "title"]
)

# once you have a stream, consume it as usual
Enum.each(stream, &IO.inspect/1)

Ecto vs keyword shapes

Streams defined using an Ecto query or schema will return data wrapped in the appropriate schema struct, with values cast to the appropriate Elixir/Ecto types, rather than raw column data in the form %{"column_name" => "column_value"}.