You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
shapeKey: 'todo', // or null if the shape state does not need to be persisted
49
54
})
55
+
56
+
// Stop syncing when done
57
+
shape.unsubscribe()
50
58
```
51
59
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:
53
65
54
66
```ts
55
-
shape.unsubscribe()
67
+
const sync =awaitpg.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()
56
94
```
57
95
58
96
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:
94
132
-`useCopy: boolean`<br>
95
133
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.
96
134
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
-
109
135
-`onInitialSync: () => void`<br>
110
136
A callback that is called when the initial sync is complete.
111
137
@@ -126,6 +152,38 @@ The returned `shape` object from the `syncShapeToTable` call has the following m
126
152
-`stream: ShapeStream`<br>
127
153
The underlying `ShapeStream` instance, see the [ShapeStream API](https://electric-sql.com/docs/api/clients/typescript#shapestream) for more details.
128
154
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")
-`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
+
129
187
## Limitations
130
188
131
189
- 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.
shapeKey: 'todo', // or null if the shape state does not need to be persisted
36
41
table: 'todo',
37
42
primaryKey: ['id'],
38
43
})
39
44
```
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:
0 commit comments