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

localstoragecollectionoptions.md

195 lines3.6 KB

1---
2id: localStorageCollectionOptions
3title: localStorageCollectionOptions
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8# Function: localStorageCollectionOptions()
9​
10```ts
11function localStorageCollectionOptions<TExplicit, TSchema, TFallback>(config): object
12```
13​
14Defined in: [packages/db/src/local-storage.ts:205](https://github.com/TanStack/db/blob/main/packages/db/src/local-storage.ts#L205)
15​
16Creates localStorage collection options for use with a standard Collection
17​
18This function creates a collection that persists data to localStorage/sessionStorage
19and synchronizes changes across browser tabs using storage events.
20​
21## Type Parameters
22​
23• **TExplicit** = `unknown`
24​
25The explicit type of items in the collection (highest priority)
26​
27• **TSchema** *extends* `StandardSchemaV1`\<`unknown`, `unknown`\> = `never`
28​
29The schema type for validation and type inference (second priority)
30​
31• **TFallback** *extends* `object` = `Record`\<`string`, `unknown`\>
32​
33The fallback type if no explicit or schema type is provided
34​
35## Parameters
36​
37### config
38​
39[`LocalStorageCollectionConfig`](../../interfaces/localstoragecollectionconfig.md)\<`TExplicit`, `TSchema`, `TFallback`\>
40​
41Configuration options for the localStorage collection
42​
43## Returns
44​
45`object`
46​
47Collection options with utilities including clearStorage and getStorageSize
48​
49### getKey()
50​
51```ts
52getKey: (item) => string | number;
53```
54​
55#### Parameters
56​
57##### item
58​
59[`ResolveType`](../../type-aliases/resolvetype.md)
60​
61#### Returns
62​
63`string` \| `number`
64​
65### id
66​
67```ts
68id: string = collectionId;
69```
70​
71### onDelete()
72​
73```ts
74onDelete: (params) => Promise<any> = wrappedOnDelete;
75```
76​
77#### Parameters
78​
79##### params
80​
81[`DeleteMutationFnParams`](../../type-aliases/deletemutationfnparams.md)\<[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>\>
82​
83#### Returns
84​
85`Promise`\<`any`\>
86​
87### onInsert()
88​
89```ts
90onInsert: (params) => Promise<any> = wrappedOnInsert;
91```
92​
93#### Parameters
94​
95##### params
96​
97[`InsertMutationFnParams`](../../type-aliases/insertmutationfnparams.md)\<[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>\>
98​
99#### Returns
100​
101`Promise`\<`any`\>
102​
103### onUpdate()
104​
105```ts
106onUpdate: (params) => Promise<any> = wrappedOnUpdate;
107```
108​
109#### Parameters
110​
111##### params
112​
113[`UpdateMutationFnParams`](../../type-aliases/updatemutationfnparams.md)\<[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>\>
114​
115#### Returns
116​
117`Promise`\<`any`\>
118​
119### schema?
120​
121```ts
122optional schema: TSchema;
123```
124​
125### sync
126​
127```ts
128sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number> & object;
129```
130​
131#### Type declaration
132​
133##### manualTrigger()?
134​
135```ts
136optional manualTrigger: () => void;
137```
138​
139###### Returns
140​
141`void`
142​
143### utils
144​
145```ts
146utils: object;
147```
148​
149#### utils.clearStorage
150​
151```ts
152clearStorage: ClearStorageFn;
153```
154​
155#### utils.getStorageSize
156​
157```ts
158getStorageSize: GetStorageSizeFn;
159```
160​
161## Examples
162​
163```ts
164// Basic localStorage collection
165const collection = createCollection(
166 localStorageCollectionOptions({
167 storageKey: 'todos',
168 getKey: (item) => item.id,
169 })
171```
172​
173```ts
174// localStorage collection with custom storage
175const collection = createCollection(
176 localStorageCollectionOptions({
177 storageKey: 'todos',
178 storage: window.sessionStorage, // Use sessionStorage instead
179 getKey: (item) => item.id,
180 })
182```
183​
184```ts
185// localStorage collection with mutation handlers
186const collection = createCollection(
187 localStorageCollectionOptions({
188 storageKey: 'todos',
189 getKey: (item) => item.id,
190 onInsert: async ({ transaction }) => {
191 console.log('Item inserted:', transaction.mutations[0].modified)
192 },
193 })
195```