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

ElectricCollectionConfig.md

242 lines6.1 KB

1---
2id: ElectricCollectionConfig
3title: ElectricCollectionConfig
4---
5​
6# Interface: ElectricCollectionConfig\<T, TSchema\>
7​
8Defined in: [packages/electric-db-collection/src/electric.ts:148](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L148)
9​
10Configuration interface for Electric collection options
11​
12## Extends
13​
14- `Omit`\<`BaseCollectionConfig`\<`T`, `string` \| `number`, `TSchema`, [`ElectricCollectionUtils`](ElectricCollectionUtils.md)\<`T`\>, `any`\>, `"onInsert"` \| `"onUpdate"` \| `"onDelete"` \| `"syncMode"`\>
15​
16## Type Parameters
17​
18### T
19​
20`T` *extends* `Row`\<`unknown`\> = `Row`\<`unknown`\>
21​
22The type of items in the collection
23​
24### TSchema
25​
26`TSchema` *extends* `StandardSchemaV1` = `never`
27​
28The schema type for validation
29​
30## Properties
31​
32### \[ELECTRIC\_TEST\_HOOKS\]?
33​
34```ts
35optional [ELECTRIC_TEST_HOOKS]: ElectricTestHooks;
36```
37​
38Defined in: [packages/electric-db-collection/src/electric.ts:171](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L171)
39​
40Internal test hooks (for testing only)
41Hidden via Symbol to prevent accidental usage in production
42​
43***
44​
45### onDelete()?
46​
47```ts
48optional onDelete: (params) => Promise<MatchingStrategy>;
49```
50​
51Defined in: [packages/electric-db-collection/src/electric.ts:288](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L288)
52​
53Optional asynchronous handler function called before a delete operation
54​
55#### Parameters
56​
57##### params
58​
59`DeleteMutationFnParams`\<`T`, `string` \| `number`, [`ElectricCollectionUtils`](ElectricCollectionUtils.md)\<`T`\>\>
60​
61Object containing transaction and collection information
62​
63#### Returns
64​
65`Promise`\<`MatchingStrategy`\>
66​
67Promise resolving to { txid, timeout? } or void
68​
69#### Examples
70​
71```ts
72// Basic Electric delete handler with txid (recommended)
73onDelete: async ({ transaction }) => {
74 const mutation = transaction.mutations[0]
75 const result = await api.todos.delete({
76 id: mutation.original.id
77 })
78 return { txid: 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:219](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L219)
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 resolving to { txid, timeout? } or void
120​
121#### Examples
122​
123```ts
124// Basic Electric insert handler with txid (recommended)
125onInsert: async ({ transaction }) => {
126 const newItem = transaction.mutations[0].modified
127 const result = await api.todos.create({
128 data: newItem
129 })
130 return { txid: result.txid }
132```
133​
134```ts
135// Insert handler with custom timeout
136onInsert: async ({ transaction }) => {
137 const newItem = transaction.mutations[0].modified
138 const result = await api.todos.create({
139 data: newItem
140 })
141 return { txid: result.txid, timeout: 10000 } // Wait up to 10 seconds
143```
144​
145```ts
146// Insert handler with multiple items - return array of txids
147onInsert: async ({ transaction }) => {
148 const items = transaction.mutations.map(m => m.modified)
149 const results = await Promise.all(
150 items.map(item => api.todos.create({ data: item }))
151 )
152 return { txid: results.map(r => r.txid) }
154```
155​
156```ts
157// Use awaitMatch utility for custom matching
158onInsert: async ({ transaction, collection }) => {
159 const newItem = transaction.mutations[0].modified
160 await api.todos.create({ data: newItem })
161 await collection.utils.awaitMatch(
162 (message) => isChangeMessage(message) &&
163 message.headers.operation === 'insert' &&
164 message.value.name === newItem.name
165 )
167```
168​
169***
170​
171### onUpdate()?
172​
173```ts
174optional onUpdate: (params) => Promise<MatchingStrategy>;
175```
176​
177Defined in: [packages/electric-db-collection/src/electric.ts:254](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L254)
178​
179Optional asynchronous handler function called before an update operation
180​
181#### Parameters
182​
183##### params
184​
185`UpdateMutationFnParams`\<`T`, `string` \| `number`, [`ElectricCollectionUtils`](ElectricCollectionUtils.md)\<`T`\>\>
186​
187Object containing transaction and collection information
188​
189#### Returns
190​
191`Promise`\<`MatchingStrategy`\>
192​
193Promise resolving to { txid, timeout? } or void
194​
195#### Examples
196​
197```ts
198// Basic Electric update handler with txid (recommended)
199onUpdate: async ({ transaction }) => {
200 const { original, changes } = transaction.mutations[0]
201 const result = await api.todos.update({
202 where: { id: original.id },
203 data: changes
204 })
205 return { txid: result.txid }
207```
208​
209```ts
210// Use awaitMatch utility for custom matching
211onUpdate: async ({ transaction, collection }) => {
212 const { original, changes } = transaction.mutations[0]
213 await api.todos.update({ where: { id: original.id }, data: changes })
214 await collection.utils.awaitMatch(
215 (message) => isChangeMessage(message) &&
216 message.headers.operation === 'update' &&
217 message.value.id === original.id
218 )
220```
221​
222***
223​
224### shapeOptions
225​
226```ts
227shapeOptions: ShapeStreamOptions<GetExtensions<T>>;
228```
229​
230Defined in: [packages/electric-db-collection/src/electric.ts:164](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L164)
231​
232Configuration options for the ElectricSQL ShapeStream
233​
234***
235​
236### syncMode?
237​
238```ts
239optional syncMode: ElectricSyncMode;
240```
241​
242Defined in: [packages/electric-db-collection/src/electric.ts:165](https://github.com/TanStack/db/blob/main/packages/electric-db-collection/src/electric.ts#L165)