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

indexedDBCollectionOptions.md

138 lines3.5 KB

1---
2id: indexedDBCollectionOptions
3title: indexedDBCollectionOptions
4---
5​
6## Call Signature
7​
8```ts
9function indexedDBCollectionOptions<T, TKey>(config): CollectionConfig<InferSchemaOutput<T>, TKey, T, IndexedDBCollectionUtils<InferSchemaOutput<T>, InferSchemaInput<T>>> & object;
10```
11​
12Defined in: [packages/db/src/indexed-db.ts:438](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db.ts#L438)
13​
14Creates IndexedDB collection options for use with a standard Collection.
15This provides persistent local storage with cross-tab synchronization.
16​
17IMPORTANT: You must first create the database with createIndexedDB() and
18pass the instance to this function. This ensures all stores are created
19upfront in a single upgrade transaction.
20​
21### Type Parameters
22​
23#### T
24​
25`T` *extends* `StandardSchemaV1`\<`unknown`, `unknown`\>
26​
27#### TKey
28​
29`TKey` *extends* `string` \| `number` = `string` \| `number`
30​
31### Parameters
32​
33#### config
34​
35[`IndexedDBCollectionConfig`](../interfaces/IndexedDBCollectionConfig.md)\<`InferSchemaOutput`\<`T`\>, `T`, `TKey`\> & `object`
36​
37### Returns
38​
39[`CollectionConfig`](../interfaces/CollectionConfig.md)\<`InferSchemaOutput`\<`T`\>, `TKey`, `T`, [`IndexedDBCollectionUtils`](../interfaces/IndexedDBCollectionUtils.md)\<`InferSchemaOutput`\<`T`\>, `InferSchemaInput`\<`T`\>\>\> & `object`
40​
41### Examples
42​
43```ts
44// Step 1: Create database with all stores
45const db = await createIndexedDB({
46 name: 'myApp',
47 version: 1,
48 stores: ['todos', 'users'],
49})
50​
51// Step 2: Create collections using the shared database
52const todosCollection = createCollection(
53 indexedDBCollectionOptions({
54 db,
55 name: 'todos',
56 schema: todoSchema,
57 getKey: (item: { id: string }) => item.id,
58 })
60```
61​
62```ts
63// Without schema (explicit type)
64const todosCollection = createCollection(
65 indexedDBCollectionOptions<Todo>({
66 db,
67 name: 'todos',
68 getKey: (item: { id: string }) => item.id,
69 })
71```
72​
73## Call Signature
74​
75```ts
76function indexedDBCollectionOptions<T, TKey>(config): CollectionConfig<T, TKey, never, IndexedDBCollectionUtils<T, T>> & object;
77```
78​
79Defined in: [packages/db/src/indexed-db.ts:456](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db.ts#L456)
80​
81Creates IndexedDB collection options for use with a standard Collection.
82This provides persistent local storage with cross-tab synchronization.
83​
84IMPORTANT: You must first create the database with createIndexedDB() and
85pass the instance to this function. This ensures all stores are created
86upfront in a single upgrade transaction.
87​
88### Type Parameters
89​
90#### T
91​
92`T` *extends* `object`
93​
94#### TKey
95​
96`TKey` *extends* `string` \| `number` = `string` \| `number`
97​
98### Parameters
99​
100#### config
101​
102[`IndexedDBCollectionConfig`](../interfaces/IndexedDBCollectionConfig.md)\<`T`, `never`, `TKey`\> & `object`
103​
104### Returns
105​
106[`CollectionConfig`](../interfaces/CollectionConfig.md)\<`T`, `TKey`, `never`, [`IndexedDBCollectionUtils`](../interfaces/IndexedDBCollectionUtils.md)\<`T`, `T`\>\> & `object`
107​
108### Examples
109​
110```ts
111// Step 1: Create database with all stores
112const db = await createIndexedDB({
113 name: 'myApp',
114 version: 1,
115 stores: ['todos', 'users'],
116})
117​
118// Step 2: Create collections using the shared database
119const todosCollection = createCollection(
120 indexedDBCollectionOptions({
121 db,
122 name: 'todos',
123 schema: todoSchema,
124 getKey: (item: { id: string }) => item.id,
125 })
127```
128​
129```ts
130// Without schema (explicit type)
131const todosCollection = createCollection(
132 indexedDBCollectionOptions<Todo>({
133 db,
134 name: 'todos',
135 getKey: (item: { id: string }) => item.id,
136 })
138```