UNPKG

2.09 kBTypeScriptView Raw
1declare module 'mongoose' {
2
3 import stream = require('stream');
4
5 type CursorFlag = 'tailable' | 'oplogReplay' | 'noCursorTimeout' | 'awaitData' | 'partial';
6
7 interface EachAsyncOptions {
8 parallel?: number;
9 batchSize?: number;
10 continueOnError?: boolean;
11 }
12
13 class Cursor<DocType = any, Options = never> extends stream.Readable {
14 [Symbol.asyncIterator](): AsyncIterableIterator<DocType>;
15
16 /**
17 * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html#addCursorFlag).
18 * Useful for setting the `noCursorTimeout` and `tailable` flags.
19 */
20 addCursorFlag(flag: CursorFlag, value: boolean): this;
21
22 /**
23 * Marks this cursor as closed. Will stop streaming and subsequent calls to
24 * `next()` will error.
25 */
26 close(callback: CallbackWithoutResult): void;
27 close(): Promise<void>;
28
29 /**
30 * Execute `fn` for every document(s) in the cursor. If batchSize is provided
31 * `fn` will be executed for each batch of documents. If `fn` returns a promise,
32 * will wait for the promise to resolve before iterating on to the next one.
33 * Returns a promise that resolves when done.
34 */
35 eachAsync(fn: (doc: DocType[]) => any, options: EachAsyncOptions & { batchSize: number }, callback: CallbackWithoutResult): void;
36 eachAsync(fn: (doc: DocType) => any, options: EachAsyncOptions, callback: CallbackWithoutResult): void;
37 eachAsync(fn: (doc: DocType[]) => any, options: EachAsyncOptions & { batchSize: number }): Promise<void>;
38 eachAsync(fn: (doc: DocType) => any, options?: EachAsyncOptions): Promise<void>;
39
40 /**
41 * Registers a transform function which subsequently maps documents retrieved
42 * via the streams interface or `.next()`
43 */
44 map<ResultType>(fn: (res: DocType) => ResultType): Cursor<ResultType, Options>;
45
46 /**
47 * Get the next document from this cursor. Will return `null` when there are
48 * no documents left.
49 */
50 next(callback: Callback<DocType | null>): void;
51 next(): Promise<DocType>;
52
53 options: Options;
54 }
55}
56
\No newline at end of file