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

querycollectionconfig.md

427 lines9.3 KB

1---
2id: QueryCollectionConfig
3title: QueryCollectionConfig
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8# Interface: QueryCollectionConfig\<TItem, TError, TQueryKey\>
9​
10Defined in: [packages/query-db-collection/src/query.ts:32](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L32)
11​
12## Type Parameters
13​
14• **TItem** *extends* `object`
15​
16• **TError** = `unknown`
17​
18• **TQueryKey** *extends* `QueryKey` = `QueryKey`
19​
20## Properties
21​
22### enabled?
23​
24```ts
25optional enabled: boolean;
26```
27​
28Defined in: [packages/query-db-collection/src/query.ts:42](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L42)
29​
30***
31​
32### getKey()
33​
34```ts
35getKey: (item) => string | number;
36```
37​
38Defined in: [packages/query-db-collection/src/query.ts:74](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L74)
39​
40#### Parameters
41​
42##### item
43​
44`TItem`
45​
46#### Returns
47​
48`string` \| `number`
49​
50***
51​
52### id?
53​
54```ts
55optional id: string;
56```
57​
58Defined in: [packages/query-db-collection/src/query.ts:73](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L73)
59​
60***
61​
62### meta?
63​
64```ts
65optional meta: Record<string, unknown>;
66```
67​
68Defined in: [packages/query-db-collection/src/query.ts:242](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L242)
69​
70Metadata to pass to the query.
71Available in queryFn via context.meta
72​
73#### Example
74​
75```ts
76// Using meta for error context
77queryFn: async (context) => {
78 try {
79 return await api.getTodos(userId)
80 } catch (error) {
81 // Use meta for better error messages
82 throw new Error(
83 context.meta?.errorMessage || 'Failed to load todos'
84 )
85 }
86},
87meta: {
88 errorMessage: `Failed to load todos for user ${userId}`
90```
91​
92***
93​
94### onDelete?
95​
96```ts
97optional onDelete: DeleteMutationFn<TItem>;
98```
99​
100Defined in: [packages/query-db-collection/src/query.ts:219](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L219)
101​
102Optional asynchronous handler function called before a delete operation
103​
104#### Param
105​
106Object containing transaction and collection information
107​
108#### Returns
109​
110Promise resolving to void or { refetch?: boolean } to control refetching
111​
112#### Examples
113​
114```ts
115// Basic query collection delete handler
116onDelete: async ({ transaction }) => {
117 const mutation = transaction.mutations[0]
118 await api.deleteTodo(mutation.original.id)
119 // Automatically refetches query after delete
121```
122​
123```ts
124// Delete handler with refetch control
125onDelete: async ({ transaction }) => {
126 const mutation = transaction.mutations[0]
127 await api.deleteTodo(mutation.original.id)
128 return { refetch: false } // Skip automatic refetch
130```
131​
132```ts
133// Delete handler with multiple items
134onDelete: async ({ transaction }) => {
135 const keysToDelete = transaction.mutations.map(m => m.key)
136 await api.deleteTodos(keysToDelete)
137 // Will refetch query to get updated data
139```
140​
141```ts
142// Delete handler with related collection refetch
143onDelete: async ({ transaction, collection }) => {
144 const mutation = transaction.mutations[0]
145 await api.deleteTodo(mutation.original.id)
146​
147 // Refetch related collections when this item is deleted
148 await Promise.all([
149 collection.utils.refetch(), // Refetch this collection
150 usersCollection.utils.refetch(), // Refetch users
151 projectsCollection.utils.refetch() // Refetch projects
152 ])
153​
154 return { refetch: false } // Skip automatic refetch since we handled it manually
156```
157​
158***
159​
160### onInsert?
161​
162```ts
163optional onInsert: InsertMutationFn<TItem>;
164```
165​
166Defined in: [packages/query-db-collection/src/query.ts:120](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L120)
167​
168Optional asynchronous handler function called before an insert operation
169​
170#### Param
171​
172Object containing transaction and collection information
173​
174#### Returns
175​
176Promise resolving to void or { refetch?: boolean } to control refetching
177​
178#### Examples
179​
180```ts
181// Basic query collection insert handler
182onInsert: async ({ transaction }) => {
183 const newItem = transaction.mutations[0].modified
184 await api.createTodo(newItem)
185 // Automatically refetches query after insert
187```
188​
189```ts
190// Insert handler with refetch control
191onInsert: async ({ transaction }) => {
192 const newItem = transaction.mutations[0].modified
193 await api.createTodo(newItem)
194 return { refetch: false } // Skip automatic refetch
196```
197​
198```ts
199// Insert handler with multiple items
200onInsert: async ({ transaction }) => {
201 const items = transaction.mutations.map(m => m.modified)
202 await api.createTodos(items)
203 // Will refetch query to get updated data
205```
206​
207```ts
208// Insert handler with error handling
209onInsert: async ({ transaction }) => {
210 try {
211 const newItem = transaction.mutations[0].modified
212 await api.createTodo(newItem)
213 } catch (error) {
214 console.error('Insert failed:', error)
215 throw error // Transaction will rollback optimistic changes
216 }
218```
219​
220***
221​
222### onUpdate?
223​
224```ts
225optional onUpdate: UpdateMutationFn<TItem>;
226```
227​
228Defined in: [packages/query-db-collection/src/query.ts:173](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L173)
229​
230Optional asynchronous handler function called before an update operation
231​
232#### Param
233​
234Object containing transaction and collection information
235​
236#### Returns
237​
238Promise resolving to void or { refetch?: boolean } to control refetching
239​
240#### Examples
241​
242```ts
243// Basic query collection update handler
244onUpdate: async ({ transaction }) => {
245 const mutation = transaction.mutations[0]
246 await api.updateTodo(mutation.original.id, mutation.changes)
247 // Automatically refetches query after update
249```
250​
251```ts
252// Update handler with multiple items
253onUpdate: async ({ transaction }) => {
254 const updates = transaction.mutations.map(m => ({
255 id: m.key,
256 changes: m.changes
257 }))
258 await api.updateTodos(updates)
259 // Will refetch query to get updated data
261```
262​
263```ts
264// Update handler with manual refetch
265onUpdate: async ({ transaction, collection }) => {
266 const mutation = transaction.mutations[0]
267 await api.updateTodo(mutation.original.id, mutation.changes)
268​
269 // Manually trigger refetch
270 await collection.utils.refetch()
271​
272 return { refetch: false } // Skip automatic refetch
274```
275​
276```ts
277// Update handler with related collection refetch
278onUpdate: async ({ transaction, collection }) => {
279 const mutation = transaction.mutations[0]
280 await api.updateTodo(mutation.original.id, mutation.changes)
281​
282 // Refetch related collections when this item changes
283 await Promise.all([
284 collection.utils.refetch(), // Refetch this collection
285 usersCollection.utils.refetch(), // Refetch users
286 tagsCollection.utils.refetch() // Refetch tags
287 ])
288​
289 return { refetch: false } // Skip automatic refetch since we handled it manually
291```
292​
293***
294​
295### queryClient
296​
297```ts
298queryClient: QueryClient;
299```
300​
301Defined in: [packages/query-db-collection/src/query.ts:39](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L39)
302​
303***
304​
305### queryFn()
306​
307```ts
308queryFn: (context) => Promise<TItem[]>;
309```
310​
311Defined in: [packages/query-db-collection/src/query.ts:38](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L38)
312​
313#### Parameters
314​
315##### context
316​
317###### client
318​
319`QueryClient`
320​
321###### direction?
322​
323`unknown`
324​
325**Deprecated**
326​
327if you want access to the direction, you can add it to the pageParam
328​
329###### meta
330​
331`undefined` \| `Record`\<`string`, `unknown`\>
332​
333###### pageParam?
334​
335`unknown`
336​
337###### queryKey
338​
339`TQueryKey`
340​
341###### signal
342​
343`AbortSignal`
344​
345#### Returns
346​
347`Promise`\<`TItem`[]\>
348​
349***
350​
351### queryKey
352​
353```ts
354queryKey: TQueryKey;
355```
356​
357Defined in: [packages/query-db-collection/src/query.ts:37](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L37)
358​
359***
360​
361### refetchInterval?
362​
363```ts
364optional refetchInterval: number | false | (query) => undefined | number | false;
365```
366​
367Defined in: [packages/query-db-collection/src/query.ts:43](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L43)
368​
369***
370​
371### retry?
372​
373```ts
374optional retry: RetryValue<TError>;
375```
376​
377Defined in: [packages/query-db-collection/src/query.ts:50](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L50)
378​
379***
380​
381### retryDelay?
382​
383```ts
384optional retryDelay: RetryDelayValue<TError>;
385```
386​
387Defined in: [packages/query-db-collection/src/query.ts:57](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L57)
388​
389***
390​
391### schema?
392​
393```ts
394optional schema: StandardSchemaV1<unknown, unknown>;
395```
396​
397Defined in: [packages/query-db-collection/src/query.ts:75](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L75)
398​
399***
400​
401### staleTime?
402​
403```ts
404optional staleTime: StaleTimeFunction<TItem[], TError, TItem[], TQueryKey>;
405```
406​
407Defined in: [packages/query-db-collection/src/query.ts:64](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L64)
408​
409***
410​
411### startSync?
412​
413```ts
414optional startSync: boolean;
415```
416​
417Defined in: [packages/query-db-collection/src/query.ts:77](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L77)
418​
419***
420​
421### sync?
422​
423```ts
424optional sync: SyncConfig<TItem, string | number>;
425```
426​
427Defined in: [packages/query-db-collection/src/query.ts:76](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L76)