UNPKG

mingo

Version:

MongoDB query language for in-memory objects

97 lines (96 loc) 3.79 kB
import { CollationSpec, Options } from "./core"; import { Iterator, Source } from "./lazy"; import { Any, AnyObject, Predicate } from "./types"; type ArrayCallback<R, T> = (value: T, index: number, array: T[]) => R; /** * The `Cursor` class provides a mechanism for iterating over a collection of data * with support for filtering, projection, sorting, skipping, and limiting results. * It is designed to be chainable and supports lazy evaluation for efficient data processing. * * @template T - The type of the elements in the cursor. */ export declare class Cursor<T> { #private; /** * Creates an instance of the Cursor class. * * @param source - The source of data to be iterated over. * @param predicate - A function or condition to filter the data. * @param projection - An object specifying the fields to include or exclude in the result. * @param options - Optional settings to customize the behavior of the cursor. */ constructor(source: Source, predicate: Predicate<Any>, projection: AnyObject, options?: Options); /** Returns the iterator from running the query */ private fetch; /** Returns an iterator with the buffered data included */ private fetchAll; /** * Return remaining objects in the cursor as an array. This method exhausts the cursor * @returns {Array} */ all(): T[]; /** * Returns the number of objects return in the cursor. This method exhausts the cursor * @returns {Number} */ count(): number; /** * Returns a cursor that begins returning results only after passing or skipping a number of documents. * @param {Number} n the number of results to skip. * @return {Cursor} Returns the cursor, so you can chain this call. */ skip(n: number): Cursor<T>; /** * Limits the number of items returned by the cursor. * * @param n - The maximum number of items to return. * @returns The current cursor instance for chaining. */ limit(n: number): Cursor<T>; /** * Returns results ordered according to a sort specification. * @param {AnyObject} modifier an object of key and values specifying the sort order. 1 for ascending and -1 for descending * @return {Cursor} Returns the cursor, so you can chain this call. */ sort(modifier: AnyObject): Cursor<T>; /** * Sets the collation options for the cursor. * Collation allows users to specify language-specific rules for string comparison, * such as case sensitivity and accent marks. * * @param spec - The collation specification to apply. * @returns The current cursor instance for chaining. */ collation(spec: CollationSpec): Cursor<T>; /** * Retrieves the next item in the cursor. */ next(): T; /** * Determines if there are more elements available in the cursor. * * @returns {boolean} `true` if there are more elements to iterate over, otherwise `false`. */ hasNext(): boolean; /** * Transforms each element in the cursor using the provided callback function. * * @param f - A callback function. * @returns An array of transformed elements. */ map<R>(f: ArrayCallback<R, T>): R[]; /** * Applies the provided callback function to each element in the cursor. * * @param f - A callback function that is invoked for each element in the cursor. */ forEach(f: ArrayCallback<void, T>): void; /** * Returns an iterator for the cursor, allowing it to be used in `for...of` loops. * The iterator fetches all the results from the cursor. * * @returns {Iterator} An iterator over the fetched results. */ [Symbol.iterator](): Iterator; } export {};