Open History to see the last commit
1---
2id: LocalOnlyCollectionConfig
3title: LocalOnlyCollectionConfig
4---
5
6# Interface: LocalOnlyCollectionConfig\<T, TSchema, TKey\>
7
8Defined in: [packages/db/src/local-only.ts:22](https://github.com/TanStack/db/blob/main/packages/db/src/local-only.ts#L22)
9
10Configuration interface for Local-only collection options
11
12## Extends
13
14- `Omit`\<[`BaseCollectionConfig`](BaseCollectionConfig.md)\<`T`, `TKey`, `TSchema`, [`LocalOnlyCollectionUtils`](LocalOnlyCollectionUtils.md)\>, `"gcTime"` \| `"startSync"`\>
15
16## Type Parameters
17
18### T
19
20`T` *extends* `object` = `object`
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### TKey
31
32`TKey` *extends* `string` \| `number` = `string` \| `number`
33
34The type of the key returned by `getKey`
35
36## Properties
37
38### autoIndex?
39
40```ts
41optional autoIndex: "off" | "eager";
42```
43
44Defined in: [packages/db/src/types.ts:548](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L548)
45
46Auto-indexing mode for the collection.
47When enabled, indexes will be automatically created for simple where expressions.
48
49#### Default
50
51```ts
52"eager"
53```
54
55#### Description
56
57- "off": No automatic indexing
58- "eager": Automatically create indexes for simple where expressions in subscribeChanges (default)
59
60#### Inherited from
61
62[`BaseCollectionConfig`](BaseCollectionConfig.md).[`autoIndex`](BaseCollectionConfig.md#autoindex)
63
64***
65
66### compare()?
67
68```ts
69optional compare: (x, y) => number;
70```
71
72Defined in: [packages/db/src/types.ts:559](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L559)
73
74Optional function to compare two items.
75This is used to order the items in the collection.
76
77#### Parameters
78
79##### x
80
81`T`
82
83The first item to compare
84
85##### y
86
87`T`
88
89The second item to compare
90
91#### Returns
92
93`number`
94
95A number indicating the order of the items
96
97#### Example
98
99```ts
100// For a collection with a 'createdAt' field
101compare: (x, y) => x.createdAt.getTime() - y.createdAt.getTime()
102```
103
104#### Inherited from
105
106```ts
107Omit.compare
108```
109
110***
111
112### defaultStringCollation?
113
114```ts
115optional defaultStringCollation: StringCollationConfig;
116```
117
118Defined in: [packages/db/src/types.ts:705](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L705)
119
120Specifies how to compare data in the collection.
121This should be configured to match data ordering on the backend.
122E.g., when using the Electric DB collection these options
123 should match the database's collation settings.
124
125#### Inherited from
126
127```ts
128Omit.defaultStringCollation
129```
130
131***
132
133### getKey()
134
135```ts
136getKey: (item) => TKey;
137```
138
139Defined in: [packages/db/src/types.ts:523](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L523)
140
141Function to extract the ID from an object
142This is required for update/delete operations which now only accept IDs
143
144#### Parameters
145
146##### item
147
148`T`
149
150The item to extract the ID from
151
152#### Returns
153
154`TKey`
155
156The ID string for the item
157
158#### Example
159
160```ts
161// For a collection with a 'uuid' field as the primary key
162getKey: (item) => item.uuid
163```
164
165#### Inherited from
166
167```ts
168Omit.getKey
169```
170
171***
172
173### id?
174
175```ts
176optional id: string;
177```
178
179Defined in: [packages/db/src/types.ts:512](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L512)
180
181#### Inherited from
182
183[`BaseCollectionConfig`](BaseCollectionConfig.md).[`id`](BaseCollectionConfig.md#id)
184
185***
186
187### initialData?
188
189```ts
190optional initialData: T[];
191```
192
193Defined in: [packages/db/src/local-only.ts:34](https://github.com/TanStack/db/blob/main/packages/db/src/local-only.ts#L34)
194
195Optional initial data to populate the collection with on creation
196This data will be applied during the initial sync process
197
198***
199
200### onDelete?
201
202```ts
203optional onDelete: DeleteMutationFn<T, TKey, LocalOnlyCollectionUtils, any>;
204```
205
206Defined in: [packages/db/src/types.ts:697](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L697)
207
208Optional asynchronous handler function called before a delete operation
209
210#### Param
211
212Object containing transaction and collection information
213
214#### Returns
215
216Promise resolving to any value
217
218#### Examples
219
220```ts
221// Basic delete handler
222onDelete: async ({ transaction, collection }) => {
223 const deletedKey = transaction.mutations[0].key
224 await api.deleteTodo(deletedKey)
225}
226```
227
228```ts
229// Delete handler with multiple items
230onDelete: async ({ transaction, collection }) => {
231 const keysToDelete = transaction.mutations.map(m => m.key)
232 await api.deleteTodos(keysToDelete)
233}
234```
235
236```ts
237// Delete handler with confirmation
238onDelete: async ({ transaction, collection }) => {
239 const mutation = transaction.mutations[0]
240 const shouldDelete = await confirmDeletion(mutation.original)
241 if (!shouldDelete) {
242 throw new Error('Delete cancelled by user')
243 }
244 await api.deleteTodo(mutation.original.id)
245}
246```
247
248```ts
249// Delete handler with optimistic rollback
250onDelete: async ({ transaction, collection }) => {
251 const mutation = transaction.mutations[0]
252 try {
253 await api.deleteTodo(mutation.original.id)
254 } catch (error) {
255 // Transaction will automatically rollback optimistic changes
256 console.error('Delete failed, rolling back:', error)
257 throw error
258 }
259}
260```
261
262#### Inherited from
263
264```ts
265Omit.onDelete
266```
267
268***
269
270### onInsert?
271
272```ts
273optional onInsert: InsertMutationFn<T, TKey, LocalOnlyCollectionUtils, any>;
274```
275
276Defined in: [packages/db/src/types.ts:610](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L610)
277
278Optional asynchronous handler function called before an insert operation
279
280#### Param
281
282Object containing transaction and collection information
283
284#### Returns
285
286Promise resolving to any value
287
288#### Examples
289
290```ts
291// Basic insert handler
292onInsert: async ({ transaction, collection }) => {
293 const newItem = transaction.mutations[0].modified
294 await api.createTodo(newItem)
295}
296```
297
298```ts
299// Insert handler with multiple items
300onInsert: async ({ transaction, collection }) => {
301 const items = transaction.mutations.map(m => m.modified)
302 await api.createTodos(items)
303}
304```
305
306```ts
307// Insert handler with error handling
308onInsert: async ({ transaction, collection }) => {
309 try {
310 const newItem = transaction.mutations[0].modified
311 const result = await api.createTodo(newItem)
312 return result
313 } catch (error) {
314 console.error('Insert failed:', error)
315 throw error // This will cause the transaction to fail
316 }
317}
318```
319
320```ts
321// Insert handler with metadata
322onInsert: async ({ transaction, collection }) => {
323 const mutation = transaction.mutations[0]
324 await api.createTodo(mutation.modified, {
325 source: mutation.metadata?.source,
326 timestamp: mutation.createdAt
327 })
328}
329```
330
331#### Inherited from
332
333```ts
334Omit.onInsert
335```
336
337***
338
339### onUpdate?
340
341```ts
342optional onUpdate: UpdateMutationFn<T, TKey, LocalOnlyCollectionUtils, any>;
343```
344
345Defined in: [packages/db/src/types.ts:654](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L654)
346
347Optional asynchronous handler function called before an update operation
348
349#### Param
350
351Object containing transaction and collection information
352
353#### Returns
354
355Promise resolving to any value
356
357#### Examples
358
359```ts
360// Basic update handler
361onUpdate: async ({ transaction, collection }) => {
362 const updatedItem = transaction.mutations[0].modified
363 await api.updateTodo(updatedItem.id, updatedItem)
364}
365```
366
367```ts
368// Update handler with partial updates
369onUpdate: async ({ transaction, collection }) => {
370 const mutation = transaction.mutations[0]
371 const changes = mutation.changes // Only the changed fields
372 await api.updateTodo(mutation.original.id, changes)
373}
374```
375
376```ts
377// Update handler with multiple items
378onUpdate: async ({ transaction, collection }) => {
379 const updates = transaction.mutations.map(m => ({
380 id: m.key,
381 changes: m.changes
382 }))
383 await api.updateTodos(updates)
384}
385```
386
387```ts
388// Update handler with optimistic rollback
389onUpdate: async ({ transaction, collection }) => {
390 const mutation = transaction.mutations[0]
391 try {
392 await api.updateTodo(mutation.original.id, mutation.changes)
393 } catch (error) {
394 // Transaction will automatically rollback optimistic changes
395 console.error('Update failed, rolling back:', error)
396 throw error
397 }
398}
399```
400
401#### Inherited from
402
403```ts
404Omit.onUpdate
405```
406
407***
408
409### schema?
410
411```ts
412optional schema: TSchema;
413```
414
415Defined in: [packages/db/src/types.ts:513](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L513)
416
417#### Inherited from
418
419```ts
420Omit.schema
421```
422
423***
424
425### syncMode?
426
427```ts
428optional syncMode: SyncMode;
429```
430
431Defined in: [packages/db/src/types.ts:568](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L568)
432
433The mode of sync to use for the collection.
434
435#### Default
436
437`eager`
438
439#### Description
440
441- `eager`: syncs all data immediately on preload
442- `on-demand`: syncs data in incremental snapshots when the collection is queried
443The exact implementation of the sync mode is up to the sync implementation.
444
445#### Inherited from
446
447[`BaseCollectionConfig`](BaseCollectionConfig.md).[`syncMode`](BaseCollectionConfig.md#syncmode)
448
449***
450
451### utils?
452
453```ts
454optional utils: LocalOnlyCollectionUtils;
455```
456
457Defined in: [packages/db/src/types.ts:707](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L707)
458
459#### Inherited from
460
461```ts
462Omit.utils
463```