Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ArrayCursor<T>

The ArrayCursor type represents a cursor returned from a Database.query.

When using TypeScript, cursors can be cast to a specific item type in order to increase type safety.

See also BatchedArrayCursor.

example
const db = new Database();
const query = aql`FOR x IN 1..5 RETURN x`;
const result = await db.query(query) as ArrayCursor<number>;
example
const db = new Database();
const query = aql`FOR x IN 1..10 RETURN x`;
const cursor = await db.query(query);
for await (const value of cursor) {
  // Process each value asynchronously
  await processValue(value);
}

Type parameters

  • T = any

    Type to use for each item. Defaults to any.

Hierarchy

  • ArrayCursor

Index

Constructors

constructor

Accessors

batches

count

  • get count(): undefined | number
  • Total number of documents in the query result. Only available if the count option was used.

    Returns undefined | number

extra

  • Additional information about the cursor.

    Returns CursorExtras

hasNext

  • get hasNext(): boolean
  • Whether the cursor has more values. If set to false, the cursor has already been depleted and contains no more items.

    Returns boolean

Methods

[Symbol.asyncIterator]

  • [Symbol.asyncIterator](): AsyncGenerator<T, undefined, undefined>
  • Enables use with for await to deplete the cursor by asynchronously yielding every value in the cursor's remaining result set.

    Note: If the result set spans multiple batches, any remaining batches will only be fetched on demand. Depending on the cursor's TTL and the processing speed, this may result in the server discarding the cursor before it is fully depleted.

    example
    const cursor = await db.query(aql`
      FOR user IN users
      FILTER user.isActive
      RETURN user
    `);
    for await (const user of cursor) {
      console.log(user.email, user.isAdmin);
    }
    

    Returns AsyncGenerator<T, undefined, undefined>

all

  • all(): Promise<T[]>
  • Depletes the cursor, then returns an array containing all values in the cursor's remaining result list.

    example
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const result = await cursor.all(); // [1, 2, 3, 4, 5]
    console.log(cursor.hasNext); // false
    

    Returns Promise<T[]>

flatMap

  • flatMap<R>(callback: (currentValue: T, index: number, self: ArrayCursor<T>) => R | R[]): Promise<R[]>
  • Depletes the cursor by applying the callback function to each item in the cursor's remaining result list. Returns an array containing the return values of callback for each item, flattened to a depth of 1.

    Note: If the result set spans multiple batches, any remaining batches will only be fetched on demand. Depending on the cursor's TTL and the processing speed, this may result in the server discarding the cursor before it is fully depleted.

    See also: Array.prototype.flatMap.

    example
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const squares = await cursor.flatMap((currentValue) => {
      return [currentValue, currentValue ** 2];
    });
    console.log(squares); // [1, 1, 2, 4, 3, 9, 4, 16, 5, 25]
    console.log(cursor.hasNext); // false
    
    example
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const odds = await cursor.flatMap((currentValue) => {
      if (currentValue % 2 === 0) {
        return []; // empty array flattens into nothing
      }
      return currentValue; // or [currentValue]
    });
    console.logs(odds); // [1, 3, 5]
    

    Type parameters

    • R

      Return type of the callback function.

    Parameters

    • callback: (currentValue: T, index: number, self: ArrayCursor<T>) => R | R[]

      Function to execute on each element.

        • (currentValue: T, index: number, self: ArrayCursor<T>): R | R[]
        • Parameters

          Returns R | R[]

    Returns Promise<R[]>

forEach

  • forEach(callback: (currentValue: T, index: number, self: ArrayCursor<T>) => false | void): Promise<boolean>
  • Advances the cursor by applying the callback function to each item in the cursor's remaining result list until the cursor is depleted or callback returns the exact value false. Returns a promise that evalues to true unless the function returned false.

    Note: If the result set spans multiple batches, any remaining batches will only be fetched on demand. Depending on the cursor's TTL and the processing speed, this may result in the server discarding the cursor before it is fully depleted.

    See also: Array.prototype.forEach.

    example
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const result = await cursor.forEach((currentValue) => {
      console.log(currentValue);
    });
    console.log(result) // true
    console.log(cursor.hasNext); // false
    
    example
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const result = await cursor.forEach((currentValue) => {
      console.log(currentValue);
      return false; // stop after the first item
    });
    console.log(result); // false
    console.log(cursor.hasNext); // true
    

    Parameters

    • callback: (currentValue: T, index: number, self: ArrayCursor<T>) => false | void

      Function to execute on each element.

        • (currentValue: T, index: number, self: ArrayCursor<T>): false | void
        • Parameters

          Returns false | void

    Returns Promise<boolean>

kill

  • kill(): Promise<void>
  • Kills the cursor and frees up associated database resources.

    This method has no effect if all batches have already been fetched.

    example
    const cursor1 = await db.query(aql`FOR x IN 1..5 RETURN x`);
    console.log(cursor1.hasMore); // false
    await cursor1.kill(); // no effect
    
    const cursor2 = await db.query(
      aql`FOR x IN 1..5 RETURN x`,
      { batchSize: 2 }
    );
    console.log(cursor2.hasMore); // true
    await cursor2.kill(); // cursor is depleted
    

    Returns Promise<void>

