Skip to content

Commit 9531cc8

Browse files
feat(query-db-collection): add meta support to QueryCollectionConfig (#363)
Co-authored-by: Kyle Mathews <[email protected]>
1 parent f0f3259 commit 9531cc8

File tree

6 files changed

+224
-24
lines changed

6 files changed

+224
-24
lines changed

.changeset/silent-monkeys-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/query-db-collection": patch
3+
---
4+
5+
Add meta support to QueryCollectionConfig to allow passing additional context to queryFn.

docs/reference/query-db-collection/functions/querycollectionoptions.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ title: queryCollectionOptions
88
# Function: queryCollectionOptions()
99

1010
```ts
11-
function queryCollectionOptions<TItem, TError, TQueryKey>(config): CollectionConfig<TItem, string | number, StandardSchemaV1<unknown, unknown>, TItem> & object
11+
function queryCollectionOptions<TItem, TError, TQueryKey, TKey, TInsertInput>(config): CollectionConfig<TItem, string | number, StandardSchemaV1<unknown, unknown>, TItem> & object
1212
```
1313

14-
Defined in: [packages/query-db-collection/src/query.ts:235](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L235)
14+
Defined in: [packages/query-db-collection/src/query.ts:277](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L277)
1515

1616
Creates query collection options for use with a standard Collection
1717

@@ -23,6 +23,10 @@ Creates query collection options for use with a standard Collection
2323

2424
**TQueryKey** *extends* readonly `unknown`[] = readonly `unknown`[]
2525

26+
**TKey** *extends* `string` \| `number` = `string` \| `number`
27+
28+
**TInsertInput** *extends* `object` = `TItem`
29+
2630
## Parameters
2731

2832
### config

docs/reference/query-db-collection/interfaces/querycollectionconfig.md

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: QueryCollectionConfig
77

88
# Interface: QueryCollectionConfig\<TItem, TError, TQueryKey\>
99

10-
Defined in: [packages/query-db-collection/src/query.ts:26](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L26)
10+
Defined in: [packages/query-db-collection/src/query.ts:32](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L32)
1111

1212
## Type Parameters
1313

@@ -25,7 +25,7 @@ Defined in: [packages/query-db-collection/src/query.ts:26](https://github.com/Ta
2525
optional enabled: boolean;
2626
```
2727

28-
Defined in: [packages/query-db-collection/src/query.ts:36](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L36)
28+
Defined in: [packages/query-db-collection/src/query.ts:42](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L42)
2929

3030
***
3131

@@ -35,7 +35,7 @@ Defined in: [packages/query-db-collection/src/query.ts:36](https://github.com/Ta
3535
getKey: (item) => string | number;
3636
```
3737

38-
Defined in: [packages/query-db-collection/src/query.ts:68](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L68)
38+
Defined in: [packages/query-db-collection/src/query.ts:74](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L74)
3939

4040
#### Parameters
4141

@@ -55,7 +55,39 @@ Defined in: [packages/query-db-collection/src/query.ts:68](https://github.com/Ta
5555
optional id: string;
5656
```
5757

58-
Defined in: [packages/query-db-collection/src/query.ts:67](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L67)
58+
Defined 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
65+
optional meta: Record<string, unknown>;
66+
```
67+
68+
Defined 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+
70+
Metadata to pass to the query.
71+
Available in queryFn via context.meta
72+
73+
#### Example
74+
75+
```ts
76+
// Using meta for error context
77+
queryFn: 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+
},
87+
meta: {
88+
errorMessage: `Failed to load todos for user ${userId}`
89+
}
90+
```
5991

6092
***
6193

@@ -65,7 +97,7 @@ Defined in: [packages/query-db-collection/src/query.ts:67](https://github.com/Ta
6597
optional onDelete: DeleteMutationFn<TItem>;
6698
```
6799

68-
Defined in: [packages/query-db-collection/src/query.ts:213](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L213)
100+
Defined in: [packages/query-db-collection/src/query.ts:219](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L219)
69101

70102
Optional asynchronous handler function called before a delete operation
71103

@@ -131,7 +163,7 @@ onDelete: async ({ transaction, collection }) => {
131163
optional onInsert: InsertMutationFn<TItem>;
132164
```
133165

134-
Defined in: [packages/query-db-collection/src/query.ts:114](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L114)
166+
Defined in: [packages/query-db-collection/src/query.ts:120](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L120)
135167

136168
Optional asynchronous handler function called before an insert operation
137169

@@ -193,7 +225,7 @@ onInsert: async ({ transaction }) => {
193225
optional onUpdate: UpdateMutationFn<TItem>;
194226
```
195227

196-
Defined in: [packages/query-db-collection/src/query.ts:167](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L167)
228+
Defined in: [packages/query-db-collection/src/query.ts:173](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L173)
197229

198230
Optional asynchronous handler function called before an update operation
199231

@@ -266,7 +298,7 @@ onUpdate: async ({ transaction, collection }) => {
266298
queryClient: QueryClient;
267299
```
268300

269-
Defined in: [packages/query-db-collection/src/query.ts:33](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L33)
301+
Defined in: [packages/query-db-collection/src/query.ts:39](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L39)
270302

271303
***
272304

@@ -276,7 +308,7 @@ Defined in: [packages/query-db-collection/src/query.ts:33](https://github.com/Ta
276308
queryFn: (context) => Promise<TItem[]>;
277309
```
278310

279-
Defined in: [packages/query-db-collection/src/query.ts:32](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L32)
311+
Defined in: [packages/query-db-collection/src/query.ts:38](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L38)
280312

281313
#### Parameters
282314

@@ -322,7 +354,7 @@ if you want access to the direction, you can add it to the pageParam
322354
queryKey: TQueryKey;
323355
```
324356

325-
Defined in: [packages/query-db-collection/src/query.ts:31](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L31)
357+
Defined in: [packages/query-db-collection/src/query.ts:37](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L37)
326358

327359
***
328360

@@ -332,7 +364,7 @@ Defined in: [packages/query-db-collection/src/query.ts:31](https://github.com/Ta
332364
optional refetchInterval: number | false | (query) => undefined | number | false;
333365
```
334366

335-
Defined in: [packages/query-db-collection/src/query.ts:37](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L37)
367+
Defined in: [packages/query-db-collection/src/query.ts:43](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L43)
336368

337369
***
338370

@@ -342,7 +374,7 @@ Defined in: [packages/query-db-collection/src/query.ts:37](https://github.com/Ta
342374
optional retry: RetryValue<TError>;
343375
```
344376

345-
Defined in: [packages/query-db-collection/src/query.ts:44](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L44)
377+
Defined in: [packages/query-db-collection/src/query.ts:50](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L50)
346378

347379
***
348380

@@ -352,7 +384,7 @@ Defined in: [packages/query-db-collection/src/query.ts:44](https://github.com/Ta
352384
optional retryDelay: RetryDelayValue<TError>;
353385
```
354386

355-
Defined in: [packages/query-db-collection/src/query.ts:51](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L51)
387+
Defined in: [packages/query-db-collection/src/query.ts:57](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L57)
356388

357389
***
358390

@@ -362,17 +394,17 @@ Defined in: [packages/query-db-collection/src/query.ts:51](https://github.com/Ta
362394
optional schema: StandardSchemaV1<unknown, unknown>;
363395
```
364396

365-
Defined in: [packages/query-db-collection/src/query.ts:69](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L69)
397+
Defined in: [packages/query-db-collection/src/query.ts:75](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L75)
366398

367399
***
368400

369401
### staleTime?
370402

371403
```ts
372-
optional staleTime: StaleTime<TItem[], TError, TItem[], TQueryKey>;
404+
optional staleTime: StaleTimeFunction<TItem[], TError, TItem[], TQueryKey>;
373405
```
374406

375-
Defined in: [packages/query-db-collection/src/query.ts:58](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L58)
407+
Defined in: [packages/query-db-collection/src/query.ts:64](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L64)
376408

377409
***
378410

@@ -382,7 +414,7 @@ Defined in: [packages/query-db-collection/src/query.ts:58](https://github.com/Ta
382414
optional startSync: boolean;
383415
```
384416

385-
Defined in: [packages/query-db-collection/src/query.ts:71](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L71)
417+
Defined in: [packages/query-db-collection/src/query.ts:77](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L77)
386418

387419
***
388420

@@ -392,4 +424,4 @@ Defined in: [packages/query-db-collection/src/query.ts:71](https://github.com/Ta
392424
optional sync: SyncConfig<TItem, string | number>;
393425
```
394426

395-
Defined in: [packages/query-db-collection/src/query.ts:70](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L70)
427+
Defined in: [packages/query-db-collection/src/query.ts:76](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L76)

docs/reference/query-db-collection/interfaces/querycollectionutils.md

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@ title: QueryCollectionUtils
55

66
<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
77

8-
# Interface: QueryCollectionUtils
8+
# Interface: QueryCollectionUtils\<TItem, TKey, TInsertInput\>
99

10-
Defined in: [packages/query-db-collection/src/query.ts:225](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L225)
10+
Defined in: [packages/query-db-collection/src/query.ts:256](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L256)
1111

12-
Query collection utilities type
12+
Write operation types for batch operations
1313

1414
## Extends
1515

1616
- `UtilsRecord`
1717

18+
## Type Parameters
19+
20+
**TItem** *extends* `object` = `Record`\<`string`, `unknown`\>
21+
22+
**TKey** *extends* `string` \| `number` = `string` \| `number`
23+
24+
**TInsertInput** *extends* `object` = `TItem`
25+
1826
## Indexable
1927

2028
```ts
@@ -29,4 +37,104 @@ Query collection utilities type
2937
refetch: RefetchFn;
3038
```
3139

32-
Defined in: [packages/query-db-collection/src/query.ts:226](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L226)
40+
Defined in: [packages/query-db-collection/src/query.ts:261](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L261)
41+
42+
***
43+
44+
### writeBatch()
45+
46+
```ts
47+
writeBatch: (operations) => void;
48+
```
49+
50+
Defined in: [packages/query-db-collection/src/query.ts:266](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L266)
51+
52+
#### Parameters
53+
54+
##### operations
55+
56+
[`SyncOperation`](../../type-aliases/syncoperation.md)\<`TItem`, `TKey`, `TInsertInput`\>[]
57+
58+
#### Returns
59+
60+
`void`
61+
62+
***
63+
64+
### writeDelete()
65+
66+
```ts
67+
writeDelete: (keys) => void;
68+
```
69+
70+
Defined in: [packages/query-db-collection/src/query.ts:264](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L264)
71+
72+
#### Parameters
73+
74+
##### keys
75+
76+
`TKey` | `TKey`[]
77+
78+
#### Returns
79+
80+
`void`
81+
82+
***
83+
84+
### writeInsert()
85+
86+
```ts
87+
writeInsert: (data) => void;
88+
```
89+
90+
Defined in: [packages/query-db-collection/src/query.ts:262](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L262)
91+
92+
#### Parameters
93+
94+
##### data
95+
96+
`TInsertInput` | `TInsertInput`[]
97+
98+
#### Returns
99+
100+
`void`
101+
102+
***
103+
104+
### writeUpdate()
105+
106+
```ts
107+
writeUpdate: (updates) => void;
108+
```
109+
110+
Defined in: [packages/query-db-collection/src/query.ts:263](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L263)
111+
112+
#### Parameters
113+
114+
##### updates
115+
116+
`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
117+
118+
#### Returns
119+
120+
`void`
121+
122+
***
123+
124+
### writeUpsert()
125+
126+
```ts
127+
writeUpsert: (data) => void;
128+
```
129+
130+
Defined in: [packages/query-db-collection/src/query.ts:265](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L265)
131+
132+
#### Parameters
133+
134+
##### data
135+
136+
`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
137+
138+
#### Returns
139+
140+
`void`

packages/query-db-collection/src/query.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,28 @@ export interface QueryCollectionConfig<
218218
*/
219219
onDelete?: DeleteMutationFn<TItem>
220220
// TODO type returning { refetch: boolean }
221+
222+
/**
223+
* Metadata to pass to the query.
224+
* Available in queryFn via context.meta
225+
*
226+
* @example
227+
* // Using meta for error context
228+
* queryFn: async (context) => {
229+
* try {
230+
* return await api.getTodos(userId)
231+
* } catch (error) {
232+
* // Use meta for better error messages
233+
* throw new Error(
234+
* context.meta?.errorMessage || 'Failed to load todos'
235+
* )
236+
* }
237+
* },
238+
* meta: {
239+
* errorMessage: `Failed to load todos for user ${userId}`
240+
* }
241+
*/
242+
meta?: Record<string, unknown>
221243
}
222244

223245
/**
@@ -276,6 +298,7 @@ export function queryCollectionOptions<
276298
onInsert,
277299
onUpdate,
278300
onDelete,
301+
meta,
279302
...baseCollectionConfig
280303
} = config
281304

@@ -312,6 +335,7 @@ export function queryCollectionOptions<
312335
> = {
313336
queryKey: queryKey,
314337
queryFn: queryFn,
338+
meta: meta,
315339
enabled: enabled,
316340
refetchInterval: refetchInterval,
317341
retry: retry,

0 commit comments

Comments
 (0)