Open History to see the last commit
1---
2id: IndexedDBCollectionConfig
3title: IndexedDBCollectionConfig
4---
5
6Defined in: [packages/db/src/indexed-db.ts:145](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db.ts#L145)
7
8Configuration options for creating an IndexedDB Collection
9
10## Extends
11
12- [`BaseCollectionConfig`](BaseCollectionConfig.md)\<`T`, `TKey`, `TSchema`\>
13
14## Type Parameters
15
16### T
17
18`T` *extends* `object` = `object`
19
20### TSchema
21
22`TSchema` *extends* `StandardSchemaV1` = `never`
23
24### TKey
25
26`TKey` *extends* `string` \| `number` = `string` \| `number`
27
28## Properties
29
30### autoIndex?
31
32```ts
33optional autoIndex: "eager" | "off";
34```
35
36Defined in: [packages/db/src/types.ts:773](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L773)
37
38Auto-indexing mode for the collection.
39When enabled, indexes will be automatically created for simple where expressions.
40
41#### Default
42
43```ts
44"off"
45```
46
47#### Description
48
49- "off": No automatic indexing (default). Use explicit indexes for better bundle size.
50- "eager": Automatically create indexes for simple where expressions in subscribeChanges.
51 Requires setting defaultIndexType.
52
53#### Inherited from
54
55[`BaseCollectionConfig`](BaseCollectionConfig.md).[`autoIndex`](BaseCollectionConfig.md#autoindex)
56
57***
58
59### compare()?
60
61```ts
62optional compare: (x, y) => number;
63```
64
65Defined in: [packages/db/src/types.ts:798](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L798)
66
67Optional function to compare two items.
68This is used to order the items in the collection.
69
70#### Parameters
71
72##### x
73
74`T`
75
76The first item to compare
77
78##### y
79
80`T`
81
82The second item to compare
83
84#### Returns
85
86`number`
87
88A number indicating the order of the items
89
90#### Example
91
92```ts
93// For a collection with a 'createdAt' field
94compare: (x, y) => x.createdAt.getTime() - y.createdAt.getTime()
95```
96
97#### Inherited from
98
99[`BaseCollectionConfig`](BaseCollectionConfig.md).[`compare`](BaseCollectionConfig.md#compare)
100
101***
102
103### db
104
105```ts
106db: IndexedDBInstance;
107```
108
109Defined in: [packages/db/src/indexed-db.ts:154](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db.ts#L154)
110
111IndexedDB instance from createIndexedDB()
112REQUIRED - must create database before collections
113
114***
115
116### defaultIndexType?
117
118```ts
119optional defaultIndexType: IndexConstructor<TKey>;
120```
121
122Defined in: [packages/db/src/types.ts:787](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L787)
123
124Default index type to use when creating indexes without an explicit type.
125Required for auto-indexing. Import from '@tanstack/db'.
126
127#### Example
128
129```ts
130import { BasicIndex } from '@tanstack/db'
131const collection = createCollection({
132 defaultIndexType: BasicIndex,
133 autoIndex: 'eager',
134 // ...
135})
136```
137
138#### Inherited from
139
140[`BaseCollectionConfig`](BaseCollectionConfig.md).[`defaultIndexType`](BaseCollectionConfig.md#defaultindextype)
141
142***
143
144### defaultStringCollation?
145
146```ts
147optional defaultStringCollation: StringCollationConfig;
148```
149
150Defined in: [packages/db/src/types.ts:993](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L993)
151
152Specifies how to compare data in the collection.
153This should be configured to match data ordering on the backend.
154E.g., when using the Electric DB collection these options
155 should match the database's collation settings.
156
157#### Inherited from
158
159[`BaseCollectionConfig`](BaseCollectionConfig.md).[`defaultStringCollation`](BaseCollectionConfig.md#defaultstringcollation)
160
161***
162
163### gcTime?
164
165```ts
166optional gcTime: number;
167```
168
169Defined in: [packages/db/src/types.ts:752](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L752)
170
171Time in milliseconds after which the collection will be garbage collected
172when it has no active subscribers. Defaults to 5 minutes (300000ms).
173Sync started without subscribers gets a minimum 50ms grace period.
174Pending preloads retain the collection until they settle. Preloading ready
175data refreshes the retention period. A non-positive or non-finite value
176disables automatic garbage collection.
177
178#### Inherited from
179
180[`BaseCollectionConfig`](BaseCollectionConfig.md).[`gcTime`](BaseCollectionConfig.md#gctime)
181
182***
183
184### getKey()
185
186```ts
187getKey: (item) => TKey;
188```
189
190Defined in: [packages/db/src/types.ts:743](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L743)
191
192Function to extract the ID from an object
193This is required for update/delete operations which now only accept IDs
194
195#### Parameters
196
197##### item
198
199`T`
200
201The item to extract the ID from
202
203#### Returns
204
205`TKey`
206
207The ID string for the item
208
209#### Example
210
211```ts
212// For a collection with a 'uuid' field as the primary key
213getKey: (item) => item.uuid
214```
215
216#### Inherited from
217
218[`BaseCollectionConfig`](BaseCollectionConfig.md).[`getKey`](BaseCollectionConfig.md#getkey)
219
220***
221
222### id?
223
224```ts
225optional id: string;
226```
227
228Defined in: [packages/db/src/types.ts:732](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L732)
229
230#### Inherited from
231
232[`BaseCollectionConfig`](BaseCollectionConfig.md).[`id`](BaseCollectionConfig.md#id)
233
234***
235
236### name
237
238```ts
239name: string;
240```
241
242Defined in: [packages/db/src/indexed-db.ts:160](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db.ts#L160)
243
244Name of the object store within the database
245Must exist in the underlying database
246
247***
248
249### onDelete?
250
251```ts
252optional onDelete:
253 | DeleteMutationFn<T, TKey, UtilsRecord, void>
254| DeleteMutationFn<T, TKey, UtilsRecord, any>;
255```
256
257Defined in: [packages/db/src/types.ts:982](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L982)
258
259Optional asynchronous handler function called before a delete operation
260Returning a value is deprecated; coordinate synchronization through collection utilities instead.
261
262#### Examples
263
264```ts
265// Basic delete handler
266onDelete: async ({ transaction, collection }) => {
267 const deletedKey = transaction.mutations[0].key
268 await api.deleteTodo(deletedKey)
269}
270```
271
272```ts
273// Delete handler with refetch (Query Collection)
274onDelete: async ({ transaction, collection }) => {
275 const keysToDelete = transaction.mutations.map(m => m.key)
276 await api.deleteTodos(keysToDelete)
277 // Trigger refetch to sync server state
278 await collection.utils.refetch()
279 // Prevent the pre-1.0 compatibility wrapper from refetching again.
280 return { refetch: false }
281}
282```
283
284```ts
285// Delete handler with sync wait (Electric Collection)
286onDelete: async ({ transaction, collection }) => {
287 const mutation = transaction.mutations[0]
288 const result = await api.deleteTodo(mutation.original.id)
289 // Wait for txid to sync
290 await collection.utils.awaitTxId(result.txid)
291}
292```
293
294```ts
295// Delete handler with confirmation
296onDelete: async ({ transaction, collection }) => {
297 const mutation = transaction.mutations[0]
298 const shouldDelete = await confirmDeletion(mutation.original)
299 if (!shouldDelete) {
300 throw new Error('Delete cancelled by user')
301 }
302 await api.deleteTodo(mutation.original.id)
303}
304```
305
306```ts
307// Delete handler with optimistic rollback
308onDelete: async ({ transaction, collection }) => {
309 const mutation = transaction.mutations[0]
310 try {
311 await api.deleteTodo(mutation.original.id)
312 } catch (error) {
313 // Transaction will automatically rollback optimistic changes
314 console.error('Delete failed, rolling back:', error)
315 throw error
316 }
317}
318```
319
320#### Inherited from
321
322[`BaseCollectionConfig`](BaseCollectionConfig.md).[`onDelete`](BaseCollectionConfig.md#ondelete)
323
324***
325
326### onInsert?
327
328```ts
329optional onInsert:
330 | InsertMutationFn<T, TKey, UtilsRecord, void>
331| InsertMutationFn<T, TKey, UtilsRecord, any>;
332```
333
334Defined in: [packages/db/src/types.ts:861](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L861)
335
336Optional asynchronous handler function called before an insert operation
337Returning a value is deprecated; coordinate synchronization through collection utilities instead.
338
339#### Examples
340
341```ts
342// Basic insert handler
343onInsert: async ({ transaction, collection }) => {
344 const newItem = transaction.mutations[0].modified
345 await api.createTodo(newItem)
346}
347```
348
349```ts
350// Insert handler with refetch (Query Collection)
351onInsert: async ({ transaction, collection }) => {
352 const newItem = transaction.mutations[0].modified
353 await api.createTodo(newItem)
354 // Trigger refetch to sync server state
355 await collection.utils.refetch()
356 // Prevent the pre-1.0 compatibility wrapper from refetching again.
357 return { refetch: false }
358}
359```
360
361```ts
362// Insert handler with sync wait (Electric Collection)
363onInsert: async ({ transaction, collection }) => {
364 const newItem = transaction.mutations[0].modified
365 const result = await api.createTodo(newItem)
366 // Wait for txid to sync
367 await collection.utils.awaitTxId(result.txid)
368}
369```
370
371```ts
372// Insert handler with multiple items
373onInsert: async ({ transaction, collection }) => {
374 const items = transaction.mutations.map(m => m.modified)
375 await api.createTodos(items)
376 // Refetch to get updated data from server
377 await collection.utils.refetch()
378 return { refetch: false }
379}
380```
381
382```ts
383// Insert handler with error handling
384onInsert: async ({ transaction, collection }) => {
385 try {
386 const newItem = transaction.mutations[0].modified
387 await api.createTodo(newItem)
388 } catch (error) {
389 console.error('Insert failed:', error)
390 throw error // This will cause the transaction to rollback
391 }
392}
393```
394
395#### Inherited from
396
397[`BaseCollectionConfig`](BaseCollectionConfig.md).[`onInsert`](BaseCollectionConfig.md#oninsert)
398
399***
400
401### onUpdate?
402
403```ts
404optional onUpdate:
405 | UpdateMutationFn<T, TKey, UtilsRecord, void>
406| UpdateMutationFn<T, TKey, UtilsRecord, any>;
407```
408
409Defined in: [packages/db/src/types.ts:923](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L923)
410
411Optional asynchronous handler function called before an update operation
412Returning a value is deprecated; coordinate synchronization through collection utilities instead.
413
414#### Examples
415
416```ts
417// Basic update handler
418onUpdate: async ({ transaction, collection }) => {
419 const updatedItem = transaction.mutations[0].modified
420 await api.updateTodo(updatedItem.id, updatedItem)
421}
422```
423
424```ts
425// Update handler with refetch (Query Collection)
426onUpdate: async ({ transaction, collection }) => {
427 const mutation = transaction.mutations[0]
428 const changes = mutation.changes // Only the changed fields
429 await api.updateTodo(mutation.original.id, changes)
430 // Trigger refetch to sync server state
431 await collection.utils.refetch()
432 // Prevent the pre-1.0 compatibility wrapper from refetching again.
433 return { refetch: false }
434}
435```
436
437```ts
438// Update handler with sync wait (Electric Collection)
439onUpdate: async ({ transaction, collection }) => {
440 const mutation = transaction.mutations[0]
441 const result = await api.updateTodo(mutation.original.id, mutation.changes)
442 // Wait for txid to sync
443 await collection.utils.awaitTxId(result.txid)
444}
445```
446
447```ts
448// Update handler with multiple items
449onUpdate: async ({ transaction, collection }) => {
450 const updates = transaction.mutations.map(m => ({
451 id: m.key,
452 changes: m.changes
453 }))
454 await api.updateTodos(updates)
455 await collection.utils.refetch()
456 return { refetch: false }
457}
458```
459
460```ts
461// Update handler with optimistic rollback
462onUpdate: async ({ transaction, collection }) => {
463 const mutation = transaction.mutations[0]
464 try {
465 await api.updateTodo(mutation.original.id, mutation.changes)
466 } catch (error) {
467 // Transaction will automatically rollback optimistic changes
468 console.error('Update failed, rolling back:', error)
469 throw error
470 }
471}
472```
473
474#### Inherited from
475
476[`BaseCollectionConfig`](BaseCollectionConfig.md).[`onUpdate`](BaseCollectionConfig.md#onupdate)
477
478***
479
480### schema?
481
482```ts
483optional schema: TSchema;
484```
485
486Defined in: [packages/db/src/types.ts:733](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L733)
487
488#### Inherited from
489
490[`BaseCollectionConfig`](BaseCollectionConfig.md).[`schema`](BaseCollectionConfig.md#schema)
491
492***
493
494### startSync?
495
496```ts
497optional startSync: boolean;
498```
499
500Defined in: [packages/db/src/types.ts:763](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L763)
501
502Whether to eagerly start syncing on collection creation.
503When true, syncing begins immediately. When false, syncing starts when the first subscriber attaches.
504
505Note: Even with startSync=true, collections will pause syncing when there are no active
506subscribers (typically when components querying the collection unmount), resuming when new
507subscribers attach. This preserves normal staleTime/gcTime behavior.
508
509#### Default
510
511```ts
512false
513```
514
515#### Inherited from
516
517[`BaseCollectionConfig`](BaseCollectionConfig.md).[`startSync`](BaseCollectionConfig.md#startsync)
518
519***
520
521### syncMode?
522
523```ts
524optional syncMode: SyncMode;
525```
526
527Defined in: [packages/db/src/types.ts:807](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L807)
528
529The mode of sync to use for the collection.
530
531#### Default
532
533`eager`
534
535#### Description
536
537- `eager`: syncs all data immediately on preload
538- `on-demand`: syncs data in incremental snapshots when the collection is queried
539The exact implementation of the sync mode is up to the sync implementation.
540
541#### Inherited from
542
543[`BaseCollectionConfig`](BaseCollectionConfig.md).[`syncMode`](BaseCollectionConfig.md#syncmode)
544
545***
546
547### utils?
548
549```ts
550optional utils: UtilsRecord;
551```
552
553Defined in: [packages/db/src/types.ts:995](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L995)
554
555#### Inherited from
556
557[`BaseCollectionConfig`](BaseCollectionConfig.md).[`utils`](BaseCollectionConfig.md#utils)