1 | /**
|
2 | * ```ts
|
3 | * import type { ArrayCursor, BatchedArrayCursor } from "arangojs/cursor.js";
|
4 | * ```
|
5 | *
|
6 | * The "cursor" module provides cursor-related interfaces for TypeScript.
|
7 | *
|
8 | * @packageDocumentation
|
9 | */
|
10 | import { LinkedList } from "./lib/linkedList.js";
|
11 | import { Database } from "./database.js";
|
12 | /**
|
13 | * Additional information about the cursor.
|
14 | */
|
15 | export interface CursorExtras {
|
16 | /**
|
17 | * Warnings encountered while executing the query.
|
18 | */
|
19 | warnings: {
|
20 | code: number;
|
21 | message: string;
|
22 | }[];
|
23 | /**
|
24 | * Query execution plan for the executed query.
|
25 | */
|
26 | plan?: Record<string, any>;
|
27 | /**
|
28 | * Additional profiling information for the executed query.
|
29 | */
|
30 | profile?: Record<string, number>;
|
31 | /**
|
32 | * Additional statistics about the query execution.
|
33 | */
|
34 | stats?: CursorStats;
|
35 | }
|
36 | /**
|
37 | * Additional statics about the query execution of the cursor.
|
38 | */
|
39 | export interface CursorStats {
|
40 | /**
|
41 | * Total number of index entries read from in-memory caches for indexes of
|
42 | * type edge or persistent.
|
43 | */
|
44 | cacheHits: number;
|
45 | /**
|
46 | * Total number of cache read attempts for index entries that could not be
|
47 | * served from in-memory caches for indexes of type edge or persistent.
|
48 | */
|
49 | cacheMisses: number;
|
50 | /**
|
51 | * Total number of cursor objects created during query execution.
|
52 | */
|
53 | cursorsCreated: number;
|
54 | /**
|
55 | * Total number of times an existing cursor object was repurposed.
|
56 | */
|
57 | cursorsRearmed: number;
|
58 | /**
|
59 | * Total number of data-modification operations successfully executed.
|
60 | */
|
61 | writesExecuted: number;
|
62 | /**
|
63 | * Total number of data-modification operations that were unsuccessful, but have been ignored because of query option ignoreErrors.
|
64 | */
|
65 | writesIgnored: number;
|
66 | /**
|
67 | * Total number of documents iterated over when scanning a collection without an index.
|
68 | */
|
69 | scannedFull: number;
|
70 | /**
|
71 | * Total number of documents iterated over when scanning a collection using an index.
|
72 | */
|
73 | scannedIndex: number;
|
74 | /**
|
75 | * Total number of documents that were removed after executing a filter condition in a FilterNode.
|
76 | */
|
77 | filtered: number;
|
78 | /**
|
79 | * Maximum memory usage of the query while it was running.
|
80 | */
|
81 | peakMemoryUsage: number;
|
82 | /**
|
83 | * Execution time of the query in seconds.
|
84 | */
|
85 | executionTime: number;
|
86 | /**
|
87 | * Total number of documents that matched the search condition if the query’s final top-level LIMIT statement were not present.
|
88 | */
|
89 | fullCount?: number;
|
90 | /**
|
91 | * Total number of cluster-internal HTTP requests performed.
|
92 | */
|
93 | httpRequests: number;
|
94 | /**
|
95 | * Runtime statistics per query execution node if `profile` was set to `2` or greater.
|
96 | */
|
97 | nodes?: {
|
98 | /**
|
99 | * Execution node ID to correlate this node with nodes in the `extra.plan`.
|
100 | */
|
101 | id: number;
|
102 | /**
|
103 | * Number of calls in this node.
|
104 | */
|
105 | calls: number;
|
106 | /**
|
107 | * Number of temporary result items returned by this node.
|
108 | */
|
109 | items: number;
|
110 | filter: number;
|
111 | /**
|
112 | * Execution time of this node in seconds.
|
113 | */
|
114 | runtime: number;
|
115 | }[];
|
116 | }
|
117 | interface BatchView<T = any> {
|
118 | isEmpty: boolean;
|
119 | more(): Promise<void>;
|
120 | shift(): T | undefined;
|
121 | }
|
122 | /**
|
123 | * The `BatchedArrayCursor` provides a batch-wise API to an {@link ArrayCursor}.
|
124 | *
|
125 | * When using TypeScript, cursors can be cast to a specific item type in order
|
126 | * to increase type safety.
|
127 | *
|
128 | * @param T - Type to use for each item. Defaults to `any`.
|
129 | *
|
130 | * @example
|
131 | * ```ts
|
132 | * const db = new Database();
|
133 | * const query = aql`FOR x IN 1..5 RETURN x`;
|
134 | * const cursor = await db.query(query) as ArrayCursor<number>;
|
135 | * const batches = cursor.batches;
|
136 | * ```
|
137 | *
|
138 | * @example
|
139 | * ```js
|
140 | * const db = new Database();
|
141 | * const query = aql`FOR x IN 1..10000 RETURN x`;
|
142 | * const cursor = await db.query(query, { batchSize: 10 });
|
143 | * for await (const batch of cursor.batches) {
|
144 | * // Process all values in a batch in parallel
|
145 | * await Promise.all(batch.map(
|
146 | * value => asyncProcessValue(value)
|
147 | * ));
|
148 | * }
|
149 | * ```
|
150 | */
|
151 | export declare class BatchedArrayCursor<T = any> {
|
152 | protected _db: Database;
|
153 | protected _batches: LinkedList<LinkedList<any>>;
|
154 | protected _count?: number;
|
155 | protected _extra: CursorExtras;
|
156 | protected _hasMore: boolean;
|
157 | protected _nextBatchId?: string;
|
158 | protected _id: string | undefined;
|
159 | protected _hostUrl?: string;
|
160 | protected _allowDirtyRead?: boolean;
|
161 | protected _itemsCursor: ArrayCursor<T>;
|
162 | /**
|
163 | * @internal
|
164 | */
|
165 | constructor(db: Database, body: {
|
166 | extra: any;
|
167 | result: T[];
|
168 | hasMore: boolean;
|
169 | nextBatchId?: string;
|
170 | id: string;
|
171 | count: number;
|
172 | }, hostUrl?: string, allowDirtyRead?: boolean);
|
173 | protected _more(): Promise<void>;
|
174 | /**
|
175 | * An {set.
ArrayCursor} providing item-wise access to the cursor result |
176 | *
|
177 | * See also { ArrayCursor#batches}.
|
178 | */
|
179 | get items(): ArrayCursor<T>;
|
180 | /**
|
181 | * Additional information about the cursor.
|
182 | */
|
183 | get extra(): Readonly<CursorExtras>;
|
184 | /**
|
185 | * Total number of documents in the query result. Only available if the
|
186 | * `count` option was used.
|
187 | */
|
188 | get count(): number | undefined;
|
189 | /**
|
190 | * Whether the cursor has any remaining batches that haven't yet been
|
191 | * fetched. If set to `false`, all batches have been fetched and no
|
192 | * additional requests to the server will be made when consuming any
|
193 | * remaining batches from this cursor.
|
194 | */
|
195 | get hasMore(): boolean;
|
196 | /**
|
197 | * Whether the cursor has more batches. If set to `false`, the cursor has
|
198 | * already been depleted and contains no more batches.
|
199 | */
|
200 | get hasNext(): boolean;
|
201 | /**
|
202 | * Enables use with `for await` to deplete the cursor by asynchronously
|
203 | * yielding every batch in the cursor's remaining result set.
|
204 | *
|
205 | * **Note**: If the result set spans multiple batches, any remaining batches
|
206 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
207 | * processing speed, this may result in the server discarding the cursor
|
208 | * before it is fully depleted.
|
209 | *
|
210 | * @example
|
211 | * ```js
|
212 | * const cursor = await db.query(aql`
|
213 | * FOR user IN users
|
214 | * FILTER user.isActive
|
215 | * RETURN user
|
216 | * `);
|
217 | * for await (const users of cursor.batches) {
|
218 | * for (const user of users) {
|
219 | * console.log(user.email, user.isAdmin);
|
220 | * }
|
221 | * }
|
222 | * ```
|
223 | */
|
224 | [Symbol.asyncIterator](): AsyncGenerator<T[], undefined, undefined>;
|
225 | /**
|
226 | * Loads all remaining batches from the server.
|
227 | *
|
228 | * **Warning**: This may impact memory use when working with very large
|
229 | * query result sets.
|
230 | *
|
231 | * @example
|
232 | * ```js
|
233 | * const cursor = await db.query(
|
234 | * aql`FOR x IN 1..5 RETURN x`,
|
235 | * { batchSize: 1 }
|
236 | * );
|
237 | * console.log(cursor.hasMore); // true
|
238 | * await cursor.batches.loadAll();
|
239 | * console.log(cursor.hasMore); // false
|
240 | * console.log(cursor.hasNext); // true
|
241 | * for await (const item of cursor) {
|
242 | * console.log(item);
|
243 | * // No server roundtrips necessary any more
|
244 | * }
|
245 | * ```
|
246 | */
|
247 | loadAll(): Promise<void>;
|
248 | /**
|
249 | * Depletes the cursor, then returns an array containing all batches in the
|
250 | * cursor's remaining result list.
|
251 | *
|
252 | * @example
|
253 | * ```js
|
254 | * const cursor = await db.query(
|
255 | * aql`FOR x IN 1..5 RETURN x`,
|
256 | * { batchSize: 2 }
|
257 | * );
|
258 | * const result = await cursor.batches.all(); // [[1, 2], [3, 4], [5]]
|
259 | * console.log(cursor.hasNext); // false
|
260 | * ```
|
261 | */
|
262 | all(): Promise<T[][]>;
|
263 | /**
|
264 | * Advances the cursor and returns all remaining values in the cursor's
|
265 | * current batch. If the current batch has already been exhausted, fetches
|
266 | * the next batch from the server and returns it, or `undefined` if the
|
267 | * cursor has been depleted.
|
268 | *
|
269 | * **Note**: If the result set spans multiple batches, any remaining batches
|
270 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
271 | * processing speed, this may result in the server discarding the cursor
|
272 | * before it is fully depleted.
|
273 | *
|
274 | * @example
|
275 | * ```js
|
276 | * const cursor = await db.query(
|
277 | * aql`FOR i IN 1..10 RETURN i`,
|
278 | * { batchSize: 5 }
|
279 | * );
|
280 | * const firstBatch = await cursor.batches.next(); // [1, 2, 3, 4, 5]
|
281 | * await cursor.next(); // 6
|
282 | * const lastBatch = await cursor.batches.next(); // [7, 8, 9, 10]
|
283 | * console.log(cursor.hasNext); // false
|
284 | * ```
|
285 | */
|
286 | next(): Promise<T[] | undefined>;
|
287 | /**
|
288 | * Advances the cursor by applying the `callback` function to each item in
|
289 | * the cursor's remaining result list until the cursor is depleted or
|
290 | * `callback` returns the exact value `false`. Returns a promise that
|
291 | * evalues to `true` unless the function returned `false`.
|
292 | *
|
293 | * **Note**: If the result set spans multiple batches, any remaining batches
|
294 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
295 | * processing speed, this may result in the server discarding the cursor
|
296 | * before it is fully depleted.
|
297 | *
|
298 | * See also:
|
299 | * [`Array.prototype.forEach`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach).
|
300 | *
|
301 | * @param callback - Function to execute on each element.
|
302 | *
|
303 | * @example
|
304 | * ```js
|
305 | * const cursor = await db.query(
|
306 | * aql`FOR x IN 1..5 RETURN x`,
|
307 | * { batchSize: 2 }
|
308 | * );
|
309 | * const result = await cursor.batches.forEach((currentBatch) => {
|
310 | * for (const value of currentBatch) {
|
311 | * console.log(value);
|
312 | * }
|
313 | * });
|
314 | * console.log(result) // true
|
315 | * console.log(cursor.hasNext); // false
|
316 | * ```
|
317 | *
|
318 | * @example
|
319 | * ```js
|
320 | * const cursor = await db.query(
|
321 | * aql`FOR x IN 1..5 RETURN x`,
|
322 | * { batchSize: 2 }
|
323 | * );
|
324 | * const result = await cursor.batches.forEach((currentBatch) => {
|
325 | * for (const value of currentBatch) {
|
326 | * console.log(value);
|
327 | * }
|
328 | * return false; // stop after the first batch
|
329 | * });
|
330 | * console.log(result); // false
|
331 | * console.log(cursor.hasNext); // true
|
332 | * ```
|
333 | */
|
334 | forEach(callback: (currentBatch: T[], index: number, self: this) => false | void): Promise<boolean>;
|
335 | /**
|
336 | * Depletes the cursor by applying the `callback` function to each batch in
|
337 | * the cursor's remaining result list. Returns an array containing the
|
338 | * return values of `callback` for each batch.
|
339 | *
|
340 | * **Note**: This creates an array of all return values, which may impact
|
341 | * memory use when working with very large query result sets. Consider using
|
342 | * {@link BatchedArrayCursor#forEach}, {@link BatchedArrayCursor#reduce} or
|
343 | * {@link BatchedArrayCursor#flatMap} instead.
|
344 | *
|
345 | * See also:
|
346 | * [`Array.prototype.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
|
347 | *
|
348 | * @param R - Return type of the `callback` function.
|
349 | * @param callback - Function to execute on each element.
|
350 | *
|
351 | * @example
|
352 | * ```js
|
353 | * const cursor = await db.query(
|
354 | * aql`FOR x IN 1..5 RETURN x`,
|
355 | * { batchSize: 2 }
|
356 | * );
|
357 | * const squares = await cursor.batches.map((currentBatch) => {
|
358 | * return currentBatch.map((value) => value ** 2);
|
359 | * });
|
360 | * console.log(squares); // [[1, 4], [9, 16], [25]]
|
361 | * console.log(cursor.hasNext); // false
|
362 | * ```
|
363 | */
|
364 | map<R>(callback: (currentBatch: T[], index: number, self: this) => R): Promise<R[]>;
|
365 | /**
|
366 | * Depletes the cursor by applying the `callback` function to each batch in
|
367 | * the cursor's remaining result list. Returns an array containing the
|
368 | * return values of `callback` for each batch, flattened to a depth of 1.
|
369 | *
|
370 | * **Note**: If the result set spans multiple batches, any remaining batches
|
371 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
372 | * processing speed, this may result in the server discarding the cursor
|
373 | * before it is fully depleted.
|
374 | *
|
375 | * See also:
|
376 | * [`Array.prototype.flatMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap).
|
377 | *
|
378 | * @param R - Return type of the `callback` function.
|
379 | * @param callback - Function to execute on each element.
|
380 | *
|
381 | * @example
|
382 | * ```js
|
383 | * const cursor = await db.query(
|
384 | * aql`FOR x IN 1..5 RETURN x`,
|
385 | * { batchSize: 2 }
|
386 | * );
|
387 | * const squares = await cursor.batches.flatMap((currentBatch) => {
|
388 | * return currentBatch.map((value) => value ** 2);
|
389 | * });
|
390 | * console.log(squares); // [1, 1, 2, 4, 3, 9, 4, 16, 5, 25]
|
391 | * console.log(cursor.hasNext); // false
|
392 | * ```
|
393 | *
|
394 | * @example
|
395 | * ```js
|
396 | * const cursor = await db.query(
|
397 | * aql`FOR x IN 1..5 RETURN x`,
|
398 | * { batchSize: 1 }
|
399 | * );
|
400 | * const odds = await cursor.batches.flatMap((currentBatch) => {
|
401 | * if (currentBatch[0] % 2 === 0) {
|
402 | * return []; // empty array flattens into nothing
|
403 | * }
|
404 | * return currentBatch;
|
405 | * });
|
406 | * console.logs(odds); // [1, 3, 5]
|
407 | * ```
|
408 | */
|
409 | flatMap<R>(callback: (currentBatch: T[], index: number, self: this) => R | R[]): Promise<R[]>;
|
410 | /**
|
411 | * Depletes the cursor by applying the `reducer` function to each batch in
|
412 | * the cursor's remaining result list. Returns the return value of `reducer`
|
413 | * for the last batch.
|
414 | *
|
415 | * **Note**: Most complex uses of the `reduce` method can be replaced with
|
416 | * simpler code using {@link BatchedArrayCursor#forEach} or the `for await`
|
417 | * syntax.
|
418 | *
|
419 | * **Note**: If the result set spans multiple batches, any remaining batches
|
420 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
421 | * processing speed, this may result in the server discarding the cursor
|
422 | * before it is fully depleted.
|
423 | *
|
424 | * See also:
|
425 | * [`Array.prototype.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
|
426 | *
|
427 | * @param R - Return type of the `reducer` function.
|
428 | * @param reducer - Function to execute on each element.
|
429 | * @param initialValue - Initial value of the `accumulator` value passed to
|
430 | * the `reducer` function.
|
431 | *
|
432 | * @example
|
433 | * ```js
|
434 | * function largestValue(baseline, values) {
|
435 | * return Math.max(baseline, ...values);
|
436 | * }
|
437 | * const cursor = await db.query(
|
438 | * aql`FOR x IN 1..5 RETURN x`,
|
439 | * { batchSize: 3 }
|
440 | * );
|
441 | * const result = await cursor.batches.reduce(largestValue, 0);
|
442 | * console.log(result); // 5
|
443 | * console.log(cursor.hasNext); // false
|
444 | * const emptyResult = await cursor.batches.reduce(largestValue, 0);
|
445 | * console.log(emptyResult); // 0
|
446 | * ```
|
447 | *
|
448 | * @example
|
449 | * ```js
|
450 | * // BAD! NEEDLESSLY COMPLEX!
|
451 | * const cursor = await db.query(
|
452 | * aql`FOR x IN 1..5 RETURN x`,
|
453 | * { batchSize: 1 }
|
454 | * );
|
455 | * const result = await cursor.reduce((accumulator, currentBatch) => {
|
456 | * if (currentBatch[0] % 2 === 0) {
|
457 | * accumulator.even.push(...currentBatch);
|
458 | * } else {
|
459 | * accumulator.odd.push(...currentBatch);
|
460 | * }
|
461 | * return accumulator;
|
462 | * }, { odd: [], even: [] });
|
463 | * console.log(result); // { odd: [1, 3, 5], even: [2, 4] }
|
464 | *
|
465 | * // GOOD! MUCH SIMPLER!
|
466 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
467 | * const odd = [];
|
468 | * const even = [];
|
469 | * for await (const currentBatch of cursor) {
|
470 | * if (currentBatch[0] % 2 === 0) {
|
471 | * even.push(...currentBatch);
|
472 | * } else {
|
473 | * odd.push(...currentBatch);
|
474 | * }
|
475 | * }
|
476 | * console.log({ odd, even }); // { odd: [1, 3, 5], even: [2, 4] }
|
477 | * ```
|
478 | */
|
479 | reduce<R>(reducer: (accumulator: R, currentBatch: T[], index: number, self: this) => R, initialValue: R): Promise<R>;
|
480 | /**
|
481 | * Depletes the cursor by applying the `reducer` function to each batch in
|
482 | * the cursor's remaining result list. Returns the return value of `reducer`
|
483 | * for the last batch.
|
484 | *
|
485 | * **Note**: If the result set spans multiple batches, any remaining batches
|
486 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
487 | * processing speed, this may result in the server discarding the cursor
|
488 | * before it is fully depleted.
|
489 | *
|
490 | * See also:
|
491 | * [`Array.prototype.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
|
492 | *
|
493 | * @param R - Return type of the `reducer` function.
|
494 | * @param reducer - Function to execute on each element.
|
495 | *
|
496 | * @example
|
497 | * ```js
|
498 | * function largestValue(values1, values2) {
|
499 | * return [Math.max(...values1, ...values2)];
|
500 | * }
|
501 | * const cursor = await db.query(
|
502 | * aql`FOR x IN 1..5 RETURN x`,
|
503 | * { batchSize: 3 }
|
504 | * );
|
505 | * const result = await cursor.batches.reduce(largestValue);
|
506 | * console.log(result); // [5]
|
507 | * console.log(cursor.hasNext); // false
|
508 | * ```
|
509 | *
|
510 | */
|
511 | reduce<R>(reducer: (accumulator: T[] | R, currentBatch: T[], index: number, self: this) => R): Promise<R | undefined>;
|
512 | /**
|
513 | * Drains the cursor and frees up associated database resources.
|
514 | *
|
515 | * This method has no effect if all batches have already been consumed.
|
516 | *
|
517 | * @example
|
518 | * ```js
|
519 | * const cursor1 = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
520 | * console.log(cursor1.hasMore); // false
|
521 | * await cursor1.kill(); // no effect
|
522 | *
|
523 | * const cursor2 = await db.query(
|
524 | * aql`FOR x IN 1..5 RETURN x`,
|
525 | * { batchSize: 2 }
|
526 | * );
|
527 | * console.log(cursor2.hasMore); // true
|
528 | * await cursor2.kill(); // cursor is depleted
|
529 | * ```
|
530 | */
|
531 | kill(): Promise<void>;
|
532 | }
|
533 | /**
|
534 | * The `ArrayCursor` type represents a cursor returned from a
|
535 | * {@link database.Database#query}.
|
536 | *
|
537 | * When using TypeScript, cursors can be cast to a specific item type in order
|
538 | * to increase type safety.
|
539 | *
|
540 | * See also {@link BatchedArrayCursor}.
|
541 | *
|
542 | * @param T - Type to use for each item. Defaults to `any`.
|
543 | *
|
544 | * @example
|
545 | * ```ts
|
546 | * const db = new Database();
|
547 | * const query = aql`FOR x IN 1..5 RETURN x`;
|
548 | * const result = await db.query(query) as ArrayCursor<number>;
|
549 | * ```
|
550 | *
|
551 | * @example
|
552 | * ```js
|
553 | * const db = new Database();
|
554 | * const query = aql`FOR x IN 1..10 RETURN x`;
|
555 | * const cursor = await db.query(query);
|
556 | * for await (const value of cursor) {
|
557 | * // Process each value asynchronously
|
558 | * await processValue(value);
|
559 | * }
|
560 | * ```
|
561 | */
|
562 | export declare class ArrayCursor<T = any> {
|
563 | protected _batches: BatchedArrayCursor<T>;
|
564 | protected _view: BatchView<T>;
|
565 | /**
|
566 | * @internal
|
567 | */
|
568 | constructor(batchedCursor: BatchedArrayCursor, view: BatchView<T>);
|
569 | /**
|
570 | * A {@link BatchedArrayCursor} providing batch-wise access to the cursor
|
571 | * result set.
|
572 | *
|
573 | * See also {@link BatchedArrayCursor#items}.
|
574 | */
|
575 | get batches(): BatchedArrayCursor<T>;
|
576 | /**
|
577 | * Additional information about the cursor.
|
578 | */
|
579 | get extra(): CursorExtras;
|
580 | /**
|
581 | * Total number of documents in the query result. Only available if the
|
582 | * `count` option was used.
|
583 | */
|
584 | get count(): number | undefined;
|
585 | /**
|
586 | * Whether the cursor has more values. If set to `false`, the cursor has
|
587 | * already been depleted and contains no more items.
|
588 | */
|
589 | get hasNext(): boolean;
|
590 | /**
|
591 | * Enables use with `for await` to deplete the cursor by asynchronously
|
592 | * yielding every value in the cursor's remaining result set.
|
593 | *
|
594 | * **Note**: If the result set spans multiple batches, any remaining batches
|
595 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
596 | * processing speed, this may result in the server discarding the cursor
|
597 | * before it is fully depleted.
|
598 | *
|
599 | * @example
|
600 | * ```js
|
601 | * const cursor = await db.query(aql`
|
602 | * FOR user IN users
|
603 | * FILTER user.isActive
|
604 | * RETURN user
|
605 | * `);
|
606 | * for await (const user of cursor) {
|
607 | * console.log(user.email, user.isAdmin);
|
608 | * }
|
609 | * ```
|
610 | */
|
611 | [Symbol.asyncIterator](): AsyncGenerator<T, undefined, undefined>;
|
612 | /**
|
613 | * Depletes the cursor, then returns an array containing all values in the
|
614 | * cursor's remaining result list.
|
615 | *
|
616 | * @example
|
617 | * ```js
|
618 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
619 | * const result = await cursor.all(); // [1, 2, 3, 4, 5]
|
620 | * console.log(cursor.hasNext); // false
|
621 | * ```
|
622 | */
|
623 | all(): Promise<T[]>;
|
624 | /**
|
625 | * Advances the cursor and returns the next value in the cursor's remaining
|
626 | * result list, or `undefined` if the cursor has been depleted.
|
627 | *
|
628 | * **Note**: If the result set spans multiple batches, any remaining batches
|
629 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
630 | * processing speed, this may result in the server discarding the cursor
|
631 | * before it is fully depleted.
|
632 | *
|
633 | * @example
|
634 | * ```js
|
635 | * const cursor = await db.query(aql`FOR x IN 1..3 RETURN x`);
|
636 | * const one = await cursor.next(); // 1
|
637 | * const two = await cursor.next(); // 2
|
638 | * const three = await cursor.next(); // 3
|
639 | * const empty = await cursor.next(); // undefined
|
640 | * ```
|
641 | */
|
642 | next(): Promise<T | undefined>;
|
643 | /**
|
644 | * Advances the cursor by applying the `callback` function to each item in
|
645 | * the cursor's remaining result list until the cursor is depleted or
|
646 | * `callback` returns the exact value `false`. Returns a promise that
|
647 | * evalues to `true` unless the function returned `false`.
|
648 | *
|
649 | * **Note**: If the result set spans multiple batches, any remaining batches
|
650 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
651 | * processing speed, this may result in the server discarding the cursor
|
652 | * before it is fully depleted.
|
653 | *
|
654 | * See also:
|
655 | * [`Array.prototype.forEach`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach).
|
656 | *
|
657 | * @param callback - Function to execute on each element.
|
658 | *
|
659 | * @example
|
660 | * ```js
|
661 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
662 | * const result = await cursor.forEach((currentValue) => {
|
663 | * console.log(currentValue);
|
664 | * });
|
665 | * console.log(result) // true
|
666 | * console.log(cursor.hasNext); // false
|
667 | * ```
|
668 | *
|
669 | * @example
|
670 | * ```js
|
671 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
672 | * const result = await cursor.forEach((currentValue) => {
|
673 | * console.log(currentValue);
|
674 | * return false; // stop after the first item
|
675 | * });
|
676 | * console.log(result); // false
|
677 | * console.log(cursor.hasNext); // true
|
678 | * ```
|
679 | */
|
680 | forEach(callback: (currentValue: T, index: number, self: this) => false | void): Promise<boolean>;
|
681 | /**
|
682 | * Depletes the cursor by applying the `callback` function to each item in
|
683 | * the cursor's remaining result list. Returns an array containing the
|
684 | * return values of `callback` for each item.
|
685 | *
|
686 | * **Note**: This creates an array of all return values, which may impact
|
687 | * memory use when working with very large query result sets. Consider using
|
688 | * {@link ArrayCursor#forEach}, {@link ArrayCursor#reduce} or
|
689 | * {@link ArrayCursor#flatMap} instead.
|
690 | *
|
691 | * See also:
|
692 | * [`Array.prototype.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
|
693 | *
|
694 | * @param R - Return type of the `callback` function.
|
695 | * @param callback - Function to execute on each element.
|
696 | *
|
697 | * @example
|
698 | * ```js
|
699 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
700 | * const squares = await cursor.map((currentValue) => {
|
701 | * return currentValue ** 2;
|
702 | * });
|
703 | * console.log(squares); // [1, 4, 9, 16, 25]
|
704 | * console.log(cursor.hasNext); // false
|
705 | * ```
|
706 | */
|
707 | map<R>(callback: (currentValue: T, index: number, self: this) => R): Promise<R[]>;
|
708 | /**
|
709 | * Depletes the cursor by applying the `callback` function to each item in
|
710 | * the cursor's remaining result list. Returns an array containing the
|
711 | * return values of `callback` for each item, flattened to a depth of 1.
|
712 | *
|
713 | * **Note**: If the result set spans multiple batches, any remaining batches
|
714 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
715 | * processing speed, this may result in the server discarding the cursor
|
716 | * before it is fully depleted.
|
717 | *
|
718 | * See also:
|
719 | * [`Array.prototype.flatMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap).
|
720 | *
|
721 | * @param R - Return type of the `callback` function.
|
722 | * @param callback - Function to execute on each element.
|
723 | *
|
724 | * @example
|
725 | * ```js
|
726 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
727 | * const squares = await cursor.flatMap((currentValue) => {
|
728 | * return [currentValue, currentValue ** 2];
|
729 | * });
|
730 | * console.log(squares); // [1, 1, 2, 4, 3, 9, 4, 16, 5, 25]
|
731 | * console.log(cursor.hasNext); // false
|
732 | * ```
|
733 | *
|
734 | * @example
|
735 | * ```js
|
736 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
737 | * const odds = await cursor.flatMap((currentValue) => {
|
738 | * if (currentValue % 2 === 0) {
|
739 | * return []; // empty array flattens into nothing
|
740 | * }
|
741 | * return currentValue; // or [currentValue]
|
742 | * });
|
743 | * console.logs(odds); // [1, 3, 5]
|
744 | * ```
|
745 | */
|
746 | flatMap<R>(callback: (currentValue: T, index: number, self: this) => R | R[]): Promise<R[]>;
|
747 | /**
|
748 | * Depletes the cursor by applying the `reducer` function to each item in
|
749 | * the cursor's remaining result list. Returns the return value of `reducer`
|
750 | * for the last item.
|
751 | *
|
752 | * **Note**: Most complex uses of the `reduce` method can be replaced with
|
753 | * simpler code using {@link ArrayCursor#forEach} or the `for await` syntax.
|
754 | *
|
755 | * **Note**: If the result set spans multiple batches, any remaining batches
|
756 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
757 | * processing speed, this may result in the server discarding the cursor
|
758 | * before it is fully depleted.
|
759 | *
|
760 | * See also:
|
761 | * [`Array.prototype.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
|
762 | *
|
763 | * @param R - Return type of the `reducer` function.
|
764 | * @param reducer - Function to execute on each element.
|
765 | * @param initialValue - Initial value of the `accumulator` value passed to
|
766 | * the `reducer` function.
|
767 | *
|
768 | * @example
|
769 | * ```js
|
770 | * function largestOfTwo(one, two) {
|
771 | * return Math.max(one, two);
|
772 | * }
|
773 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
774 | * const result = await cursor.reduce(largestOfTwo, 0);
|
775 | * console.log(result); // 5
|
776 | * console.log(cursor.hasNext); // false
|
777 | * const emptyResult = await cursor.reduce(largestOfTwo, 0);
|
778 | * console.log(emptyResult); // 0
|
779 | * ```
|
780 | *
|
781 | * @example
|
782 | * ```js
|
783 | * // BAD! NEEDLESSLY COMPLEX!
|
784 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
785 | * const result = await cursor.reduce((accumulator, currentValue) => {
|
786 | * if (currentValue % 2 === 0) {
|
787 | * accumulator.even.push(...currentValue);
|
788 | * } else {
|
789 | * accumulator.odd.push(...currentValue);
|
790 | * }
|
791 | * return accumulator;
|
792 | * }, { odd: [], even: [] });
|
793 | * console.log(result); // { odd: [1, 3, 5], even: [2, 4] }
|
794 | *
|
795 | * // GOOD! MUCH SIMPLER!
|
796 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
797 | * const odd = [];
|
798 | * const even = [];
|
799 | * for await (const currentValue of cursor) {
|
800 | * if (currentValue % 2 === 0) {
|
801 | * even.push(currentValue);
|
802 | * } else {
|
803 | * odd.push(currentValue);
|
804 | * }
|
805 | * }
|
806 | * console.log({ odd, even }); // { odd: [1, 3, 5], even: [2, 4] }
|
807 | * ```
|
808 | */
|
809 | reduce<R>(reducer: (accumulator: R, currentValue: T, index: number, self: this) => R, initialValue: R): Promise<R>;
|
810 | /**
|
811 | * Depletes the cursor by applying the `reducer` function to each item in
|
812 | * the cursor's remaining result list. Returns the return value of `reducer`
|
813 | * for the last item.
|
814 | *
|
815 | * **Note**: If the result set spans multiple batches, any remaining batches
|
816 | * will only be fetched on demand. Depending on the cursor's TTL and the
|
817 | * processing speed, this may result in the server discarding the cursor
|
818 | * before it is fully depleted.
|
819 | *
|
820 | * See also:
|
821 | * [`Array.prototype.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
|
822 | *
|
823 | * @param R - Return type of the `reducer` function.
|
824 | * @param reducer - Function to execute on each element.
|
825 | *
|
826 | * @example
|
827 | * ```js
|
828 | * function largestOfTwo(one, two) {
|
829 | * return Math.max(one, two);
|
830 | * }
|
831 | * const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
832 | * const result = await cursor.reduce(largestOfTwo);
|
833 | * console.log(result); // 5
|
834 | * console.log(cursor.hasNext); // false
|
835 | * const emptyResult = await cursor.reduce(largestOfTwo);
|
836 | * console.log(emptyResult); // undefined
|
837 | * ```
|
838 | */
|
839 | reduce<R>(reducer: (accumulator: T | R, currentValue: T, index: number, self: this) => R): Promise<R | undefined>;
|
840 | /**
|
841 | * Kills the cursor and frees up associated database resources.
|
842 | *
|
843 | * This method has no effect if all batches have already been fetched.
|
844 | *
|
845 | * @example
|
846 | * ```js
|
847 | * const cursor1 = await db.query(aql`FOR x IN 1..5 RETURN x`);
|
848 | * console.log(cursor1.hasMore); // false
|
849 | * await cursor1.kill(); // no effect
|
850 | *
|
851 | * const cursor2 = await db.query(
|
852 | * aql`FOR x IN 1..5 RETURN x`,
|
853 | * { batchSize: 2 }
|
854 | * );
|
855 | * console.log(cursor2.hasMore); // true
|
856 | * await cursor2.kill(); // cursor is depleted
|
857 | * ```
|
858 | */
|
859 | kill(): Promise<void>;
|
860 | }
|
861 | export {};
|
862 | //# sourceMappingURL=cursor.d.ts.map |
\ | No newline at end of file |