Skip to content

Commit

Permalink
Merge pull request #14384 from Automattic/vkarpov15/gh-14374
Browse files Browse the repository at this point in the history
types(querycursor): correct cursor async iterator type with `populate()` support
  • Loading branch information
vkarpov15 committed Feb 27, 2024
2 parents 28855e1 + 12981b8 commit c02141b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 26 additions & 0 deletions test/types/querycursor.test.ts
Expand Up @@ -20,3 +20,29 @@ Test.find().cursor().
expectType<number>(i);
}).
then(() => console.log('Done!'));

async function gh14374() {
// `Parent` represents the object as it is stored in MongoDB
interface Parent {
child?: Types.ObjectId
name?: string
}
const ParentModel = model<Parent>(
'Parent',
new Schema({
child: { type: Schema.Types.ObjectId, ref: 'Child' },
name: String
})
);

interface Child {
name: string
}
const childSchema: Schema = new Schema({ name: String });

const cursor = ParentModel.find({}).populate<{ child: Child }>('child').cursor();
for await (const doc of cursor) {
expectType<Child>(doc.child);
}

}
4 changes: 2 additions & 2 deletions types/query.d.ts
Expand Up @@ -216,7 +216,7 @@ declare module 'mongoose' {
* A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
* This is equivalent to calling `.cursor()` with no arguments.
*/
[Symbol.asyncIterator](): AsyncIterableIterator<DocType>;
[Symbol.asyncIterator](): AsyncIterableIterator<Unpacked<ResultType>>;

/** Executes the query */
exec(): Promise<ResultType>;
Expand Down Expand Up @@ -294,7 +294,7 @@ declare module 'mongoose' {
* Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
* A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
*/
cursor(options?: QueryOptions<DocType>): Cursor<DocType, QueryOptions<DocType>>;
cursor(options?: QueryOptions<DocType>): Cursor<Unpacked<ResultType>, QueryOptions<DocType>>;

/**
* Declare and/or execute this query as a `deleteMany()` operation. Works like
Expand Down

0 comments on commit c02141b

Please sign in to comment.