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

QueryCollectionUtils.md

283 lines6.1 KB

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