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

LocalStorageCollectionUtils.md

80 lines1.8 KB

1---
2id: LocalStorageCollectionUtils
3title: LocalStorageCollectionUtils
4---
5​
6Defined in: [packages/db/src/local-storage.ts:102](https://github.com/TanStack/db/blob/main/packages/db/src/local-storage.ts#L102)
7​
8LocalStorage collection utilities type
9​
10## Extends
11​
12- [`UtilsRecord`](../type-aliases/UtilsRecord.md)
13​
14## Indexable
15​
16```ts
17[key: string]: any
18```
19​
20## Properties
21​
22### acceptMutations()
23​
24```ts
25acceptMutations: (transaction) => void;
26```
27​
28Defined in: [packages/db/src/local-storage.ts:122](https://github.com/TanStack/db/blob/main/packages/db/src/local-storage.ts#L122)
29​
30Accepts mutations from a transaction that belong to this collection and persists them to localStorage.
31This should be called in your transaction's mutationFn to persist local-storage data.
32​
33#### Parameters
34​
35##### transaction
36​
37The transaction containing mutations to accept
38​
39###### mutations
40​
41[`PendingMutation`](PendingMutation.md)\<`Record`\<`string`, `unknown`\>, [`OperationType`](../type-aliases/OperationType.md), [`Collection`](Collection.md)\<`Record`\<`string`, `unknown`\>, `any`, `any`, `any`, `any`\>\>[]
42​
43#### Returns
44​
45`void`
46​
47#### Example
48​
49```ts
50const localSettings = createCollection(localStorageCollectionOptions({...}))
51​
52const tx = createTransaction({
53 mutationFn: async ({ transaction }) => {
54 // Make API call first
55 await api.save(...)
56 // Then persist local-storage mutations after success
57 localSettings.utils.acceptMutations(transaction)
58 }
59})
60```
61​
62***
63​
64### clearStorage
65​
66```ts
67clearStorage: ClearStorageFn;
68```
69​
70Defined in: [packages/db/src/local-storage.ts:103](https://github.com/TanStack/db/blob/main/packages/db/src/local-storage.ts#L103)
71​
72***
73​
74### getStorageSize
75​
76```ts
77getStorageSize: GetStorageSizeFn;
78```
79​
80Defined in: [packages/db/src/local-storage.ts:104](https://github.com/TanStack/db/blob/main/packages/db/src/local-storage.ts#L104)