MCPcopy Create free account
hub / github.com/TanStack/db / load

Function load

packages/trailbase-db-collection/src/trailbase.ts:190–257  ·  view source on GitHub ↗
(opts: LoadSubsetOptions)

Source from the content-addressed store, hash-verified

188
189 // Load (more) data.
190 async function load(opts: LoadSubsetOptions) {
191 const lastKey = opts.cursor?.lastKey
192 let cursor: string | undefined =
193 lastKey !== undefined ? cursors.get(lastKey) : undefined
194 let offset: number | undefined =
195 (opts.offset ?? 0) > 0 ? opts.offset : undefined
196
197 const order: Array<string> | undefined = buildOrder(opts)
198 const filters: Array<FilterOrComposite> | undefined = buildFilters(
199 opts,
200 config,
201 )
202
203 let remaining: number = opts.limit ?? Number.MAX_VALUE
204 if (remaining <= 0) {
205 return
206 }
207
208 while (true) {
209 const limit = Math.min(remaining, 256)
210 const response = await config.recordApi.list({
211 pagination: {
212 limit,
213 offset,
214 cursor,
215 },
216 order,
217 filters,
218 })
219
220 const length = response.records.length
221 if (length === 0) {
222 // Drained - read everything.
223 break
224 }
225
226 begin()
227
228 for (let i = 0; i < Math.min(length, remaining); ++i) {
229 write({
230 type: `insert`,
231 value: parse(response.records[i]!),
232 })
233 }
234
235 commit()
236
237 remaining -= length
238
239 // Drained or read enough.
240 if (length < limit || remaining <= 0) {
241 if (response.cursor) {
242 cursors.set(
243 getKey(parse(response.records.at(-1)!)),
244 response.cursor,
245 )
246 }
247 break

Callers 1

startFunction · 0.85

Calls 6

buildOrderFunction · 0.85
buildFiltersFunction · 0.85
parseFunction · 0.85
getKeyFunction · 0.50
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected