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

localonlycollectionoptions.md

198 lines4.3 KB

1---
2id: localOnlyCollectionOptions
3title: localOnlyCollectionOptions
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8# Function: localOnlyCollectionOptions()
9​
10```ts
11function localOnlyCollectionOptions<TExplicit, TSchema, TFallback, TKey>(config): object
12```
13​
14Defined in: [packages/db/src/local-only.ts:137](https://github.com/TanStack/db/blob/main/packages/db/src/local-only.ts#L137)
15​
16Creates Local-only collection options for use with a standard Collection
17​
18This is an in-memory collection that doesn't sync with external sources but uses a loopback sync config
19that immediately "syncs" all optimistic changes to the collection, making them permanent.
20Perfect for local-only data that doesn't need persistence or external synchronization.
21​
22## Type Parameters
23​
24• **TExplicit** = `unknown`
25​
26The explicit type of items in the collection (highest priority)
27​
28• **TSchema** *extends* `StandardSchemaV1`\<`unknown`, `unknown`\> = `never`
29​
30The schema type for validation and type inference (second priority)
31​
32• **TFallback** *extends* `Record`\<`string`, `unknown`\> = `Record`\<`string`, `unknown`\>
33​
34The fallback type if no explicit or schema type is provided
35​
36• **TKey** *extends* `string` \| `number` = `string` \| `number`
37​
38The type of the key returned by getKey
39​
40## Parameters
41​
42### config
43​
44[`LocalOnlyCollectionConfig`](../../interfaces/localonlycollectionconfig.md)\<`TExplicit`, `TSchema`, `TFallback`, `TKey`\>
45​
46Configuration options for the Local-only collection
47​
48## Returns
49​
50`object`
51​
52Collection options with utilities (currently empty but follows the pattern)
53​
54### gcTime
55​
56```ts
57gcTime: number = 0;
58```
59​
60### getKey()
61​
62```ts
63getKey: (item) => TKey;
64```
65​
66#### Parameters
67​
68##### item
69​
70[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>
71​
72#### Returns
73​
74`TKey`
75​
76### id?
77​
78```ts
79optional id: string;
80```
81​
82Standard Collection configuration properties
83​
84### onDelete()
85​
86```ts
87onDelete: (params) => Promise<any> = wrappedOnDelete;
88```
89​
90Wrapper for onDelete handler that also confirms the transaction immediately
91​
92#### Parameters
93​
94##### params
95​
96[`DeleteMutationFnParams`](../../type-aliases/deletemutationfnparams.md)\<[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>, `TKey`, [`LocalOnlyCollectionUtils`](../../interfaces/localonlycollectionutils.md)\>
97​
98#### Returns
99​
100`Promise`\<`any`\>
101​
102### onInsert()
103​
104```ts
105onInsert: (params) => Promise<any> = wrappedOnInsert;
106```
107​
108Create wrapper handlers that call user handlers first, then confirm transactions
109Wraps the user's onInsert handler to also confirm the transaction immediately
110​
111#### Parameters
112​
113##### params
114​
115[`InsertMutationFnParams`](../../type-aliases/insertmutationfnparams.md)\<[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>, `TKey`, [`LocalOnlyCollectionUtils`](../../interfaces/localonlycollectionutils.md)\>
116​
117#### Returns
118​
119`Promise`\<`any`\>
120​
121### onUpdate()
122​
123```ts
124onUpdate: (params) => Promise<any> = wrappedOnUpdate;
125```
126​
127Wrapper for onUpdate handler that also confirms the transaction immediately
128​
129#### Parameters
130​
131##### params
132​
133[`UpdateMutationFnParams`](../../type-aliases/updatemutationfnparams.md)\<[`ResolveType`](../../type-aliases/resolvetype.md)\<`TExplicit`, `TSchema`, `TFallback`\>, `TKey`, [`LocalOnlyCollectionUtils`](../../interfaces/localonlycollectionutils.md)\>
134​
135#### Returns
136​
137`Promise`\<`any`\>
138​
139### schema?
140​
141```ts
142optional schema: TSchema;
143```
144​
145### startSync
146​
147```ts
148startSync: boolean = true;
149```
150​
151### sync
152​
153```ts
154sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, TKey> = syncResult.sync;
155```
156​
157### utils
158​
159```ts
160utils: LocalOnlyCollectionUtils;
161```
162​
163## Examples
164​
165```ts
166// Basic local-only collection
167const collection = createCollection(
168 localOnlyCollectionOptions({
169 getKey: (item) => item.id,
170 })
172```
173​
174```ts
175// Local-only collection with initial data
176const collection = createCollection(
177 localOnlyCollectionOptions({
178 getKey: (item) => item.id,
179 initialData: [
180 { id: 1, name: 'Item 1' },
181 { id: 2, name: 'Item 2' },
182 ],
183 })
185```
186​
187```ts
188// Local-only collection with mutation handlers
189const collection = createCollection(
190 localOnlyCollectionOptions({
191 getKey: (item) => item.id,
192 onInsert: async ({ transaction }) => {
193 console.log('Item inserted:', transaction.mutations[0].modified)
194 // Custom logic after insert
195 },
196 })
198```