Skip to content

Commit 9508a84

Browse files
committed
add functions option to TypeScript types and test case for mongodb-js#112
1 parent f43c787 commit 9508a84

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

index.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { Schema } from 'mongoose';
22

3-
declare function mongooseAutoPopulate(schema: Schema): void;
3+
export type AutopopulatedFunctionNames = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndDelete' | 'findOneAndReplace' | 'save';
44

5-
export default mongooseAutoPopulate;
5+
interface AutopopulateOptions {
6+
// Only autopopulate on the given functions
7+
functions?: AutopopulatedFunctionNames[] | RegExp
8+
}
9+
10+
declare function mongooseAutoPopulate(schema: Schema, options?: AutopopulateOptions): void;
11+
12+
export default mongooseAutoPopulate;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
"acquit-markdown": "0.1.0",
2828
"eslint": "7.32.0",
2929
"mocha": "5.1.1",
30-
"mongoose": "9.0.0-rc1",
30+
"mongoose": "git@github.com:Automattic/mongoose.git#vkarpov15/mongodb-js/mongoose-autopopulate#112",
3131
"nyc": "11.7.3"
3232
},
3333
"peerDependencies": {
34-
"mongoose": "6.x || 7.x || 8.x || ^9.0.0-0"
34+
"mongoose": "6.x || 7.x || 8.x || 9.x"
3535
},
3636
"types": "./index.d.ts",
3737
"author": "Valeri Karpov <val@karpov.io>",

test/bugs.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,78 @@ describe('bug fixes', function() {
360360
section = await Section.findById(section);
361361
assert.equal(section.subdoc.subSection.name, 'foo');
362362
});
363+
364+
it('supports match as a function in virtuals (gh-112)', async function() {
365+
const teacherSchema = new Schema({
366+
schoolClass: {
367+
type: Schema.Types.ObjectId,
368+
ref: 'gh112_SchoolClass',
369+
autopopulate: { maxDepth: 2 }
370+
},
371+
schoolSubject: {
372+
type: Schema.Types.ObjectId,
373+
ref: 'gh112_SchoolSubject',
374+
autopopulate: { maxDepth: 2 }
375+
}
376+
});
377+
378+
const lectureSchema = new Schema({
379+
schoolClass: {
380+
type: Schema.Types.ObjectId,
381+
ref: 'gh112_SchoolClass',
382+
autopopulate: { maxDepth: 2 }
383+
},
384+
schoolSubject: {
385+
type: Schema.Types.ObjectId,
386+
ref: 'gh112_SchoolSubject',
387+
autopopulate: { maxDepth: 2 }
388+
}
389+
});
390+
391+
lectureSchema.virtual('assignedTeacher', {
392+
ref: 'gh112_Teacher',
393+
localField: 'schoolClass',
394+
foreignField: 'schoolClass',
395+
autopopulate: { maxDepth: 2 },
396+
match: (lecture) => ({
397+
schoolSubject: lecture.schoolSubject
398+
}),
399+
justOne: true
400+
});
401+
402+
teacherSchema.plugin(autopopulate);
403+
lectureSchema.plugin(autopopulate);
404+
405+
const Teacher = db.model('gh112_Teacher', teacherSchema);
406+
const Lecture = db.model('gh112_Lecture', lectureSchema);
407+
const SchoolClass = db.model('gh112_SchoolClass', new Schema({ name: String }));
408+
const SchoolSubject = db.model('gh112_SchoolSubject', new Schema({ name: String }));
409+
410+
// Clean up any leftover data from previous runs
411+
await Teacher.deleteMany({});
412+
await Lecture.deleteMany({});
413+
await SchoolClass.deleteMany({});
414+
await SchoolSubject.deleteMany({});
415+
416+
const schoolClass = await SchoolClass.create({ name: 'MTH-101' });
417+
const schoolSubject = await SchoolSubject.create({ name: 'Math' });
418+
419+
await Teacher.create({
420+
schoolClass: schoolClass._id,
421+
schoolSubject: schoolSubject._id
422+
});
423+
424+
await Lecture.create({
425+
schoolClass: schoolClass._id,
426+
schoolSubject: schoolSubject._id
427+
});
428+
429+
const lecture = await Lecture.findOne();
430+
assert.ok(lecture.assignedTeacher);
431+
// schoolClass/schoolSubject may be populated documents or ObjectIds depending on Mongoose version
432+
const teacherSchoolClassId = lecture.assignedTeacher.schoolClass._id || lecture.assignedTeacher.schoolClass;
433+
const teacherSchoolSubjectId = lecture.assignedTeacher.schoolSubject._id || lecture.assignedTeacher.schoolSubject;
434+
assert.equal(teacherSchoolClassId.toString(), schoolClass._id.toString());
435+
assert.equal(teacherSchoolSubjectId.toString(), schoolSubject._id.toString());
436+
});
363437
});

0 commit comments

Comments
 (0)