Skip to content

Commit 41b0e10

Browse files
committed
docs
1 parent 37ec06e commit 41b0e10

File tree

2 files changed

+109
-16
lines changed

2 files changed

+109
-16
lines changed

docs/docs/sync.md

+73-15
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ await pg.exec(`
3434
`)
3535
```
3636

37-
You can then use the `syncShapeToTable` method to sync a table from Electric:
37+
You can sync data from Electric using either the single table or multi-table API.
38+
39+
### Single Table Sync
40+
41+
Use the `syncShapeToTable` method to sync a single table from Electric:
3842

3943
```ts
4044
const shape = await pg.electric.syncShapeToTable({
@@ -46,13 +50,47 @@ const shape = await pg.electric.syncShapeToTable({
4650
},
4751
table: 'todo',
4852
primaryKey: ['id'],
53+
shapeKey: 'todo', // or null if the shape state does not need to be persisted
4954
})
55+
56+
// Stop syncing when done
57+
shape.unsubscribe()
5058
```
5159

52-
To stop syncing you can call `unsubscribe` on the shape:
60+
### Multi-Table Sync
61+
62+
The multi-table API ensures transactional consistency across tables by syncing updates that happened in a single transaction in Postgres within a single transaction in PGLite.
63+
64+
Use the `syncShapesToTables` method to sync multiple tables simultaneously:
5365

5466
```ts
55-
shape.unsubscribe()
67+
const sync = await pg.electric.syncShapesToTables({
68+
shapes: {
69+
todos: {
70+
shape: {
71+
url: 'http://localhost:3000/v1/shape',
72+
params: { table: 'todo' }
73+
},
74+
table: 'todo',
75+
primaryKey: ['id'],
76+
},
77+
users: {
78+
shape: {
79+
url: 'http://localhost:3000/v1/shape',
80+
params: { table: 'users' }
81+
},
82+
table: 'users',
83+
primaryKey: ['id'],
84+
}
85+
},
86+
key: 'my-sync', // or null if the sync state does not need to be persisted
87+
onInitialSync: () => {
88+
console.log('Initial sync complete')
89+
}
90+
})
91+
92+
// Stop syncing when done
93+
sync.unsubscribe()
5694
```
5795

5896
There is a full example you can run locally in the [GitHub repository](https://github.com/electric-sql/pglite/tree/main/packages/pglite-sync/example).
@@ -94,18 +132,6 @@ It takes the following options as an object:
94132
- `useCopy: boolean`<br>
95133
Whether to use the `COPY FROM` command to insert the initial data, defaults to `false`. This process may be faster than inserting row by row as it combines the inserts into a CSV to be passed to Postgres.
96134

97-
- `commitGranularity: CommitGranularity`<br>
98-
The granularity of the commit operation, defaults to `"up-to-date"`. Note that a commit will always be performed immediately on the `up-to-date` message.
99-
Options:
100-
101-
- `"up-to-date"`: Commit all messages when the `up-to-date` message is received.
102-
<!-- - `"transaction"`: Commit all messages within transactions as they were applied to the source Postgres. -->
103-
- `"operation"`: Commit each message in its own transaction.
104-
- `number`: Commit every N messages.
105-
106-
- `commitThrottle: number`<br>
107-
The number of milliseconds to wait between commits, defaults to `0`.
108-
109135
- `onInitialSync: () => void`<br>
110136
A callback that is called when the initial sync is complete.
111137

@@ -126,6 +152,38 @@ The returned `shape` object from the `syncShapeToTable` call has the following m
126152
- `stream: ShapeStream`<br>
127153
The underlying `ShapeStream` instance, see the [ShapeStream API](https://electric-sql.com/docs/api/clients/typescript#shapestream) for more details.
128154

155+
## syncShapesToTables API
156+
157+
The `syncShapesToTables` API allows syncing multiple shapes into multiple tables simultaneously while maintaining transactional consistency. It takes the following options:
158+
159+
- `shapes: Record<string, ShapeOptions>`<br>
160+
An object mapping shape names to their configuration options. Each shape configuration includes:
161+
- `shape: ShapeStreamOptions` - The shape stream specification
162+
- `table: string` - The target table name
163+
- `schema?: string` - Optional schema name (defaults to "public")
164+
- `mapColumns?: MapColumns` - Optional column mapping
165+
- `primaryKey: string[]` - Array of primary key columns
166+
167+
- `key: string | null`<br>
168+
Identifier for the multi-shape subscription. If provided, sync state will be persisted to allow resuming between sessions.
169+
170+
- `useCopy?: boolean`<br>
171+
Whether to use `COPY FROM` for faster initial data loading (defaults to false).
172+
173+
- `onInitialSync?: () => void`<br>
174+
Optional callback that fires when initial sync is complete for all shapes.
175+
176+
The returned sync object provides:
177+
178+
- `isUpToDate: boolean`<br>
179+
Whether all shapes have caught up to the main Postgres.
180+
181+
- `streams: Record<string, ShapeStream>`<br>
182+
Access to individual shape streams by their names.
183+
184+
- `unsubscribe()`<br>
185+
Stop syncing all shapes.
186+
129187
## Limitations
130188

131189
- It is currently not possible to sync multiple shapes into the same table, as shape subscriptions require being able to drop all data and start over. We are working on a fix for this case, but the current version will throw if a shape is synced into the same table more than once.

packages/pglite-sync/README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,47 @@ await pg.exec(`
2828
`)
2929
```
3030

31-
You can then use the syncShapeToTable method to sync a table from Electric:
31+
You can sync data from Electric using either the single table or multi-table API:
32+
33+
### Single Table Sync
34+
35+
Use `syncShapeToTable` to sync a single table:
3236

3337
```ts
3438
const shape = await pg.electric.syncShapeToTable({
3539
shape: { url: 'http://localhost:3000/v1/shape', table: 'todo' },
40+
shapeKey: 'todo', // or null if the shape state does not need to be persisted
3641
table: 'todo',
3742
primaryKey: ['id'],
3843
})
3944
```
45+
46+
### Multi-Table Sync
47+
48+
The multi-table API is useful when you need to sync related tables together, ensuring consistency across multiple tables by syncing updates that happened in as single transaction in Postgres within a single transaction in PGLite.
49+
50+
Use `syncShapesToTables` to sync multiple tables simultaneously:
51+
52+
```ts
53+
const sync = await pg.electric.syncShapesToTables({
54+
shapes: {
55+
todos: {
56+
shape: { url: 'http://localhost:3000/v1/shape', table: 'todo' },
57+
table: 'todo',
58+
primaryKey: ['id'],
59+
},
60+
users: {
61+
shape: { url: 'http://localhost:3000/v1/shape', table: 'users' },
62+
table: 'users',
63+
primaryKey: ['id'],
64+
}
65+
},
66+
key: 'my-sync', // or null if the sync state does not need to be persisted
67+
onInitialSync: () => {
68+
console.log('Initial sync complete')
69+
}
70+
})
71+
72+
// Unsubscribe when done
73+
sync.unsubscribe()
74+
```

0 commit comments

Comments
 (0)