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

QueryCollectionUtils.md

282 lines6.0 KB

1---
2id: QueryCollectionUtils
3title: QueryCollectionUtils
4---
5​
6Defined in: [packages/query-db-collection/src/query.ts:290](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L290)
7​
8Utility methods available on Query Collections for direct writes and manual operations.
9Direct writes bypass optimistic mutations and write to the synced data store.
10Eager collections patch Query cache; on-demand collections revalidate scoped entries.
11​
12## Type Parameters
13​
14### TItem
15​
16`TItem` *extends* `object` = `Record`\<`string`, `unknown`\>
17​
18The type of items stored in the collection
19​
20### TKey
21​
22`TKey` *extends* `string` \| `number` = `string` \| `number`
23​
24The type of the item keys
25​
26### TInsertInput
27​
28`TInsertInput` *extends* `object` = `TItem`
29​
30The type accepted for insert operations
31​
32### TError
33​
34`TError` = `unknown`
35​
36The type of errors that can occur during queries
37​
38## Properties
39​
40### clearError()
41​
42```ts
43clearError: () => Promise<void>;
44```
45​
46Defined in: [packages/query-db-collection/src/query.ts:344](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L344)
47​
48Refetch, retaining errors until a successful result applies. While a user
49mutation is persisting or its handler is active, this retains the Query
50fetch boundary so it cannot wait on publication blocked by that
51transaction.
52​
53#### Returns
54​
55`Promise`\<`void`\>
56​
57Promise that resolves when the applicable refetch boundary completes
58​
59#### Throws
60​
61Error if the refetch fails
62​
63***
64​
65### dataUpdatedAt
66​
67```ts
68dataUpdatedAt: number;
69```
70​
71Defined in: [packages/query-db-collection/src/query.ts:328](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L328)
72​
73Get timestamp of last successful data update (in milliseconds)
74​
75***
76​
77### errorCount
78​
79```ts
80errorCount: number;
81```
82​
83Defined in: [packages/query-db-collection/src/query.ts:320](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L320)
84​
85Get the number of consecutive sync failures.
86Incremented only when query fails completely (not per retry attempt); reset after a successful result applies.
87​
88***
89​
90### fetchStatus
91​
92```ts
93fetchStatus: "idle" | "fetching" | "paused";
94```
95​
96Defined in: [packages/query-db-collection/src/query.ts:334](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L334)
97​
98Get the aggregate observer fetch status. Returns `fetching` if any
99observer is fetching, otherwise `paused` if any observer is paused, and
100`idle` when every observer is idle or no observers exist.
101​
102***
103​
104### isError
105​
106```ts
107isError: boolean;
108```
109​
110Defined in: [packages/query-db-collection/src/query.ts:315](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L315)
111​
112Check if the collection is in an error state
113​
114***
115​
116### isFetching
117​
118```ts
119isFetching: boolean;
120```
121​
122Defined in: [packages/query-db-collection/src/query.ts:322](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L322)
123​
124Check if query is currently fetching (initial or background)
125​
126***
127​
128### isLoading
129​
130```ts
131isLoading: boolean;
132```
133​
134Defined in: [packages/query-db-collection/src/query.ts:326](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L326)
135​
136Check if query is loading for the first time (no data yet)
137​
138***
139​
140### isRefetching
141​
142```ts
143isRefetching: boolean;
144```
145​
146Defined in: [packages/query-db-collection/src/query.ts:324](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L324)
147​
148Check if query is refetching in background (not initial fetch)
149​
150***
151​
152### lastError
153​
154```ts
155lastError: TError | undefined;
156```
157​
158Defined in: [packages/query-db-collection/src/query.ts:313](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L313)
159​
160Get the last error encountered by the query (if any); reset after a successful result applies
161​
162***
163​
164### refetch
165​
166```ts
167refetch: RefetchFn;
168```
169​
170Defined in: [packages/query-db-collection/src/query.ts:299](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L299)
171​
172Manually refetch and await the applicable fetch or application boundary.
173​
174***
175​
176### writeBatch()
177​
178```ts
179writeBatch: (callback) => void;
180```
181​
182Defined in: [packages/query-db-collection/src/query.ts:309](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L309)
183​
184Execute direct writes as one atomic batch, then update or revalidate the Query cache
185​
186#### Parameters
187​
188##### callback
189​
190() => `void`
191​
192#### Returns
193​
194`void`
195​
196***
197​
198### writeDelete()
199​
200```ts
201writeDelete: (keys) => void;
202```
203​
204Defined in: [packages/query-db-collection/src/query.ts:305](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L305)
205​
206Delete items without an optimistic update. On-demand queries revalidate their scoped cache entries.
207​
208#### Parameters
209​
210##### keys
211​
212`TKey` | `TKey`[]
213​
214#### Returns
215​
216`void`
217​
218***
219​
220### writeInsert()
221​
222```ts
223writeInsert: (data) => void;
224```
225​
226Defined in: [packages/query-db-collection/src/query.ts:301](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L301)
227​
228Insert items without an optimistic update. On-demand queries revalidate their scoped cache entries.
229​
230#### Parameters
231​
232##### data
233​
234`TInsertInput` | `TInsertInput`[]
235​
236#### Returns
237​
238`void`
239​
240***
241​
242### writeUpdate()
243​
244```ts
245writeUpdate: (updates) => void;
246```
247​
248Defined in: [packages/query-db-collection/src/query.ts:303](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L303)
249​
250Update items without an optimistic update. On-demand queries revalidate their scoped cache entries.
251​
252#### Parameters
253​
254##### updates
255​
256`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
257​
258#### Returns
259​
260`void`
261​
262***
263​
264### writeUpsert()
265​
266```ts
267writeUpsert: (data) => void;
268```
269​
270Defined in: [packages/query-db-collection/src/query.ts:307](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L307)
271​
272Insert or update items without an optimistic update. On-demand queries revalidate their scoped cache entries.
273​
274#### Parameters
275​
276##### data
277​
278`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
279​
280#### Returns
281​
282`void`