Open History to see the last commit
1---
2id: queryCollectionOptions
3title: queryCollectionOptions
4---
5
6## Call Signature
7
8```ts
9function queryCollectionOptions<T, TQueryFn, TError, TQueryKey, TKey, TQueryData>(config): CollectionConfig<InferSchemaOutput<T>, TKey, T, QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>> & object;
10```
11
12Defined in: [packages/query-db-collection/src/query.ts:575](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L575)
13
14Creates query collection options for use with a standard Collection.
15This integrates TanStack Query with TanStack DB for automatic synchronization.
16
17Supports automatic type inference following the priority order:
181. Schema inference (highest priority)
192. QueryFn return type inference (second priority)
20
21### Type Parameters
22
23#### T
24
25`T` *extends* `StandardSchemaV1`\<`unknown`, `unknown`\>
26
27Type of the schema if a schema is provided otherwise it is the type of the values returned by the queryFn
28
29#### TQueryFn
30
31`TQueryFn` *extends* (`context`) => `any`
32
33#### TError
34
35`TError` = `unknown`
36
37The type of errors that can occur during queries
38
39#### TQueryKey
40
41`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
42
43The type of the query key
44
45#### TKey
46
47`TKey` *extends* `string` \| `number` = `string` \| `number`
48
49The type of the item keys
50
51#### TQueryData
52
53`TQueryData` = `Awaited`\<`ReturnType`\<`TQueryFn`\>\>
54
55### Parameters
56
57#### config
58
59`Omit`\<[`QueryCollectionConfig`](../interfaces/QueryCollectionConfig.md)\<`InferSchemaOutput`\<`T`\>, `TQueryFn`, `TError`, `TQueryKey`, `TKey`, `T`, `TQueryData`\>, `"select"`\> & `object`
60
61Configuration options for the Query collection
62
63### Returns
64
65`CollectionConfig`\<`InferSchemaOutput`\<`T`\>, `TKey`, `T`, [`QueryCollectionUtils`](../interfaces/QueryCollectionUtils.md)\<`InferSchemaOutput`\<`T`\>, `TKey`, `InferSchemaInput`\<`T`\>, `TError`\>\> & `object`
66
67Collection options with utilities for direct writes and manual operations
68
69### Examples
70
71```ts
72// Type inferred from queryFn return type (NEW!)
73const todosCollection = createCollection(
74 queryCollectionOptions({
75 queryKey: ['todos'],
76 queryFn: async () => {
77 const response = await fetch('/api/todos')
78 return response.json() as Todo[] // Type automatically inferred!
79 },
80 queryClient,
81 getKey: (item) => item.id, // item is typed as Todo
82 })
83)
84```
85
86```ts
87// Explicit type
88const todosCollection = createCollection<Todo>(
89 queryCollectionOptions({
90 queryKey: ['todos'],
91 queryFn: async () => fetch('/api/todos').then(r => r.json()),
92 queryClient,
93 getKey: (item) => item.id,
94 })
95)
96```
97
98```ts
99// Schema inference
100const todosCollection = createCollection(
101 queryCollectionOptions({
102 queryKey: ['todos'],
103 queryFn: async () => fetch('/api/todos').then(r => r.json()),
104 queryClient,
105 schema: todoSchema, // Type inferred from schema
106 getKey: (item) => item.id,
107 })
108)
109```
110
111```ts
112// With persistence handlers
113const todosCollection = createCollection(
114 queryCollectionOptions({
115 queryKey: ['todos'],
116 queryFn: fetchTodos,
117 queryClient,
118 getKey: (item) => item.id,
119 onInsert: async ({ transaction }) => {
120 await api.createTodos(transaction.mutations.map(m => m.modified))
121 },
122 onUpdate: async ({ transaction }) => {
123 await api.updateTodos(transaction.mutations)
124 },
125 onDelete: async ({ transaction }) => {
126 await api.deleteTodos(transaction.mutations.map(m => m.key))
127 }
128 })
129)
130```
131
132```ts
133// The select option extracts the items array from a response with metadata
134const todosCollection = createCollection(
135 queryCollectionOptions({
136 queryKey: ['todos'],
137 queryFn: async () => fetch('/api/todos').then(r => r.json()),
138 select: (data) => data.items, // Extract the array of items
139 queryClient,
140 schema: todoSchema,
141 getKey: (item) => item.id,
142 })
143)
144```
145
146## Call Signature
147
148```ts
149function queryCollectionOptions<T, TQueryFn, TError, TQueryKey, TKey, TQueryData>(config): CollectionConfig<T, TKey, never, QueryCollectionUtils<T, TKey, T, TError>> & object;
150```
151
152Defined in: [packages/query-db-collection/src/query.ts:614](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L614)
153
154Creates query collection options for use with a standard Collection.
155This integrates TanStack Query with TanStack DB for automatic synchronization.
156
157Supports automatic type inference following the priority order:
1581. Schema inference (highest priority)
1592. QueryFn return type inference (second priority)
160
161### Type Parameters
162
163#### T
164
165`T` *extends* `object`
166
167Type of the schema if a schema is provided otherwise it is the type of the values returned by the queryFn
168
169#### TQueryFn
170
171`TQueryFn` *extends* (`context`) => `any` = (`context`) => `any`
172
173#### TError
174
175`TError` = `unknown`
176
177The type of errors that can occur during queries
178
179#### TQueryKey
180
181`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
182
183The type of the query key
184
185#### TKey
186
187`TKey` *extends* `string` \| `number` = `string` \| `number`
188
189The type of the item keys
190
191#### TQueryData
192
193`TQueryData` = `Awaited`\<`ReturnType`\<`TQueryFn`\>\>
194
195### Parameters
196
197#### config
198
199[`QueryCollectionConfig`](../interfaces/QueryCollectionConfig.md)\<`T`, `TQueryFn`, `TError`, `TQueryKey`, `TKey`, `never`, `TQueryData`\> & `object`
200
201Configuration options for the Query collection
202
203### Returns
204
205`CollectionConfig`\<`T`, `TKey`, `never`, [`QueryCollectionUtils`](../interfaces/QueryCollectionUtils.md)\<`T`, `TKey`, `T`, `TError`\>\> & `object`
206
207Collection options with utilities for direct writes and manual operations
208
209### Examples
210
211```ts
212// Type inferred from queryFn return type (NEW!)
213const todosCollection = createCollection(
214 queryCollectionOptions({
215 queryKey: ['todos'],
216 queryFn: async () => {
217 const response = await fetch('/api/todos')
218 return response.json() as Todo[] // Type automatically inferred!
219 },
220 queryClient,
221 getKey: (item) => item.id, // item is typed as Todo
222 })
223)
224```
225
226```ts
227// Explicit type
228const todosCollection = createCollection<Todo>(
229 queryCollectionOptions({
230 queryKey: ['todos'],
231 queryFn: async () => fetch('/api/todos').then(r => r.json()),
232 queryClient,
233 getKey: (item) => item.id,
234 })
235)
236```
237
238```ts
239// Schema inference
240const todosCollection = createCollection(
241 queryCollectionOptions({
242 queryKey: ['todos'],
243 queryFn: async () => fetch('/api/todos').then(r => r.json()),
244 queryClient,
245 schema: todoSchema, // Type inferred from schema
246 getKey: (item) => item.id,
247 })
248)
249```
250
251```ts
252// With persistence handlers
253const todosCollection = createCollection(
254 queryCollectionOptions({
255 queryKey: ['todos'],
256 queryFn: fetchTodos,
257 queryClient,
258 getKey: (item) => item.id,
259 onInsert: async ({ transaction }) => {
260 await api.createTodos(transaction.mutations.map(m => m.modified))
261 },
262 onUpdate: async ({ transaction }) => {
263 await api.updateTodos(transaction.mutations)
264 },
265 onDelete: async ({ transaction }) => {
266 await api.deleteTodos(transaction.mutations.map(m => m.key))
267 }
268 })
269)
270```
271
272```ts
273// The select option extracts the items array from a response with metadata
274const todosCollection = createCollection(
275 queryCollectionOptions({
276 queryKey: ['todos'],
277 queryFn: async () => fetch('/api/todos').then(r => r.json()),
278 select: (data) => data.items, // Extract the array of items
279 queryClient,
280 schema: todoSchema,
281 getKey: (item) => item.id,
282 })
283)
284```
285
286## Call Signature
287
288```ts
289function queryCollectionOptions<T, TError, TQueryKey, TKey>(config): CollectionConfig<InferSchemaOutput<T>, TKey, T, QueryCollectionUtils<InferSchemaOutput<T>, TKey, InferSchemaInput<T>, TError>> & object;
290```
291
292Defined in: [packages/query-db-collection/src/query.ts:647](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L647)
293
294Creates query collection options for use with a standard Collection.
295This integrates TanStack Query with TanStack DB for automatic synchronization.
296
297Supports automatic type inference following the priority order:
2981. Schema inference (highest priority)
2992. QueryFn return type inference (second priority)
300
301### Type Parameters
302
303#### T
304
305`T` *extends* `StandardSchemaV1`\<`unknown`, `unknown`\>
306
307Type of the schema if a schema is provided otherwise it is the type of the values returned by the queryFn
308
309#### TError
310
311`TError` = `unknown`
312
313The type of errors that can occur during queries
314
315#### TQueryKey
316
317`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
318
319The type of the query key
320
321#### TKey
322
323`TKey` *extends* `string` \| `number` = `string` \| `number`
324
325The type of the item keys
326
327### Parameters
328
329#### config
330
331[`QueryCollectionConfig`](../interfaces/QueryCollectionConfig.md)\<`InferSchemaOutput`\<`T`\>, (`context`) => `InferSchemaOutput`\<`T`\>[] \| `Promise`\<`InferSchemaOutput`\<`T`\>[]\>, `TError`, `TQueryKey`, `TKey`, `T`, `InferSchemaOutput`\<`T`\>[]\> & `object`
332
333Configuration options for the Query collection
334
335### Returns
336
337`CollectionConfig`\<`InferSchemaOutput`\<`T`\>, `TKey`, `T`, [`QueryCollectionUtils`](../interfaces/QueryCollectionUtils.md)\<`InferSchemaOutput`\<`T`\>, `TKey`, `InferSchemaInput`\<`T`\>, `TError`\>\> & `object`
338
339Collection options with utilities for direct writes and manual operations
340
341### Examples
342
343```ts
344// Type inferred from queryFn return type (NEW!)
345const todosCollection = createCollection(
346 queryCollectionOptions({
347 queryKey: ['todos'],
348 queryFn: async () => {
349 const response = await fetch('/api/todos')
350 return response.json() as Todo[] // Type automatically inferred!
351 },
352 queryClient,
353 getKey: (item) => item.id, // item is typed as Todo
354 })
355)
356```
357
358```ts
359// Explicit type
360const todosCollection = createCollection<Todo>(
361 queryCollectionOptions({
362 queryKey: ['todos'],
363 queryFn: async () => fetch('/api/todos').then(r => r.json()),
364 queryClient,
365 getKey: (item) => item.id,
366 })
367)
368```
369
370```ts
371// Schema inference
372const todosCollection = createCollection(
373 queryCollectionOptions({
374 queryKey: ['todos'],
375 queryFn: async () => fetch('/api/todos').then(r => r.json()),
376 queryClient,
377 schema: todoSchema, // Type inferred from schema
378 getKey: (item) => item.id,
379 })
380)
381```
382
383```ts
384// With persistence handlers
385const todosCollection = createCollection(
386 queryCollectionOptions({
387 queryKey: ['todos'],
388 queryFn: fetchTodos,
389 queryClient,
390 getKey: (item) => item.id,
391 onInsert: async ({ transaction }) => {
392 await api.createTodos(transaction.mutations.map(m => m.modified))
393 },
394 onUpdate: async ({ transaction }) => {
395 await api.updateTodos(transaction.mutations)
396 },
397 onDelete: async ({ transaction }) => {
398 await api.deleteTodos(transaction.mutations.map(m => m.key))
399 }
400 })
401)
402```
403
404```ts
405// The select option extracts the items array from a response with metadata
406const todosCollection = createCollection(
407 queryCollectionOptions({
408 queryKey: ['todos'],
409 queryFn: async () => fetch('/api/todos').then(r => r.json()),
410 select: (data) => data.items, // Extract the array of items
411 queryClient,
412 schema: todoSchema,
413 getKey: (item) => item.id,
414 })
415)
416```
417
418## Call Signature
419
420```ts
421function queryCollectionOptions<T, TError, TQueryKey, TKey>(config): CollectionConfig<T, TKey, never, QueryCollectionUtils<T, TKey, T, TError>> & object;
422```
423
424Defined in: [packages/query-db-collection/src/query.ts:681](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L681)
425
426Creates query collection options for use with a standard Collection.
427This integrates TanStack Query with TanStack DB for automatic synchronization.
428
429Supports automatic type inference following the priority order:
4301. Schema inference (highest priority)
4312. QueryFn return type inference (second priority)
432
433### Type Parameters
434
435#### T
436
437`T` *extends* `object`
438
439Type of the schema if a schema is provided otherwise it is the type of the values returned by the queryFn
440
441#### TError
442
443`TError` = `unknown`
444
445The type of errors that can occur during queries
446
447#### TQueryKey
448
449`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
450
451The type of the query key
452
453#### TKey
454
455`TKey` *extends* `string` \| `number` = `string` \| `number`
456
457The type of the item keys
458
459### Parameters
460
461#### config
462
463[`QueryCollectionConfig`](../interfaces/QueryCollectionConfig.md)\<`T`, (`context`) => `T`[] \| `Promise`\<`T`[]\>, `TError`, `TQueryKey`, `TKey`, `never`, `T`[]\> & `object`
464
465Configuration options for the Query collection
466
467### Returns
468
469`CollectionConfig`\<`T`, `TKey`, `never`, [`QueryCollectionUtils`](../interfaces/QueryCollectionUtils.md)\<`T`, `TKey`, `T`, `TError`\>\> & `object`
470
471Collection options with utilities for direct writes and manual operations
472
473### Examples
474
475```ts
476// Type inferred from queryFn return type (NEW!)
477const todosCollection = createCollection(
478 queryCollectionOptions({
479 queryKey: ['todos'],
480 queryFn: async () => {
481 const response = await fetch('/api/todos')
482 return response.json() as Todo[] // Type automatically inferred!
483 },
484 queryClient,
485 getKey: (item) => item.id, // item is typed as Todo
486 })
487)
488```
489
490```ts
491// Explicit type
492const todosCollection = createCollection<Todo>(
493 queryCollectionOptions({
494 queryKey: ['todos'],
495 queryFn: async () => fetch('/api/todos').then(r => r.json()),
496 queryClient,
497 getKey: (item) => item.id,
498 })
499)
500```
501
502```ts
503// Schema inference
504const todosCollection = createCollection(
505 queryCollectionOptions({
506 queryKey: ['todos'],
507 queryFn: async () => fetch('/api/todos').then(r => r.json()),
508 queryClient,
509 schema: todoSchema, // Type inferred from schema
510 getKey: (item) => item.id,
511 })
512)
513```
514
515```ts
516// With persistence handlers
517const todosCollection = createCollection(
518 queryCollectionOptions({
519 queryKey: ['todos'],
520 queryFn: fetchTodos,
521 queryClient,
522 getKey: (item) => item.id,
523 onInsert: async ({ transaction }) => {
524 await api.createTodos(transaction.mutations.map(m => m.modified))
525 },
526 onUpdate: async ({ transaction }) => {
527 await api.updateTodos(transaction.mutations)
528 },
529 onDelete: async ({ transaction }) => {
530 await api.deleteTodos(transaction.mutations.map(m => m.key))
531 }
532 })
533)
534```
535
536```ts
537// The select option extracts the items array from a response with metadata
538const todosCollection = createCollection(
539 queryCollectionOptions({
540 queryKey: ['todos'],
541 queryFn: async () => fetch('/api/todos').then(r => r.json()),
542 select: (data) => data.items, // Extract the array of items
543 queryClient,
544 schema: todoSchema,
545 getKey: (item) => item.id,
546 })
547)
548```