Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(querycursor): correct cursor async iterator type with populate() support #14384

Merged
merged 5 commits into from Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -208,7 +208,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 @@ -286,7 +286,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