Skip to content

Commit 46b052f

Browse files
docs: update examples to use new explicit sync pattern instead of deprecated return values
- Update ElectricCollectionConfig examples to use await collection.utils.awaitTxId() - Update overview.md example to use explicit sync awaiting - Update mutations.md to show cleaner pattern for awaiting txids - Update examples/react/projects/README.md to use collection.utils.refetch() Co-authored-by: Kyle Mathews <KyleAMathews@users.noreply.github.com>
1 parent 2716d59 commit 46b052f

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

docs/guides/mutations.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,14 +453,13 @@ onUpdate: async ({ transaction, collection }) => {
453453
**ElectricCollection** - wait for txid(s) to sync:
454454
```typescript
455455
onUpdate: async ({ transaction, collection }) => {
456-
const txids = await Promise.all(
456+
await Promise.all(
457457
transaction.mutations.map(async (mutation) => {
458458
const response = await api.todos.update(mutation.original.id, mutation.changes)
459-
return response.txid
459+
// Wait for this txid to sync
460+
await collection.utils.awaitTxId(response.txid)
460461
})
461462
)
462-
// Wait for all txids to sync
463-
await Promise.all(txids.map(txid => collection.utils.awaitTxId(txid)))
464463
}
465464
```
466465

docs/overview.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,11 @@ export const todoCollection = createCollection(
498498
},
499499
getKey: (item) => item.id,
500500
schema: todoSchema,
501-
onInsert: async ({ transaction }) => {
501+
onInsert: async ({ transaction, collection }) => {
502502
const response = await api.todos.create(transaction.mutations[0].modified)
503503

504-
return { txid: response.txid }
504+
// Wait for txid to sync
505+
await collection.utils.awaitTxId(response.txid)
505506
},
506507
// You can also implement onUpdate, onDelete as needed.
507508
})

docs/reference/electric-db-collection/interfaces/ElectricCollectionConfig.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ Promise resolving to { txid, timeout? } or void
7070

7171
```ts
7272
// Basic Electric delete handler with txid (recommended)
73-
onDelete: async ({ transaction }) => {
73+
onDelete: async ({ transaction, collection }) => {
7474
const mutation = transaction.mutations[0]
7575
const result = await api.todos.delete({
7676
id: mutation.original.id
7777
})
78-
return { txid: result.txid }
78+
// Wait for txid to sync
79+
await collection.utils.awaitTxId(result.txid)
7980
}
8081
```
8182

@@ -122,34 +123,39 @@ Promise resolving to { txid, timeout? } or void
122123

123124
```ts
124125
// Basic Electric insert handler with txid (recommended)
125-
onInsert: async ({ transaction }) => {
126+
onInsert: async ({ transaction, collection }) => {
126127
const newItem = transaction.mutations[0].modified
127128
const result = await api.todos.create({
128129
data: newItem
129130
})
130-
return { txid: result.txid }
131+
// Wait for txid to sync
132+
await collection.utils.awaitTxId(result.txid)
131133
}
132134
```
133135

134136
```ts
135137
// Insert handler with custom timeout
136-
onInsert: async ({ transaction }) => {
138+
onInsert: async ({ transaction, collection }) => {
137139
const newItem = transaction.mutations[0].modified
138140
const result = await api.todos.create({
139141
data: newItem
140142
})
141-
return { txid: result.txid, timeout: 10000 } // Wait up to 10 seconds
143+
// Wait for txid to sync with custom timeout (10 seconds)
144+
await collection.utils.awaitTxId(result.txid, 10000)
142145
}
143146
```
144147

145148
```ts
146-
// Insert handler with multiple items - return array of txids
147-
onInsert: async ({ transaction }) => {
149+
// Insert handler with multiple items - wait for all txids
150+
onInsert: async ({ transaction, collection }) => {
148151
const items = transaction.mutations.map(m => m.modified)
149152
const results = await Promise.all(
150153
items.map(item => api.todos.create({ data: item }))
151154
)
152-
return { txid: results.map(r => r.txid) }
155+
// Wait for all txids to sync
156+
await Promise.all(
157+
results.map(r => collection.utils.awaitTxId(r.txid))
158+
)
153159
}
154160
```
155161

@@ -196,13 +202,14 @@ Promise resolving to { txid, timeout? } or void
196202

197203
```ts
198204
// Basic Electric update handler with txid (recommended)
199-
onUpdate: async ({ transaction }) => {
205+
onUpdate: async ({ transaction, collection }) => {
200206
const { original, changes } = transaction.mutations[0]
201207
const result = await api.todos.update({
202208
where: { id: original.id },
203209
data: changes
204210
})
205-
return { txid: result.txid }
211+
// Wait for txid to sync
212+
await collection.utils.awaitTxId(result.txid)
206213
}
207214
```
208215

examples/react/projects/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,15 @@ export const todoCollection = createCollection(
201201
queryClient,
202202
schema: todoSchema,
203203
getKey: (item) => item.id,
204-
onInsert: async ({ transaction }) => {
204+
onInsert: async ({ transaction, collection }) => {
205205
const { modified: newTodo } = transaction.mutations[0]
206-
const result = await trpc.todos.create.mutate({
206+
await trpc.todos.create.mutate({
207207
text: newTodo.text,
208208
completed: newTodo.completed,
209209
project_id: newTodo.project_id,
210210
})
211-
return { txid: result.txid }
211+
// Trigger refetch to sync server state
212+
await collection.utils.refetch()
212213
},
213214
// You can also implement onUpdate, onDelete as needed
214215
})

0 commit comments

Comments
 (0)