///
import { Readable } from 'stream';
import { BSONSerializeOptions, Document, Long } from '../bson';
import type { MongoClient } from '../mongo_client';
import { TypedEventEmitter } from '../mongo_types';
import { ExecutionResult } from '../operations/execute_operation';
import { ReadConcern, ReadConcernLike } from '../read_concern';
import { ReadPreference, ReadPreferenceLike } from '../read_preference';
import type { Server } from '../sdam/server';
import { ClientSession } from '../sessions';
import { Callback, MongoDBNamespace } from '../utils';
/** @internal */
declare const kId: unique symbol;
/** @internal */
declare const kDocuments: unique symbol;
/** @internal */
declare const kServer: unique symbol;
/** @internal */
declare const kNamespace: unique symbol;
/** @internal */
declare const kClient: unique symbol;
/** @internal */
declare const kSession: unique symbol;
/** @internal */
declare const kOptions: unique symbol;
/** @internal */
declare const kTransform: unique symbol;
/** @internal */
declare const kInitialized: unique symbol;
/** @internal */
declare const kClosed: unique symbol;
/** @internal */
declare const kKilled: unique symbol;
/** @internal */
declare const kInit: unique symbol;
/** @public */
export declare const CURSOR_FLAGS: readonly ["tailable", "oplogReplay", "noCursorTimeout", "awaitData", "exhaust", "partial"];
/** @public
* @deprecated This interface is deprecated */
export interface CursorCloseOptions {
/** Bypass calling killCursors when closing the cursor. */
/** @deprecated the skipKillCursors option is deprecated */
skipKillCursors?: boolean;
}
/** @public */
export interface CursorStreamOptions {
/** A transformation method applied to each document emitted by the stream */
transform?(this: void, doc: Document): Document;
}
/** @public */
export declare type CursorFlag = typeof CURSOR_FLAGS[number];
/** @public */
export interface AbstractCursorOptions extends BSONSerializeOptions {
session?: ClientSession;
readPreference?: ReadPreferenceLike;
readConcern?: ReadConcernLike;
batchSize?: number;
maxTimeMS?: number;
/**
* Comment to apply to the operation.
*
* In server versions pre-4.4, 'comment' must be string. A server
* error will be thrown if any other type is provided.
*
* In server versions 4.4 and above, 'comment' can be any valid BSON type.
*/
comment?: unknown;
tailable?: boolean;
awaitData?: boolean;
noCursorTimeout?: boolean;
}
/** @internal */
export declare type InternalAbstractCursorOptions = Omit & {
readPreference: ReadPreference;
readConcern?: ReadConcern;
oplogReplay?: boolean;
exhaust?: boolean;
partial?: boolean;
};
/** @public */
export declare type AbstractCursorEvents = {
[AbstractCursor.CLOSE](): void;
};
/** @public */
export declare abstract class AbstractCursor extends TypedEventEmitter {
/** @internal */
[kId]?: Long;
/** @internal */
[kSession]: ClientSession;
/** @internal */
[kServer]?: Server;
/** @internal */
[kNamespace]: MongoDBNamespace;
/** @internal */
[kDocuments]: TSchema[];
/** @internal */
[kClient]: MongoClient;
/** @internal */
[kTransform]?: (doc: TSchema) => any;
/** @internal */
[kInitialized]: boolean;
/** @internal */
[kClosed]: boolean;
/** @internal */
[kKilled]: boolean;
/** @internal */
[kOptions]: InternalAbstractCursorOptions;
/** @event */
static readonly CLOSE: "close";
/** @internal */
constructor(client: MongoClient, namespace: MongoDBNamespace, options?: AbstractCursorOptions);
get id(): Long | undefined;
/** @internal */
get client(): MongoClient;
/** @internal */
get server(): Server | undefined;
get namespace(): MongoDBNamespace;
get readPreference(): ReadPreference;
get readConcern(): ReadConcern | undefined;
/** @internal */
get session(): ClientSession;
set session(clientSession: ClientSession);
/** @internal */
get cursorOptions(): InternalAbstractCursorOptions;
get closed(): boolean;
get killed(): boolean;
get loadBalanced(): boolean;
/** Returns current buffered documents length */
bufferedCount(): number;
/** Returns current buffered documents */
readBufferedDocuments(number?: number): TSchema[];
[Symbol.asyncIterator](): AsyncIterator;
stream(options?: CursorStreamOptions): Readable & AsyncIterable;
hasNext(): Promise;
hasNext(callback: Callback): void;
/** Get the next available document from the cursor, returns null if no more documents are available. */
next(): Promise;
next(callback: Callback): void;
next(callback?: Callback): Promise | void;
/**
* Try to get the next available document from the cursor or `null` if an empty batch is returned
*/
tryNext(): Promise;
tryNext(callback: Callback): void;
/**
* Iterates over all the documents for this cursor using the iterator, callback pattern.
*
* @param iterator - The iteration callback.
* @param callback - The end callback.
*/
forEach(iterator: (doc: TSchema) => boolean | void): Promise;
forEach(iterator: (doc: TSchema) => boolean | void, callback: Callback): void;
close(): Promise;
close(callback: Callback): void;
/**
* @deprecated options argument is deprecated
*/
close(options: CursorCloseOptions): Promise;
/**
* @deprecated options argument is deprecated
*/
close(options: CursorCloseOptions, callback: Callback): void;
/**
* Returns an array of documents. The caller is responsible for making sure that there
* is enough memory to store the results. Note that the array only contains partial
* results when this cursor had been previously accessed. In that case,
* cursor.rewind() can be used to reset the cursor.
*
* @param callback - The result callback.
*/
toArray(): Promise;
toArray(callback: Callback): void;
/**
* Add a cursor flag to the cursor
*
* @param flag - The flag to set, must be one of following ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'partial' -.
* @param value - The flag boolean value.
*/
addCursorFlag(flag: CursorFlag, value: boolean): this;
/**
* Map all documents using the provided function
* If there is a transform set on the cursor, that will be called first and the result passed to
* this function's transform.
*
* @remarks
* **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor,
* it **does not** return a new instance of a cursor. This means when calling map,
* you should always assign the result to a new variable in order to get a correctly typed cursor variable.
* Take note of the following example:
*
* @example
* ```typescript
* const cursor: FindCursor = coll.find();
* const mappedCursor: FindCursor = cursor.map(doc => Object.keys(doc).length);
* const keyCounts: number[] = await mappedCursor.toArray(); // cursor.toArray() still returns Document[]
* ```
* @param transform - The mapping transformation method.
*/
map(transform: (doc: TSchema) => T): AbstractCursor;
/**
* Set the ReadPreference for the cursor.
*
* @param readPreference - The new read preference for the cursor.
*/
withReadPreference(readPreference: ReadPreferenceLike): this;
/**
* Set the ReadPreference for the cursor.
*
* @param readPreference - The new read preference for the cursor.
*/
withReadConcern(readConcern: ReadConcernLike): this;
/**
* Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)
*
* @param value - Number of milliseconds to wait before aborting the query.
*/
maxTimeMS(value: number): this;
/**
* Set the batch size for the cursor.
*
* @param value - The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/find/|find command documentation}.
*/
batchSize(value: number): this;
/**
* Rewind this cursor to its uninitialized state. Any options that are present on the cursor will
* remain in effect. Iterating this cursor will cause new queries to be sent to the server, even
* if the resultant data has already been retrieved by this cursor.
*/
rewind(): void;
/**
* Returns a new uninitialized copy of this cursor, with options matching those that have been set on the current instance
*/
abstract clone(): AbstractCursor;
/** @internal */
abstract _initialize(session: ClientSession | undefined, callback: Callback): void;
/** @internal */
_getMore(batchSize: number, callback: Callback): void;
/**
* @internal
*
* This function is exposed for the unified test runner's createChangeStream
* operation. We cannot refactor to use the abstract _initialize method without
* a significant refactor.
*/
[kInit](callback: Callback): void;
}
/**
* @param cursor - the cursor on which to call `next`
* @param blocking - a boolean indicating whether or not the cursor should `block` until data
* is available. Generally, this flag is set to `false` because if the getMore returns no documents,
* the cursor has been exhausted. In certain scenarios (ChangeStreams, tailable await cursors and
* `tryNext`, for example) blocking is necessary because a getMore returning no documents does
* not indicate the end of the cursor.
* @param callback - callback to return the result to the caller
* @returns
*/
export declare function next(cursor: AbstractCursor, blocking: boolean, callback: Callback): void;
/** @internal */
export declare function assertUninitialized(cursor: AbstractCursor): void;
export {};
//# sourceMappingURL=abstract_cursor.d.ts.map