map

  • map<R>(callback: (currentValue: T, index: number, self: ArrayCursor<T>) => R): Promise<R[]>
  • Depletes the cursor by applying the callback function to each item in the cursor's remaining result list. Returns an array containing the return values of callback for each item.

    Note: This creates an array of all return values, which may impact memory use when working with very large query result sets. Consider using ArrayCursor.forEach, ArrayCursor.reduce or ArrayCursor.flatMap instead.

    See also: Array.prototype.map.

    example
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const squares = await cursor.map((currentValue) => {
      return currentValue ** 2;
    });
    console.log(squares); // [1, 4, 9, 16, 25]
    console.log(cursor.hasNext); // false
    

    Type parameters

    • R

      Return type of the callback function.

    Parameters

    • callback: (currentValue: T, index: number, self: ArrayCursor<T>) => R

      Function to execute on each element.

        • (currentValue: T, index: number, self: ArrayCursor<T>): R
        • Parameters

          Returns R

    Returns Promise<R[]>

next

  • next(): Promise<undefined | T>
  • Advances the cursor and returns the next value in the cursor's remaining result list, or undefined if the cursor has been depleted.

    Note: If the result set spans multiple batches, any remaining batches will only be fetched on demand. Depending on the cursor's TTL and the processing speed, this may result in the server discarding the cursor before it is fully depleted.

    example
    const cursor = await db.query(aql`FOR x IN 1..3 RETURN x`);
    const one = await cursor.next(); // 1
    const two = await cursor.next(); // 2
    const three = await cursor.next(); // 3
    const empty = await cursor.next(); // undefined
    

    Returns Promise<undefined | T>

reduce

  • reduce<R>(reducer: (accumulator: R, currentValue: T, index: number, self: ArrayCursor<T>) => R, initialValue: R): Promise<R>
  • reduce<R>(reducer: (accumulator: T | R, currentValue: T, index: number, self: ArrayCursor<T>) => R): Promise<undefined | R>
  • Depletes the cursor by applying the reducer function to each item in the cursor's remaining result list. Returns the return value of reducer for the last item.

    Note: Most complex uses of the reduce method can be replaced with simpler code using ArrayCursor.forEach or the for await syntax.

    Note: If the result set spans multiple batches, any remaining batches will only be fetched on demand. Depending on the cursor's TTL and the processing speed, this may result in the server discarding the cursor before it is fully depleted.

    See also: Array.prototype.reduce.

    example
    function largestOfTwo(one, two) {
      return Math.max(one, two);
    }
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const result = await cursor.reduce(largestOfTwo, 0);
    console.log(result); // 5
    console.log(cursor.hasNext); // false
    const emptyResult = await cursor.reduce(largestOfTwo, 0);
    console.log(emptyResult); // 0
    
    example
    // BAD! NEEDLESSLY COMPLEX!
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const result = await cursor.reduce((accumulator, currentValue) => {
      if (currentValue % 2 === 0) {
        accumulator.even.push(...currentValue);
      } else {
        accumulator.odd.push(...currentValue);
      }
      return accumulator;
    }, { odd: [], even: [] });
    console.log(result); // { odd: [1, 3, 5], even: [2, 4] }
    
    // GOOD! MUCH SIMPLER!
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const odd = [];
    const even = [];
    for await (const currentValue of cursor) {
      if (currentValue % 2 === 0) {
        even.push(currentValue);
      } else {
        odd.push(currentValue);
      }
    }
    console.log({ odd, even }); // { odd: [1, 3, 5], even: [2, 4] }
    

    Type parameters

    • R

      Return type of the reducer function.

    Parameters

    • reducer: (accumulator: R, currentValue: T, index: number, self: ArrayCursor<T>) => R

      Function to execute on each element.

        • (accumulator: R, currentValue: T, index: number, self: ArrayCursor<T>): R
        • Parameters

          • accumulator: R
          • currentValue: T
          • index: number
          • self: ArrayCursor<T>

          Returns R

    • initialValue: R

      Initial value of the accumulator value passed to the reducer function.

    Returns Promise<R>

  • Depletes the cursor by applying the reducer function to each item in the cursor's remaining result list. Returns the return value of reducer for the last item.

    Note: If the result set spans multiple batches, any remaining batches will only be fetched on demand. Depending on the cursor's TTL and the processing speed, this may result in the server discarding the cursor before it is fully depleted.

    See also: Array.prototype.reduce.

    example
    function largestOfTwo(one, two) {
      return Math.max(one, two);
    }
    const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
    const result = await cursor.reduce(largestOfTwo);
    console.log(result); // 5
    console.log(cursor.hasNext); // false
    const emptyResult = await cursor.reduce(largestOfTwo);
    console.log(emptyResult); // undefined
    

    Type parameters

    • R

      Return type of the reducer function.

    Parameters

    • reducer: (accumulator: T | R, currentValue: T, index: number, self: ArrayCursor<T>) => R

      Function to execute on each element.

        • (accumulator: T | R, currentValue: T, index: number, self: ArrayCursor<T>): R
        • Parameters

          • accumulator: T | R
          • currentValue: T
          • index: number
          • self: ArrayCursor<T>

          Returns R

    Returns Promise<undefined | R>

Generated using TypeDoc