Skip to content

Commit 487ceba

Browse files
authored
docs: reorganise the README a touch.
2 parents 8d69981 + ae90c01 commit 487ceba

File tree

2 files changed

+60
-54
lines changed

2 files changed

+60
-54
lines changed

README.md

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ This allows you to define and personalise the shape definition at runtime using
122122

123123
You can sync _into_ any client in any language that [speaks HTTP and JSON](https://electric-sql.com/docs/api/http).
124124

125-
For example, using the Electric [sync_stream_updatescript client](https://electric-sql.com/docs/api/clients/typescript):
125+
For example, using the Electric [Typescript client](https://electric-sql.com/docs/api/clients/typescript):
126126

127-
```sync_stream_updatescript
127+
```typescript
128128
import { Shape, ShapeStream } from "@electric-sql/client";
129129

130130
const stream = new ShapeStream({
@@ -152,54 +152,6 @@ const MyComponent = () => {
152152

153153
See the Electric [demos](https://electric-sql.com/demos) and [documentation](https://electric-sql.com/demos) for more client-side usage examples.
154154

155-
### Shape Definitions
156-
157-
Phoenix.Sync allows shapes to be defined in two ways:
158-
159-
#### As an `Ecto` schema module or `Ecto.Query`
160-
161-
This is the simplest way to
162-
integrate with your existing data model and means you don't have to worry too
163-
much about the lower-level details of the integration.
164-
165-
Examples:
166-
167-
```elixir
168-
169-
sync_render(conn, params, from(t in Todos.Todo, where: t.completed == false))
170-
```
171-
172-
Query support is currently limited to only `where` conditions. Support for more complex queries, including `JOIN`s is planned.
173-
174-
**Note:** The static shapes defined using the `sync/2` or `sync/3` router macros do not accept `Ecto.Query` structs as a shape definition. This is to avoid excessive recompilation caused by having your router having a compile-time dependency on your `Ecto` schemas.
175-
176-
If you want to add a where-clause filter to a static shape in your router, you must add an explicit [`where` clause](https://electric-sql.com/docs/guides/shapes#where-clause) alongside your `Ecto.Schema` module:
177-
178-
```elixir
179-
180-
sync "/incomplete-todos", Todos.Todo, where: "completed = false"
181-
```
182-
183-
You can also include `replica` (see below) in your static shape definitions:
184-
185-
```elixir
186-
187-
sync "/incomplete-todos", Todos.Todo, where: "completed = false", replica: :full
188-
```
189-
190-
#### As a keyword list
191-
192-
At minimum a shape requires a `table`. You can think of shapes defined with
193-
just a table name as the sync-equivalent of `SELECT * FROM table`.
194-
195-
The available options are:
196-
197-
- `table` (required). The Postgres table name. Be aware of casing and [Postgres's handling of unquoted upper-case names](https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_upper_case_table_or_column_names).
198-
- `namespace` (optional). The Postgres namespace that the table belongs to. Defaults to `public`.
199-
- `where` (optional). Filter to apply to the synced data in SQL syntax, e.g. `where: "amount < 1.23 AND colour in ('red', 'green')`.
200-
- `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"]`.
201-
- `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.
202-
203155
## Installation and configuration
204156

205157
`Phoenix.Sync` can be used in two modes:
@@ -363,5 +315,60 @@ Phoenix.Sync uses Electric to handle the core concerns of partial replication, f
363315

364316
Electric defines partial replication using [Shapes](https://electric-sql.com/docs/guides/shapes).
365317

318+
### Shape definitions
319+
320+
Phoenix.Sync allows shapes to be defined in two ways:
321+
322+
1. using an `Ecto.Query` (or `Ecto.Schema` module)
323+
2. using a keyword list
324+
325+
### Using an `Ecto.Query`
326+
366327
Phoenix.Sync maps Ecto queries to shape definitions. This allows you to control what data syncs where using Ecto.Schema and Ecto.Query.
367328

329+
For example, using a query:
330+
331+
```elixir
332+
sync_render(conn, params, from(t in Todos.Todo, where: t.completed == false))
333+
```
334+
335+
Or using a schema (equivalent to `from(t in Todos.Todo)`):
336+
337+
```elixir
338+
sync_render(conn, params, Todos.Todo)
339+
```
340+
341+
Query support is currently limited to `where` conditions. Support for more complex queries using `join`, `order_by`, `limit` and preloaded association graphs is planned and will be added in Q2 2025.
342+
343+
The static shapes defined using the `sync/2` or `sync/3` router macros do not accept `Ecto.Query` structs as a shape definition. This is to avoid excessive recompilation caused by your router having a compile-time dependency on your `Ecto` schemas.
344+
345+
If you want to add a where-clause filter to a static shape in your router, you can add an explicit [`where` clause](https://electric-sql.com/docs/guides/shapes#where-clause) alongside your `Ecto.Schema` module:
346+
347+
```elixir
348+
349+
sync "/incomplete-todos", Todos.Todo, where: "completed = false"
350+
```
351+
352+
You can also include `replica` (see below) in your static shape definitions:
353+
354+
```elixir
355+
356+
sync "/incomplete-todos", Todos.Todo, where: "completed = false", replica: :full
357+
```
358+
359+
For anything else more dyanamic, or to use Ecto queries, you should switch from using the `sync` macros in your router to using `sync_render/3` in a controller.
360+
361+
### Using a keyword list
362+
363+
At minimum a shape requires a `table`. You can think of shapes defined with
364+
just a table name as the sync-equivalent of `SELECT * FROM table`.
365+
366+
The available options are:
367+
368+
- `table` (required). The Postgres table name. Be aware of casing and [Postgres's handling of unquoted upper-case names](https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_upper_case_table_or_column_names).
369+
- `namespace` (optional). The Postgres namespace that the table belongs to. Defaults to `public`.
370+
- `where` (optional). Filter to apply to the synced data in SQL format, e.g. `where: "amount < 1.23 AND colour in ('red', 'green')`.
371+
- `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"]`.
372+
- `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.
373+
374+
See the [Electric Shapes guide](https://electric-sql.com/docs/guides/shapes) for more information.

mix.exs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule Phoenix.Sync.MixProject do
1818
package: package(),
1919
description: description(),
2020
source_url: "https://github.com/electric-sql/phoenix_sync",
21-
homepage_url: "https://electric-sql.com"
21+
homepage_url: "https://hexdocs.pm/phoenix_sync"
2222
]
2323
end
2424

@@ -70,16 +70,15 @@ defmodule Phoenix.Sync.MixProject do
7070
defp package do
7171
[
7272
links: %{
73-
"Electric SQL" => "https://electric-sql.com",
74-
"Github" => "https://github.com/electric-sql/phoenix_sync"
73+
"Source code" => "https://github.com/electric-sql/phoenix_sync"
7574
},
7675
licenses: ["Apache-2.0"],
7776
files: ~w(lib .formatter.exs mix.exs README.md LICENSE)
7877
]
7978
end
8079

8180
defp description do
82-
"Phoenix.Sync enables real-time sync for Postgres-backed Phoenix and Plug applications"
81+
"Real-time sync for Postgres-backed Phoenix applications."
8382
end
8483

8584
defp elixirc_paths(:test), do: ["lib", "test/support"]

0 commit comments

Comments
 (0)