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

QueryCollectionUtils.md

287 lines5.7 KB

1---
2id: QueryCollectionUtils
3title: QueryCollectionUtils
4---
5​
6Defined in: [packages/query-db-collection/src/query.ts:262](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L262)
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## Extends
13​
14- `UtilsRecord`
15​
16## Type Parameters
17​
18### TItem
19​
20`TItem` *extends* `object` = `Record`\<`string`, `unknown`\>
21​
22The type of items stored in the collection
23​
24### TKey
25​
26`TKey` *extends* `string` \| `number` = `string` \| `number`
27​
28The type of the item keys
29​
30### TInsertInput
31​
32`TInsertInput` *extends* `object` = `TItem`
33​
34The type accepted for insert operations
35​
36### TError
37​
38`TError` = `unknown`
39​
40The type of errors that can occur during queries
41​
42## Indexable
43​
44```ts
45[key: string]: any
46```
47​
48## Properties
49​
50### clearError()
51​
52```ts
53clearError: () => Promise<void>;
54```
55​
56Defined in: [packages/query-db-collection/src/query.ts:306](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L306)
57​
58Clear the error state and trigger a refetch of the query
59​
60#### Returns
61​
62`Promise`\<`void`\>
63​
64Promise that resolves when the refetch completes successfully
65​
66#### Throws
67​
68Error if the refetch fails
69​
70***
71​
72### dataUpdatedAt
73​
74```ts
75dataUpdatedAt: number;
76```
77​
78Defined in: [packages/query-db-collection/src/query.ts:297](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L297)
79​
80Get timestamp of last successful data update (in milliseconds)
81​
82***
83​
84### errorCount
85​
86```ts
87errorCount: number;
88```
89​
90Defined in: [packages/query-db-collection/src/query.ts:289](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L289)
91​
92Get the number of consecutive sync failures.
93Incremented only when query fails completely (not per retry attempt); reset on success.
94​
95***
96​
97### fetchStatus
98​
99```ts
100fetchStatus: "idle" | "fetching" | "paused";
101```
102​
103Defined in: [packages/query-db-collection/src/query.ts:299](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L299)
104​
105Get current fetch status
106​
107***
108​
109### isError
110​
111```ts
112isError: boolean;
113```
114​
115Defined in: [packages/query-db-collection/src/query.ts:284](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L284)
116​
117Check if the collection is in an error state
118​
119***
120​
121### isFetching
122​
123```ts
124isFetching: boolean;
125```
126​
127Defined in: [packages/query-db-collection/src/query.ts:291](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L291)
128​
129Check if query is currently fetching (initial or background)
130​
131***
132​
133### isLoading
134​
135```ts
136isLoading: boolean;
137```
138​
139Defined in: [packages/query-db-collection/src/query.ts:295](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L295)
140​
141Check if query is loading for the first time (no data yet)
142​
143***
144​
145### isRefetching
146​
147```ts
148isRefetching: boolean;
149```
150​
151Defined in: [packages/query-db-collection/src/query.ts:293](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L293)
152​
153Check if query is refetching in background (not initial fetch)
154​
155***
156​
157### lastError
158​
159```ts
160lastError: TError | undefined;
161```
162​
163Defined in: [packages/query-db-collection/src/query.ts:282](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L282)
164​
165Get the last error encountered by the query (if any); reset on success
166​
167***
168​
169### refetch
170​
171```ts
172refetch: RefetchFn;
173```
174​
175Defined in: [packages/query-db-collection/src/query.ts:268](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L268)
176​
177Manually trigger a refetch of the query
178​
179***
180​
181### writeBatch()
182​
183```ts
184writeBatch: (callback) => void;
185```
186​
187Defined in: [packages/query-db-collection/src/query.ts:278](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L278)
188​
189Execute direct writes as one atomic batch, then update or revalidate the Query cache
190​
191#### Parameters
192​
193##### callback
194​
195() => `void`
196​
197#### Returns
198​
199`void`
200​
201***
202​
203### writeDelete()
204​
205```ts
206writeDelete: (keys) => void;
207```
208​
209Defined in: [packages/query-db-collection/src/query.ts:274](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L274)
210​
211Delete items without an optimistic update. On-demand queries revalidate their scoped cache entries.
212​
213#### Parameters
214​
215##### keys
216​
217`TKey` | `TKey`[]
218​
219#### Returns
220​
221`void`
222​
223***
224​
225### writeInsert()
226​
227```ts
228writeInsert: (data) => void;
229```
230​
231Defined in: [packages/query-db-collection/src/query.ts:270](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L270)
232​
233Insert items without an optimistic update. On-demand queries revalidate their scoped cache entries.
234​
235#### Parameters
236​
237##### data
238​
239`TInsertInput` | `TInsertInput`[]
240​
241#### Returns
242​
243`void`
244​
245***
246​
247### writeUpdate()
248​
249```ts
250writeUpdate: (updates) => void;
251```
252​
253Defined in: [packages/query-db-collection/src/query.ts:272](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L272)
254​
255Update items without an optimistic update. On-demand queries revalidate their scoped cache entries.
256​
257#### Parameters
258​
259##### updates
260​
261`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
262​
263#### Returns
264​
265`void`
266​
267***
268​
269### writeUpsert()
270​
271```ts
272writeUpsert: (data) => void;
273```
274​
275Defined in: [packages/query-db-collection/src/query.ts:276](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L276)
276​
277Insert or update items without an optimistic update. On-demand queries revalidate their scoped cache entries.
278​
279#### Parameters
280​
281##### data
282​
283`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
284​
285#### Returns
286​
287`void`