Skip to content

Commit 2be8fb7

Browse files
committed
doc tweaks
1 parent 2993f55 commit 2be8fb7

File tree

7 files changed

+77
-102
lines changed

7 files changed

+77
-102
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Phoenix.Sync
2+
13
<p>
24
<br />
35
<a href="https://hexdocs.pm/phoenix_sync" target="_blank">
@@ -10,8 +12,6 @@
1012
<br />
1113
</p>
1214

13-
# Phoenix.Sync
14-
1515
[![Hex.pm](https://img.shields.io/hexpm/v/phoenix_sync.svg)](https://hex.pm/packages/phoenix_sync)
1616
[![Documentation](https://img.shields.io/badge/docs-hexdocs-green)](https://hexdocs.pm/phoenix_sync)
1717
[![License](https://img.shields.io/badge/license-Apache_2.0-green)](./LICENSE)
@@ -176,8 +176,8 @@ end
176176

177177
# config/config.exs
178178
config :phoenix_sync,
179-
mode: :embedded,
180179
env: config_env(),
180+
mode: :embedded,
181181
repo: MyApp.Repo
182182

183183
# application.ex
@@ -202,8 +202,8 @@ end
202202

203203
# config/config.exs
204204
config :phoenix_sync,
205-
mode: :http,
206205
env: config_env(),
206+
mode: :http,
207207
url: "https://api.electric-sql.cloud",
208208
credentials: [
209209
secret: "...", # required
@@ -233,8 +233,8 @@ end
233233

234234
# config/config.exs
235235
config :phoenix_sync,
236-
mode: :http,
237236
env: config_env(),
237+
mode: :http,
238238
http: [
239239
port: 3000,
240240
],
@@ -272,14 +272,14 @@ end
272272

273273
# config/dev.exs
274274
config :phoenix_sync,
275-
mode: :embedded,
276275
env: config_env(),
276+
mode: :embedded,
277277
repo: MyApp.Repo
278278

279279
# config/test.esx
280280
config :phoenix_sync,
281-
mode: :http,
282281
env: config_env(),
282+
mode: :http,
283283
http: [
284284
port: 3000,
285285
],
@@ -371,4 +371,5 @@ The available options are:
371371
- `columns` (optional). The columns to include in the synced data. By default Electric will include all columns in the table. The column list **must** include all primary keys. E.g. `columns: ["id", "title", "amount"]`.
372372
- `replica` (optional). By default Electric will only send primary keys + changed columns on updates. Set `replica: :full` to receive the full row, not just the changed columns.
373373

374-
See the [Electric Shapes guide](https://electric-sql.com/docs/guides/shapes) for more information.
374+
See the [Electric Shapes guide](https://electric-sql.com/docs/guides/shapes) for more information.
375+

lib/phoenix/sync.ex

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,4 @@
11
defmodule Phoenix.Sync do
2-
@moduledoc """
3-
Wrappers to ease integration of [Electric’s Postgres syncing
4-
service](https://electric-sql.com) with [Phoenix
5-
applications](https://www.phoenixframework.org/).
6-
7-
There are currently 2 integration modes: [`Phoenix.LiveView`
8-
streams](#module-phoenix-liveview-streams) and [configuration
9-
gateway](#module-configuration-gateway).
10-
11-
## Phoenix.LiveView Streams
12-
13-
`Phoenix.Sync.LiveView.sync_stream/4` integrates with
14-
[`Phoenix.LiveView.stream/4`](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#stream/4)
15-
and provides a live updating collection of items.
16-
17-
## Configuration Gateway
18-
19-
Using `Phoenix.Sync.Plug` you can create endpoints that
20-
return configuration information for your Electric Typescript clients. See
21-
[that module's documentation](`Phoenix.Sync.Plug`) for
22-
more information.
23-
24-
## Installation
25-
26-
Add `phoenix_sync` to your application dependencies:
27-
28-
def deps do
29-
[
30-
{:phoenix_sync, "~> 0.1"}
31-
]
32-
end
33-
34-
## Configuration
35-
36-
In your `config/config.exs` or `config/runtime.exs` you **must** configure the
37-
client for the Electric streaming API:
38-
39-
import Config
40-
41-
config :phoenix_sync, Electric.Client,
42-
# one of `base_url` or `endpoint` is required
43-
base_url: System.get_env("ELECTRIC_URL", "http://localhost:3000"),
44-
# endpoint: System.get_env("ELECTRIC_ENDPOINT", "http://localhost:3000/v1/shape"),
45-
# optional
46-
database_id: System.get_env("ELECTRIC_DATABASE_ID", nil)
47-
48-
See the documentation for [`Electric.Client.new/1`](`Electric.Client.new/1`)
49-
for information on the client configuration.
50-
51-
## Embedding Electric
52-
53-
**TODO**
54-
"""
55-
562
alias Electric.Client.ShapeDefinition
573

584
@shape_keys [:namespace, :where, :columns]
@@ -73,31 +19,10 @@ defmodule Phoenix.Sync do
7319
@type param_overrides :: [param_override()]
7420

7521
defdelegate plug_opts(), to: Phoenix.Sync.Application
76-
@doc false
77-
defdelegate plug_opts(opts), to: Phoenix.Sync.Application
78-
79-
@doc """
80-
Create a new `Electric.Client` instance based on the application config.
81-
82-
To connect your live streams to an externally hosted Electric instance over
83-
HTTP, configure your app with the URL of the Electric server:
8422

85-
# dev.exs
86-
config :phoenix_sync, Electric.Client,
87-
base_url: "http://localhost:3000"
23+
defdelegate client!(), to: Phoenix.Sync.Client, as: :new!
8824

89-
If Electric is installed as a dependency of your app, and you wish to connect
90-
your live streams to this internal application, then you don't need to configure
91-
anything -- `client!/0` will return an `Electric.Client` instance configured to
92-
data directly from the running Electric application.
93-
94-
See [`Electric.Client.new/1`](`Electric.Client.new/1`) for the available
95-
options.
96-
97-
"""
98-
defdelegate client!, to: Phoenix.Sync.Client, as: :new!
99-
100-
@doc """
25+
_ = """
10126
Use request query parameters to create a `Electric.Client.ShapeDefinition`.
10227
10328
Useful when creating authorization endpoints that validate a user's access to
@@ -149,9 +74,10 @@ defmodule Phoenix.Sync do
14974
{:ok, %Electric.Client.ShapeDefinition{table: "things"}}
15075
15176
"""
77+
78+
@doc false
15279
@spec shape_from_params(Plug.Conn.t() | Plug.Conn.params(), overrides :: param_overrides()) ::
15380
{:ok, Electric.Client.ShapeDefinition.t()} | {:error, String.t()}
154-
15581
def shape_from_params(conn_or_map, overrides \\ [])
15682

15783
def shape_from_params(%Plug.Conn{} = conn, overrides) do

lib/phoenix/sync/client.ex

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule Phoenix.Sync.Client do
2828
2929
This client can then generate streams for use in your Elixir applications:
3030
31-
client = Phoenix.Sync.Client.new()
31+
{:ok, client} = Phoenix.Sync.Client.new()
3232
stream = Electric.Client.stream(client, Todos.Todo)
3333
for msg <- stream, do: IO.inspect(msg)
3434
@@ -40,6 +40,14 @@ defmodule Phoenix.Sync.Client do
4040
apply(adapter, :client, [env, opts])
4141
end
4242

43+
@doc """
44+
Create a new sync client based on the application configuration or raise if
45+
the config is invalid.
46+
47+
client = Phoenix.Sync.Client.new!()
48+
49+
See `new/0`.
50+
"""
4351
def new! do
4452
case new() do
4553
{:ok, client} ->
@@ -50,6 +58,14 @@ defmodule Phoenix.Sync.Client do
5058
end
5159
end
5260

61+
@doc """
62+
Create a new sync client based on the given opts or raise if
63+
the config is invalid.
64+
65+
client = Phoenix.Sync.Client.new!(mode: :embedded)
66+
67+
See `new/1`.
68+
"""
5369
def new!(opts) do
5470
case new(opts) do
5571
{:ok, client} ->
@@ -95,6 +111,7 @@ defmodule Phoenix.Sync.Client do
95111
stream(shape, stream_opts, nil)
96112
end
97113

114+
@doc false
98115
def stream(shape, stream_opts, sync_opts) do
99116
client = new!(sync_opts)
100117
{shape, shape_stream_opts} = resolve_shape(shape)

lib/phoenix/sync/electric.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule Phoenix.Sync.Electric do
1515
Before configuring your router, you must install and configure the
1616
`:phoenix_sync` application.
1717
18-
See the documentation for [embedding electric](`Phoenix.Sync`) for
18+
See the documentation for [embedding electric](readme.html#installation-and-configuration) for
1919
details on embedding Electric into your Elixir application.
2020
2121
## Plug Integration

lib/phoenix/sync/live_view.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ defmodule Phoenix.Sync.LiveView do
346346
component_event(component: component, event: event)
347347
end
348348

349-
@doc """
349+
_ = """
350350
Embed client configuration for a shape into your HTML.
351351
352352
<Phoenix.Sync.LiveView.electric_client_configuration
@@ -396,6 +396,8 @@ defmodule Phoenix.Sync.LiveView do
396396
);
397397
</script>
398398
"""
399+
400+
@doc false
399401
attr :shape, :any, required: true, doc: "The Ecto query (or schema module) to subscribe to"
400402

401403
attr :key, :string,

lib/phoenix/sync/plug.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Phoenix.Sync.Plug do
2-
@moduledoc """
2+
@moduledoc false
3+
_ = """
34
Provides an configuration endpoint for use in your Phoenix applications.
45
56
Rather than configuring your [Electric Typescript

lib/phoenix/sync/router.ex

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,42 @@ defmodule Phoenix.Sync.Router do
66
## Phoenix Integration
77
88
When using within a Phoenix application, you should just import the macros
9-
defined here
9+
defined here in your `Phoenix.Router` module:
1010
11-
import #{__MODULE__}
11+
defmodule MyAppWeb.Router do
12+
use Phoenix.Router
13+
14+
import #{__MODULE__}
15+
16+
scope "/shapes" do
17+
sync "/all-todos", MyApp.Todos.Todo
18+
19+
sync "/pending-todos", MyApp.Todos.Todo,
20+
where: "completed = false"
21+
end
22+
end
1223
1324
## Plug Integration
1425
15-
Within Plug applications you need to do a little more work.
26+
Within your `Plug.Router` module, `use #{__MODULE__}` and then
27+
add your `sync` routes:
28+
29+
defmodule MyApp.Plug.Router do
30+
use Plug.Router, copy_opts_to_assign: :options
31+
use #{__MODULE__}
32+
33+
plug :match
34+
plug :dispatch
35+
36+
sync "/shapes/all-todos", MyApp.Todos.Todo
1637
17-
TODO
38+
sync "/shapes/pending-todos", MyApp.Todos.Todo,
39+
where: "completed = false"
40+
end
1841
42+
You **must** use the `copy_opts_to_assign` option in `Plug.Router` in order
43+
for the `sync` macro to get the configuration defined in your
44+
`application.ex` [`start/2`](`c:Application.start/2`) callback.
1945
"""
2046

2147
import Phoenix.Sync.Plug.Utils
@@ -63,15 +89,9 @@ defmodule Phoenix.Sync.Router do
6389
6490
sync "/incomplete-todos", table: "todos", where: "completed = false"
6591
66-
## Options
67-
68-
* `:table` - (required if no schema provided) The database table to expose
69-
* `:namespace` - (optional) The namespace for the table
70-
* `:where` - (optional) The where clause to apply to the data
71-
* `:columns` - (optional) Subset of table columns to include in the streamed data (default all columns)
72-
* `:storage` - (optional) Storage configuration
73-
* `:replica` - (optional) Replication strategy
7492
93+
See [the section on Shape definitions](readme.html#shape-definitions) for
94+
more details on keyword-based shapes.
7595
"""
7696
defmacro sync(path, opts) when is_list(opts) do
7797
route(env!(__CALLER__), path, build_definition(path, __CALLER__, opts))
@@ -82,6 +102,14 @@ defmodule Phoenix.Sync.Router do
82102
route(env!(__CALLER__), path, build_shape_from_query(queryable, __CALLER__, []))
83103
end
84104

105+
@doc """
106+
Create a synchronization route from an `Ecto.Schema` plus shape options.
107+
108+
sync "/my-shape", MyApp.Todos.Todo,
109+
where: "completed = false"
110+
111+
See `sync/2`.
112+
"""
85113
# e.g. shape "/path", Ecto.Query.from(t in MyTable), replica: :full
86114
defmacro sync(path, queryable, opts) when is_tuple(queryable) and is_list(opts) do
87115
route(env!(__CALLER__), path, build_shape_from_query(queryable, __CALLER__, opts))

0 commit comments

Comments
 (0)