Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

ElectricCollectionConfig.md

250 lines6.7 KB

1---
2id: ElectricCollectionConfig
3title: ElectricCollectionConfig
4---
5​
6Defined in: [packages/electric-db-collection/src/electric.ts:351](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L351)
7​
8Configuration interface for Electric collection options
9​
10## Extends
11​
12- `Omit`\<`BaseCollectionConfig`\<`T`, `string` \| `number`, `TSchema`, [`ElectricCollectionUtils`](ElectricCollectionUtils.md)\<`T`\>, `any`\>, `"onInsert"` \| `"onUpdate"` \| `"onDelete"` \| `"syncMode"`\>
13​
14## Type Parameters
15​
16### T
17​
18`T` *extends* `Row`\<`unknown`\> = `Row`\<`unknown`\>
19​
20The type of items in the collection
21​
22### TSchema
23​
24`TSchema` *extends* `StandardSchemaV1` = `never`
25​
26The schema type for validation
27​
28## Properties
29​
30### \[ELECTRIC\_TEST\_HOOKS\]?
31​
32```ts
33optional [ELECTRIC_TEST_HOOKS]: ElectricTestHooks;
34```
35​
36Defined in: [packages/electric-db-collection/src/electric.ts:374](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L374)
37​
38Internal test hooks (for testing only)
39Hidden via Symbol to prevent accidental usage in production
40​
41***
42​
43### onDelete()?
44​
45```ts
46optional onDelete: (params) => Promise<MatchingStrategy>;
47```
48​
49Defined in: [packages/electric-db-collection/src/electric.ts:491](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L491)
50​
51Optional asynchronous handler function called before a delete operation
52​
53#### Parameters
54​
55##### params
56​
57`DeleteMutationFnParams`\<`T`, `string` \| `number`, [`ElectricCollectionUtils`](ElectricCollectionUtils.md)\<`T`\>\>
58​
59Object containing transaction and collection information
60​
61#### Returns
62​
63`Promise`\<`MatchingStrategy`\>
64​
65Promise that should resolve after synchronization is complete. Returning
66`{ txid }` is deprecated; await `collection.utils.awaitTxId(txid)` instead.
67​
68#### Examples
69​
70```ts
71// Basic Electric delete handler with explicit synchronization
72onDelete: async ({ transaction, collection }) => {
73 const mutation = transaction.mutations[0]
74 const result = await api.todos.delete({
75 id: mutation.original.id
76 })
77 // Wait for txid to sync
78 await collection.utils.awaitTxId(result.txid)
80```
81​
82```ts
83// Use awaitMatch utility for custom matching
84onDelete: async ({ transaction, collection }) => {
85 const mutation = transaction.mutations[0]
86 await api.todos.delete({ id: mutation.original.id })
87 await collection.utils.awaitMatch(
88 (message) => isChangeMessage(message) &&
89 message.headers.operation === 'delete' &&
90 message.value.id === mutation.original.id
91 )
93```
94​
95***
96​
97### onInsert()?
98​
99```ts
100optional onInsert: (params) => Promise<MatchingStrategy>;
101```
102​
103Defined in: [packages/electric-db-collection/src/electric.ts:422](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L422)
104​
105Optional asynchronous handler function called before an insert operation
106​
107#### Parameters
108​
109##### params
110​
111`InsertMutationFnParams`\<`T`, `string` \| `number`, [`ElectricCollectionUtils`](ElectricCollectionUtils.md)\<`T`\>\>
112​
113Object containing transaction and collection information
114​
115#### Returns
116​
117`Promise`\<`MatchingStrategy`\>
118​
119Promise that should resolve after synchronization is complete. Returning
120`{ txid }` is deprecated; await `collection.utils.awaitTxId(txid)` instead.
121​
122#### Examples
123​
124```ts
125// Basic Electric insert handler with explicit synchronization
126onInsert: async ({ transaction, collection }) => {
127 const newItem = transaction.mutations[0].modified
128 const result = await api.todos.create({
129 data: newItem
130 })
131 // Wait for txid to sync
132 await collection.utils.awaitTxId(result.txid)
134```
135​
136```ts
137// Insert handler with custom timeout
138onInsert: async ({ transaction, collection }) => {
139 const newItem = transaction.mutations[0].modified
140 const result = await api.todos.create({
141 data: newItem
142 })
143 // Wait for txid to sync with custom timeout (10 seconds)
144 await collection.utils.awaitTxId(result.txid, 10000)
146```
147​
148```ts
149// Insert handler with multiple items - wait for all txids
150onInsert: async ({ transaction, collection }) => {
151 const items = transaction.mutations.map(m => m.modified)
152 const results = await Promise.all(
153 items.map(item => api.todos.create({ data: item }))
154 )
155 // Wait for all txids to sync
156 await Promise.all(
157 results.map(r => collection.utils.awaitTxId(r.txid))
158 )
160```
161​
162```ts
163// Use awaitMatch utility for custom matching
164onInsert: async ({ transaction, collection }) => {
165 const newItem = transaction.mutations[0].modified
166 await api.todos.create({ data: newItem })
167 await collection.utils.awaitMatch(
168 (message) => isChangeMessage(message) &&
169 message.headers.operation === 'insert' &&
170 message.value.name === newItem.name
171 )
173```
174​
175***
176​
177### onUpdate()?
178​
179```ts
180optional onUpdate: (params) => Promise<MatchingStrategy>;
181```
182​
183Defined in: [packages/electric-db-collection/src/electric.ts:457](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L457)
184​
185Optional asynchronous handler function called before an update operation
186​
187#### Parameters
188​
189##### params
190​
191`UpdateMutationFnParams`\<`T`, `string` \| `number`, [`ElectricCollectionUtils`](ElectricCollectionUtils.md)\<`T`\>\>
192​
193Object containing transaction and collection information
194​
195#### Returns
196​
197`Promise`\<`MatchingStrategy`\>
198​
199Promise that should resolve after synchronization is complete. Returning
200`{ txid }` is deprecated; await `collection.utils.awaitTxId(txid)` instead.
201​
202#### Examples
203​
204```ts
205// Basic Electric update handler with explicit synchronization
206onUpdate: async ({ transaction, collection }) => {
207 const { original, changes } = transaction.mutations[0]
208 const result = await api.todos.update({
209 where: { id: original.id },
210 data: changes
211 })
212 // Wait for txid to sync
213 await collection.utils.awaitTxId(result.txid)
215```
216​
217```ts
218// Use awaitMatch utility for custom matching
219onUpdate: async ({ transaction, collection }) => {
220 const { original, changes } = transaction.mutations[0]
221 await api.todos.update({ where: { id: original.id }, data: changes })
222 await collection.utils.awaitMatch(
223 (message) => isChangeMessage(message) &&
224 message.headers.operation === 'update' &&
225 message.value.id === original.id
226 )
228```
229​
230***
231​
232### shapeOptions
233​
234```ts
235shapeOptions: ShapeStreamOptions<GetExtensions<T>>;
236```
237​
238Defined in: [packages/electric-db-collection/src/electric.ts:367](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L367)
239​
240Configuration options for the ElectricSQL ShapeStream
241​
242***
243​
244### syncMode?
245​
246```ts
247optional syncMode: ElectricSyncMode;
248```
249​
250Defined in: [packages/electric-db-collection/src/electric.ts:368](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L368)