Skip to content

Commit

Permalink
allow QueryOptions populate parameter use PopulateOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
osmanakol committed Aug 22, 2021
1 parent 6c36263 commit 57e729b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ declare module 'mongoose' {
omitUndefined?: boolean;
overwrite?: boolean;
overwriteDiscriminatorKey?: boolean;
populate?: string;
populate?: string | string[] | PopulateOptions | PopulateOptions[];
projection?: any;
/**
* if true, returns the raw result from the MongoDB driver
Expand Down
13 changes: 12 additions & 1 deletion test/typescript/queries.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Schema, model, Document, Types, Query, Model, QueryWithHelpers } from 'mongoose';
import { Schema, model, Document, Types, Query, Model, QueryWithHelpers, PopulatedDoc } from 'mongoose';
import { ObjectId } from 'mongodb';

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

const childSchema: Schema = new Schema({ name: String });
const ChildModel = model<Child>('Child', childSchema);

const schema: Schema<ITest, Model<ITest, QueryHelpers>> = new Schema({
name: { type: 'String' },
tags: [String],
child: { type: 'ObjectId', ref: 'Child' },
docs: [{ _id: 'ObjectId', id: Number, tags: [String] }],
endDate: Date
});
Expand All @@ -21,6 +26,9 @@ schema.query.byName = function(name: string): QueryWithHelpers<any, ITest> {
return this._byName(name);
};

interface Child {
name: string;
}
interface ISubdoc extends Document {
myId?: Types.ObjectId;
id?: number;
Expand All @@ -31,13 +39,16 @@ interface ITest extends Document {
name?: string;
age?: number;
parent?: Types.ObjectId;
child?: PopulatedDoc<Child & Document<ObjectId>>,
tags?: string[];
docs?: ISubdoc[];
endDate?: Date;
}

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

Test.find({}, {}, {populate:{path:"child", model:ChildModel, match:true}}).exec().then((res: Array<ITest>) => console.log(res))

Test.find().byName('test').byName('test2').orFail().exec().then(console.log);

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

0 comments on commit 57e729b

Please sign in to comment.