UNPKG

2.08 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(): Promise<void>;
27
28 /**
29 * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will
30 * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even
31 * if the resultant data has already been retrieved by this cursor.
32 */
33 rewind(): this;
34
35 /**
36 * Execute `fn` for every document(s) in the cursor. If batchSize is provided
37 * `fn` will be executed for each batch of documents. If `fn` returns a promise,
38 * will wait for the promise to resolve before iterating on to the next one.
39 * Returns a promise that resolves when done.
40 */
41 eachAsync(fn: (doc: DocType[], i: number) => any, options: EachAsyncOptions & { batchSize: number }): Promise<void>;
42 eachAsync(fn: (doc: DocType, i: number) => any, options?: EachAsyncOptions): Promise<void>;
43
44 /**
45 * Registers a transform function which subsequently maps documents retrieved
46 * via the streams interface or `.next()`
47 */
48 map<ResultType>(fn: (res: DocType) => ResultType): Cursor<ResultType, Options>;
49
50 /**
51 * Get the next document from this cursor. Will return `null` when there are
52 * no documents left.
53 */
54 next(): Promise<DocType>;
55
56 options: Options;
57 }
58}
59
\No newline at end of file