Skip to content

Commit

Permalink
Merge pull request #13162 from hasezoey/addIndexCursor
Browse files Browse the repository at this point in the history
[6.x] add `cursor.eachAsync` index parameter
  • Loading branch information
vkarpov15 committed Mar 13, 2023
2 parents 779c140 + b717dd4 commit 0b2f6ed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 8 additions & 0 deletions test/types/querycursor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Schema, model, Document, Types } from 'mongoose';
import { expectType } from 'tsd';

const schema: Schema = new Schema({ name: { type: 'String' } });

Expand All @@ -10,3 +11,10 @@ const Test = model<ITest>('Test', schema);

Test.find().cursor().eachAsync(async(doc: ITest) => console.log(doc.name)).
then(() => console.log('Done!'));

Test.find().cursor().
eachAsync(async(doc: ITest, i) => {
expectType<any>(doc._id);
expectType<number>(i);
}).
then(() => console.log('Done!'));
8 changes: 4 additions & 4 deletions types/cursor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ declare module 'mongoose' {
* will wait for the promise to resolve before iterating on to the next one.
* Returns a promise that resolves when done.
*/
eachAsync(fn: (doc: DocType[]) => any, options: EachAsyncOptions & { batchSize: number }, callback: CallbackWithoutResult): void;
eachAsync(fn: (doc: DocType) => any, options: EachAsyncOptions, callback: CallbackWithoutResult): void;
eachAsync(fn: (doc: DocType[]) => any, options: EachAsyncOptions & { batchSize: number }): Promise<void>;
eachAsync(fn: (doc: DocType) => any, options?: EachAsyncOptions): Promise<void>;
eachAsync(fn: (doc: DocType[], index: number) => any, options: EachAsyncOptions & { batchSize: number }, callback: CallbackWithoutResult): void;
eachAsync(fn: (doc: DocType, index: number) => any, options: EachAsyncOptions, callback: CallbackWithoutResult): void;
eachAsync(fn: (doc: DocType[], index: number) => any, options: EachAsyncOptions & { batchSize: number }): Promise<void>;
eachAsync(fn: (doc: DocType, index: number) => any, options?: EachAsyncOptions): Promise<void>;

/**
* Registers a transform function which subsequently maps documents retrieved
Expand Down

0 comments on commit 0b2f6ed

Please sign in to comment.