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

QueryCollectionUtils.md

282 lines6.1 KB

1---
2id: QueryCollectionUtils
3title: QueryCollectionUtils
4---
5​
6Defined in: [packages/query-db-collection/src/query.ts:291](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L291)
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:347](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L347)
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:331](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L331)
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:323](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L323)
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:337](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L337)
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:318](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L318)
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:325](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L325)
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:329](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L329)
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:327](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L327)
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:316](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L316)
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:300](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L300)
171​
172Manually refetch and await the applicable fetch or application boundary.
173​
174***
175​
176### writeBatch()
177​
178```ts
179writeBatch: (callback) => Promise<void>;
180```
181​
182Defined in: [packages/query-db-collection/src/query.ts:312](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L312)
183​
184Execute direct writes as one atomic batch. Resolves when the sync commit applies.
185​
186#### Parameters
187​
188##### callback
189​
190() => `void`
191​
192#### Returns
193​
194`Promise`\<`void`\>
195​
196***
197​
198### writeDelete()
199​
200```ts
201writeDelete: (keys) => Promise<void>;
202```
203​
204Defined in: [packages/query-db-collection/src/query.ts:308](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L308)
205​
206Delete items without an optimistic update. Resolves when the sync commit applies.
207​
208#### Parameters
209​
210##### keys
211​
212`TKey` | `TKey`[]
213​
214#### Returns
215​
216`Promise`\<`void`\>
217​
218***
219​
220### writeInsert()
221​
222```ts
223writeInsert: (data) => Promise<void>;
224```
225​
226Defined in: [packages/query-db-collection/src/query.ts:302](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L302)
227​
228Insert items without an optimistic update. Resolves when the sync commit applies.
229​
230#### Parameters
231​
232##### data
233​
234`TInsertInput` | `TInsertInput`[]
235​
236#### Returns
237​
238`Promise`\<`void`\>
239​
240***
241​
242### writeUpdate()
243​
244```ts
245writeUpdate: (updates) => Promise<void>;
246```
247​
248Defined in: [packages/query-db-collection/src/query.ts:304](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L304)
249​
250Update items without an optimistic update. Resolves when the sync commit applies.
251​
252#### Parameters
253​
254##### updates
255​
256`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
257​
258#### Returns
259​
260`Promise`\<`void`\>
261​
262***
263​
264### writeUpsert()
265​
266```ts
267writeUpsert: (data) => Promise<void>;
268```
269​
270Defined in: [packages/query-db-collection/src/query.ts:310](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L310)
271​
272Insert or update items without an optimistic update. Resolves when the sync commit applies.
273​
274#### Parameters
275​
276##### data
277​
278`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
279​
280#### Returns
281​
282`Promise`\<`void`\>