Class AbstractCursor<T, TRaw>Abstract

Overview

Represents some lazy, abstract iterable cursor over any arbitrary data, which may or may not be paginated.

Shouldn't be directly instantiated, but rather spawned via Collection.findAndRerank/Collection.find, or their Table alternatives.


Typing

For most intents and purposes, you may treat the cursor as if it is typed simply as Cursor<T>.

If you're using a projection, it is heavily recommended to provide an explicit type representing the type of the document after projection.

In full, the cursor is typed as FindCursor<T, TRaw>, where

  • T is the type of the mapped records, and
  • TRaw is the type of the raw records before any mapping.

If no mapping function is provided, T and TRaw will be the same type. Mapping is done using the FindCursor.map method.

See

  • CollectionFindCursor
  • CollectionFindAndRerankCursor
  • TableFindCursor

Type Parameters

Hierarchy (view full)

Constructors

Properties

_buffer: TRaw[] = []
_consumed: number = 0
_mapping?: ((doc) => T)

Type declaration

    • (doc): T
    • Parameters

      • doc: any

      Returns T

_nextPageState: QueryState<string> = ...
_options: WithTimeout<"generalMethodTimeoutMs">
_state: CursorState = 'idle'
bufferedCount: "ERROR: `.bufferedCount()` has been renamed to be simply `.buffered()`"

This temporary error-ing property exists for migration convenience, and will be removed in a future version.

Deprecated

  • .bufferedCount() has been renamed to simply be .buffered().
readBufferedDocuments: "ERROR: `.readBufferedDocuments()` has been renamed to be `.consumeBuffer()`"

This temporary error-ing property exists for migration convenience, and will be removed in a future version.

Deprecated

  • .readBufferedDocuments() has been renamed to be .consumeBuffer().

Accessors

Methods

  • An async iterator that lazily iterates over all records in the cursor.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return immediately.

    It will close the cursor when iteration is complete, even if it was broken early.

    Returns AsyncGenerator<T, void, void>

    Example

    for await (const doc of cursor) {
      console.log(doc);
    }
  • Consumes up to max records from the buffer, or all records if max is not provided.

    Note that this actually consumes the buffer; it doesn't just peek at it.

    Parameters

    • Optional max: number

      The maximum number of records to read from the buffer. If not provided, all records will be read.

    Returns TRaw[]

    The records read from the buffer.

  • Iterates over all records in the cursor, calling the provided consumer for each record.

    If the consumer returns false, iteration will stop.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return immediately.

    It will close the cursor when iteration is complete, even if it was stopped early.

    Parameters

    • consumer: ((doc) => boolean | Promise<boolean>) | ((doc) => void | Promise<void>)

      The consumer to call for each record.

    Returns Promise<void>

    A promise that resolves when iteration is complete.

    Remarks

    If you get an IDE error "Promise returned from forEach argument is ignored", it is a known WebStorm bug.

  • Tests if there is a next record in the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return false.

    Returns Promise<boolean>

    Whether or not there is a next record.

  • Fetches the next record from the cursor. Returns null if there are no more records to fetch.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return null.

    Returns Promise<null | T>

    The next record, or null if there are no more records.

  • Rewinds the cursor to its uninitialized state, clearing the buffer and any state.

    Any configuration set on the cursor will remain, but iteration will start from the beginning, sending new queries to the server, even if the resultant data was already fetched by this cursor.

    Returns void

  • Returns an array of all matching records in the cursor. The user should ensure that there is enough memory to store all records in the cursor.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return an empty array.

    Returns Promise<T[]>

    An array of all records in the cursor.