From f4d7ad883b2bc3279ca8d3e1c489d1500f78636e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 11 Jun 2022 12:58:10 -0400 Subject: [PATCH] fix: add refPath to SchemaTypeOptions class #11862 --- lib/options/SchemaTypeOptions.js | 13 +++++++++++++ test/types/populate.test.ts | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/options/SchemaTypeOptions.js b/lib/options/SchemaTypeOptions.js index b19f7b2e7ca..20a33213df2 100644 --- a/lib/options/SchemaTypeOptions.js +++ b/lib/options/SchemaTypeOptions.js @@ -119,6 +119,19 @@ Object.defineProperty(SchemaTypeOptions.prototype, 'default', opts); Object.defineProperty(SchemaTypeOptions.prototype, 'ref', opts); +/** + * The path in the document that `populate()` should use to find the model + * to use. + * + * @api public + * @property ref + * @memberOf SchemaTypeOptions + * @type Function|String + * @instance + */ + + Object.defineProperty(SchemaTypeOptions.prototype, 'refPath', opts); + /** * Whether to include or exclude this path by default when loading documents * using `find()`, `findOne()`, etc. diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index 122d5ec3168..6963e915342 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -1,4 +1,4 @@ -import { Schema, model, Document, PopulatedDoc, Types, HydratedDocument } from 'mongoose'; +import mongoose, { Schema, model, Document, PopulatedDoc, Types, HydratedDocument, SchemaTypeOptions } from 'mongoose'; // Use the mongodb ObjectId to make instanceof calls possible import { ObjectId } from 'mongodb'; import { expectAssignable, expectError, expectType } from 'tsd'; @@ -205,6 +205,9 @@ function gh11862() { userType: string; friend: Types.ObjectId; } + + const t: SchemaTypeOptions = { type: 'ObjectId', refPath: 'userType' }; + const userSchema = new Schema({ userType: String, friend: { type: 'ObjectId', refPath: 'userType' }