Skip to content

Commit

Permalink
fix: add refPath to SchemaTypeOptions class #11862
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jun 11, 2022
1 parent 53d2625 commit f4d7ad8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/options/SchemaTypeOptions.js
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion 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';
Expand Down Expand Up @@ -205,6 +205,9 @@ function gh11862() {
userType: string;
friend: Types.ObjectId;
}

const t: SchemaTypeOptions<mongoose.Types.ObjectId> = { type: 'ObjectId', refPath: 'userType' };

const userSchema = new Schema<IUser>({
userType: String,
friend: { type: 'ObjectId', refPath: 'userType' }
Expand Down

0 comments on commit f4d7ad8

Please sign in to comment.