Open History to see the last commit
1---
2id: IndexedDBCollectionConfig
3title: IndexedDBCollectionConfig
4---
5
6Defined in: [packages/db/src/indexed-db.ts:149](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db.ts#L149)
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:937](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L937)
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:962](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L962)
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:158](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db.ts#L158)
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:951](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L951)
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:1137](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L1137)
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:916](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L916)
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 handoff grace period
174until a subscriber attaches. After the last subscriber leaves, gcTime
175applies without that startup minimum.
176Pending preloads retain the collection until they settle. Preloading ready
177data refreshes the retention period. A persisting transaction that touches
178the collection also retains it, and the period starts again when the last
179one completes or fails, preserving any remaining startup grace. Pending
180manual transactions do not retain it; keep a subscription for draft
181visibility until commit or rollback. Rolling
182back a manual transaction that stayed pending preserves the existing GC
183deadline. Cleanup runs at a host idle opportunity after the delay.
184A non-positive or non-finite value
185disables automatic garbage collection.
186
187#### Inherited from
188
189[`BaseCollectionConfig`](BaseCollectionConfig.md).[`gcTime`](BaseCollectionConfig.md#gctime)
190
191***
192
193### getKey()
194
195```ts
196getKey: (item) => TKey;
197```
198
199Defined in: [packages/db/src/types.ts:898](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L898)
200
201Function to extract the ID from an object
202This is required for update/delete operations which now only accept IDs
203
204#### Parameters
205
206##### item
207
208`T`
209
210The item to extract the ID from
211
212#### Returns
213
214`TKey`
215
216The ID string for the item
217
218#### Example
219
220```ts
221// For a collection with a 'uuid' field as the primary key
222getKey: (item) => item.uuid
223```
224
225#### Inherited from
226
227[`BaseCollectionConfig`](BaseCollectionConfig.md).[`getKey`](BaseCollectionConfig.md#getkey)
228
229***
230
231### id?
232
233```ts
234optional id: string;
235```
236
237Defined in: [packages/db/src/types.ts:887](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L887)
238
239#### Inherited from
240
241[`BaseCollectionConfig`](BaseCollectionConfig.md).[`id`](BaseCollectionConfig.md#id)
242
243***
244
245### name
246
247```ts
248name: string;
249```
250
251Defined in: [packages/db/src/indexed-db.ts:164](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db.ts#L164)
252
253Name of the object store within the database
254Must exist in the underlying database
255
256***
257
258### onDelete?
259
260```ts
261optional onDelete: DeleteMutationFn<T, TKey, UtilsRecord>;
262```
263
264Defined in: [packages/db/src/types.ts:1129](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L1129)
265
266Optional asynchronous handler function called before a delete operation
267
268#### Examples
269
270```ts
271// Basic delete handler
272onDelete: async ({ transaction, collection }) => {
273 const deletedKey = transaction.mutations[0].key
274 await api.deleteTodo(deletedKey)
275}
276```
277
278```ts
279// Delete handler with refetch (Query Collection)
280onDelete: async ({ transaction, collection }) => {
281 const keysToDelete = transaction.mutations.map(m => m.key)
282 await api.deleteTodos(keysToDelete)
283 // Trigger refetch to sync server state
284 await collection.utils.refetch()
285}
286```
287
288```ts
289// Delete handler with sync wait (Electric Collection)
290onDelete: async ({ transaction, collection }) => {
291 const mutation = transaction.mutations[0]
292 const result = await api.deleteTodo(mutation.original.id)
293 // Wait for txid to sync
294 await collection.utils.awaitTxId(result.txid)
295}
296```
297
298```ts
299// Delete handler with confirmation
300onDelete: async ({ transaction, collection }) => {
301 const mutation = transaction.mutations[0]
302 const shouldDelete = await confirmDeletion(mutation.original)
303 if (!shouldDelete) {
304 throw new Error('Delete cancelled by user')
305 }
306 await api.deleteTodo(mutation.original.id)
307}
308```
309
310```ts
311// Delete handler with optimistic rollback
312onDelete: async ({ transaction, collection }) => {
313 const mutation = transaction.mutations[0]
314 try {
315 await api.deleteTodo(mutation.original.id)
316 } catch (error) {
317 // Transaction will automatically rollback optimistic changes
318 console.error('Delete failed, rolling back:', error)
319 throw error
320 }
321}
322```
323
324#### Inherited from
325
326[`BaseCollectionConfig`](BaseCollectionConfig.md).[`onDelete`](BaseCollectionConfig.md#ondelete)
327
328***
329
330### onInsert?
331
332```ts
333optional onInsert: InsertMutationFn<T, TKey, UtilsRecord>;
334```
335
336Defined in: [packages/db/src/types.ts:1021](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L1021)
337
338Optional asynchronous handler function called before an insert operation
339
340#### Examples
341
342```ts
343// Basic insert handler
344onInsert: async ({ transaction, collection }) => {
345 const newItem = transaction.mutations[0].modified
346 await api.createTodo(newItem)
347}
348```
349
350```ts
351// Insert handler with refetch (Query Collection)
352onInsert: async ({ transaction, collection }) => {
353 const newItem = transaction.mutations[0].modified
354 await api.createTodo(newItem)
355 // Trigger refetch to sync server state
356 await collection.utils.refetch()
357}
358```
359
360```ts
361// Insert handler with sync wait (Electric Collection)
362onInsert: async ({ transaction, collection }) => {
363 const newItem = transaction.mutations[0].modified
364 const result = await api.createTodo(newItem)
365 // Wait for txid to sync
366 await collection.utils.awaitTxId(result.txid)
367}
368```
369
370```ts
371// Insert handler with multiple items
372onInsert: async ({ transaction, collection }) => {
373 const items = transaction.mutations.map(m => m.modified)
374 await api.createTodos(items)
375 // Refetch to get updated data from server
376 await collection.utils.refetch()
377}
378```
379
380```ts
381// Insert handler with error handling
382onInsert: async ({ transaction, collection }) => {
383 try {
384 const newItem = transaction.mutations[0].modified
385 await api.createTodo(newItem)
386 } catch (error) {
387 console.error('Insert failed:', error)
388 throw error // This will cause the transaction to rollback
389 }
390}
391```
392
393#### Inherited from
394
395[`BaseCollectionConfig`](BaseCollectionConfig.md).[`onInsert`](BaseCollectionConfig.md#oninsert)
396
397***
398
399### onUpdate?
400
401```ts
402optional onUpdate: UpdateMutationFn<T, TKey, UtilsRecord>;
403```
404
405Defined in: [packages/db/src/types.ts:1076](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L1076)
406
407Optional asynchronous handler function called before an update operation
408
409#### Examples
410
411```ts
412// Basic update handler
413onUpdate: async ({ transaction, collection }) => {
414 const updatedItem = transaction.mutations[0].modified
415 await api.updateTodo(updatedItem.id, updatedItem)
416}
417```
418
419```ts
420// Update handler with refetch (Query Collection)
421onUpdate: async ({ transaction, collection }) => {
422 const mutation = transaction.mutations[0]
423 const changes = mutation.changes // Only the changed fields
424 await api.updateTodo(mutation.original.id, changes)
425 // Trigger refetch to sync server state
426 await collection.utils.refetch()
427}
428```
429
430```ts
431// Update handler with sync wait (Electric Collection)
432onUpdate: async ({ transaction, collection }) => {
433 const mutation = transaction.mutations[0]
434 const result = await api.updateTodo(mutation.original.id, mutation.changes)
435 // Wait for txid to sync
436 await collection.utils.awaitTxId(result.txid)
437}
438```
439
440```ts
441// Update handler with multiple items
442onUpdate: async ({ transaction, collection }) => {
443 const updates = transaction.mutations.map(m => ({
444 id: m.key,
445 changes: m.changes
446 }))
447 await api.updateTodos(updates)
448 await collection.utils.refetch()
449}
450```
451
452```ts
453// Update handler with optimistic rollback
454onUpdate: async ({ transaction, collection }) => {
455 const mutation = transaction.mutations[0]
456 try {
457 await api.updateTodo(mutation.original.id, mutation.changes)
458 } catch (error) {
459 // Transaction will automatically rollback optimistic changes
460 console.error('Update failed, rolling back:', error)
461 throw error
462 }
463}
464```
465
466#### Inherited from
467
468[`BaseCollectionConfig`](BaseCollectionConfig.md).[`onUpdate`](BaseCollectionConfig.md#onupdate)
469
470***
471
472### schema?
473
474```ts
475optional schema: TSchema;
476```
477
478Defined in: [packages/db/src/types.ts:888](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L888)
479
480#### Inherited from
481
482[`BaseCollectionConfig`](BaseCollectionConfig.md).[`schema`](BaseCollectionConfig.md#schema)
483
484***
485
486### startSync?
487
488```ts
489optional startSync: boolean;
490```
491
492Defined in: [packages/db/src/types.ts:927](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L927)
493
494Whether to eagerly start syncing on collection creation.
495When true, syncing begins immediately. When false, syncing starts when the first subscriber attaches.
496
497Note: Even with startSync=true, collections will pause syncing when there are no active
498subscribers (typically when components querying the collection unmount), resuming when new
499subscribers attach. This preserves normal staleTime/gcTime behavior.
500
501#### Default
502
503```ts
504false
505```
506
507#### Inherited from
508
509[`BaseCollectionConfig`](BaseCollectionConfig.md).[`startSync`](BaseCollectionConfig.md#startsync)
510
511***
512
513### syncMode?
514
515```ts
516optional syncMode: SyncMode;
517```
518
519Defined in: [packages/db/src/types.ts:971](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L971)
520
521The mode of sync to use for the collection.
522
523#### Default
524
525`eager`
526
527#### Description
528
529- `eager`: syncs all data immediately on preload
530- `on-demand`: syncs data in incremental snapshots when the collection is queried
531The exact implementation of the sync mode is up to the sync implementation.
532
533#### Inherited from
534
535[`BaseCollectionConfig`](BaseCollectionConfig.md).[`syncMode`](BaseCollectionConfig.md#syncmode)
536
537***
538
539### utils?
540
541```ts
542optional utils: UtilsRecord;
543```
544
545Defined in: [packages/db/src/types.ts:1139](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L1139)
546
547#### Inherited from
548
549[`BaseCollectionConfig`](BaseCollectionConfig.md).[`utils`](BaseCollectionConfig.md#utils)