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

QueryCollectionUtils.md

223 lines4.6 KB

1---
2id: QueryCollectionUtils
3title: QueryCollectionUtils
4---
5​
6Defined in: [packages/query-db-collection/src/query.ts:301](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L301)
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## Template
14​
15The type of errors that can occur during queries
16​
17## Type Parameters
18​
19### TItem
20​
21`TItem` *extends* `object` = `Record`\<`string`, `unknown`\>
22​
23The type of items stored in the collection
24​
25### TKey
26​
27`TKey` *extends* `string` \| `number` = `string` \| `number`
28​
29The type of the item keys
30​
31### TInsertInput
32​
33`TInsertInput` *extends* `object` = `TItem`
34​
35The type accepted for insert operations
36​
37### _TError
38​
39`_TError` = `unknown`
40​
41## Properties
42​
43### dataUpdatedAt
44​
45```ts
46dataUpdatedAt: number;
47```
48​
49Defined in: [packages/query-db-collection/src/query.ts:334](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L334)
50​
51Get timestamp of last successful data update (in milliseconds)
52​
53***
54​
55### fetchStatus
56​
57```ts
58fetchStatus: "idle" | "fetching" | "paused";
59```
60​
61Defined in: [packages/query-db-collection/src/query.ts:340](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L340)
62​
63Get the aggregate observer fetch status. Returns `fetching` if any
64observer is fetching, otherwise `paused` if any observer is paused, and
65`idle` when every observer is idle or no observers exist.
66​
67***
68​
69### isFetching
70​
71```ts
72isFetching: boolean;
73```
74​
75Defined in: [packages/query-db-collection/src/query.ts:328](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L328)
76​
77Check if query is currently fetching (initial or background)
78​
79***
80​
81### isLoading
82​
83```ts
84isLoading: boolean;
85```
86​
87Defined in: [packages/query-db-collection/src/query.ts:332](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L332)
88​
89Check if query is loading for the first time (no data yet)
90​
91***
92​
93### isRefetching
94​
95```ts
96isRefetching: boolean;
97```
98​
99Defined in: [packages/query-db-collection/src/query.ts:330](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L330)
100​
101Check if query is refetching in background (not initial fetch)
102​
103***
104​
105### refetch
106​
107```ts
108refetch: RefetchFn;
109```
110​
111Defined in: [packages/query-db-collection/src/query.ts:311](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L311)
112​
113Manually refetch and await the applicable fetch or application boundary.
114​
115***
116​
117### writeBatch()
118​
119```ts
120writeBatch: (callback) => Promise<void>;
121```
122​
123Defined in: [packages/query-db-collection/src/query.ts:323](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L323)
124​
125Execute direct writes as one atomic batch. Resolves when the sync commit applies.
126​
127#### Parameters
128​
129##### callback
130​
131() => `void`
132​
133#### Returns
134​
135`Promise`\<`void`\>
136​
137***
138​
139### writeDelete()
140​
141```ts
142writeDelete: (keys) => Promise<void>;
143```
144​
145Defined in: [packages/query-db-collection/src/query.ts:319](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L319)
146​
147Delete items without an optimistic update. Resolves when the sync commit applies.
148​
149#### Parameters
150​
151##### keys
152​
153`TKey` | `TKey`[]
154​
155#### Returns
156​
157`Promise`\<`void`\>
158​
159***
160​
161### writeInsert()
162​
163```ts
164writeInsert: (data) => Promise<void>;
165```
166​
167Defined in: [packages/query-db-collection/src/query.ts:313](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L313)
168​
169Insert items without an optimistic update. Resolves when the sync commit applies.
170​
171#### Parameters
172​
173##### data
174​
175`TInsertInput` | `TInsertInput`[]
176​
177#### Returns
178​
179`Promise`\<`void`\>
180​
181***
182​
183### writeUpdate()
184​
185```ts
186writeUpdate: (updates) => Promise<void>;
187```
188​
189Defined in: [packages/query-db-collection/src/query.ts:315](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L315)
190​
191Update items without an optimistic update. Resolves when the sync commit applies.
192​
193#### Parameters
194​
195##### updates
196​
197`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
198​
199#### Returns
200​
201`Promise`\<`void`\>
202​
203***
204​
205### writeUpsert()
206​
207```ts
208writeUpsert: (data) => Promise<void>;
209```
210​
211Defined in: [packages/query-db-collection/src/query.ts:321](https://github.com/TanStack/db/blob/main/packages/query-db-collection/src/query.ts#L321)
212​
213Insert or update items without an optimistic update. Resolves when the sync commit applies.
214​
215#### Parameters
216​
217##### data
218​
219`Partial`\<`TItem`\> | `Partial`\<`TItem`\>[]
220​
221#### Returns
222​
223`Promise`\<`void`\>