Skip to content

Commit 8d00f62

Browse files
authoredAug 23, 2021
Merge pull request #10587 from osmanakol/master
allow QueryOptions populate parameter type PopulateOptions
2 parents 6c36263 + 57e729b commit 8d00f62

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ declare module 'mongoose' {
10791079
omitUndefined?: boolean;
10801080
overwrite?: boolean;
10811081
overwriteDiscriminatorKey?: boolean;
1082-
populate?: string;
1082+
populate?: string | string[] | PopulateOptions | PopulateOptions[];
10831083
projection?: any;
10841084
/**
10851085
* if true, returns the raw result from the MongoDB driver

‎test/typescript/queries.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import { Schema, model, Document, Types, Query, Model, QueryWithHelpers } from 'mongoose';
1+
import { Schema, model, Document, Types, Query, Model, QueryWithHelpers, PopulatedDoc } from 'mongoose';
2+
import { ObjectId } from 'mongodb';
23

34
interface QueryHelpers {
45
_byName(this: QueryWithHelpers<any, ITest, QueryHelpers>, name: string): QueryWithHelpers<Array<ITest>, ITest, QueryHelpers>;
56
byName(name: string): QueryWithHelpers<Array<ITest>, ITest, QueryHelpers>;
67
}
78

9+
const childSchema: Schema = new Schema({ name: String });
10+
const ChildModel = model<Child>('Child', childSchema);
11+
812
const schema: Schema<ITest, Model<ITest, QueryHelpers>> = new Schema({
913
name: { type: 'String' },
1014
tags: [String],
15+
child: { type: 'ObjectId', ref: 'Child' },
1116
docs: [{ _id: 'ObjectId', id: Number, tags: [String] }],
1217
endDate: Date
1318
});
@@ -21,6 +26,9 @@ schema.query.byName = function(name: string): QueryWithHelpers<any, ITest> {
2126
return this._byName(name);
2227
};
2328

29+
interface Child {
30+
name: string;
31+
}
2432
interface ISubdoc extends Document {
2533
myId?: Types.ObjectId;
2634
id?: number;
@@ -31,13 +39,16 @@ interface ITest extends Document {
3139
name?: string;
3240
age?: number;
3341
parent?: Types.ObjectId;
42+
child?: PopulatedDoc<Child & Document<ObjectId>>,
3443
tags?: string[];
3544
docs?: ISubdoc[];
3645
endDate?: Date;
3746
}
3847

3948
const Test = model<ITest, Model<ITest, QueryHelpers>>('Test', schema);
4049

50+
Test.find({}, {}, {populate:{path:"child", model:ChildModel, match:true}}).exec().then((res: Array<ITest>) => console.log(res))
51+
4152
Test.find().byName('test').byName('test2').orFail().exec().then(console.log);
4253

4354
Test.count({ name: /Test/ }).exec().then((res: number) => console.log(res));

0 commit comments

Comments
 (0)
Please sign in to comment.