(opts: LoadSubsetOptions)
| 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 |
no test coverage detected