Skip to content

Commit

Permalink
fix(document): allow calling $model() with no args for TypeScript
Browse files Browse the repository at this point in the history
Fix #13878
  • Loading branch information
vkarpov15 committed Oct 10, 2023
1 parent 642abd1 commit 430f7ad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,40 +1060,45 @@ Model.prototype.$__deleteOne = function $__deleteOne(options, cb) {
};

/**
* Returns another Model instance.
* Returns the model instance used to create this document if no `name` specified.
* If `name` specified, returns the model with the given `name`.
*
* #### Example:
*
* const doc = new Tank;
* await doc.model('User').findById(id);
* const doc = new Tank({});
* doc.$model() === Tank; // true
* await doc.$model('User').findById(id);
*
* @param {String} name model name
* @method model
* @param {String} [name] model name
* @method $model
* @api public
* @return {Model}
*/

Model.prototype.model = function model(name) {
Model.prototype.$model = function $model(name) {
if (arguments.length === 0) {
return this.constructor;
}
return this[modelDbSymbol].model(name);
};

/**
* Returns another Model instance.
* Returns the model instance used to create this document if no `name` specified.
* If `name` specified, returns the model with the given `name`.
*
* #### Example:
*
* const doc = new Tank;
* await doc.model('User').findById(id);
* const doc = new Tank({});
* doc.$model() === Tank; // true
* await doc.$model('User').findById(id);
*
* @param {String} name model name
* @method $model
* @param {String} [name] model name
* @method model
* @api public
* @return {Model}
*/

Model.prototype.$model = function $model(name) {
return this[modelDbSymbol].model(name);
};
Model.prototype.model = Model.prototype.$model;

/**
* Returns a document with `_id` only if at least one document exists in the database that matches
Expand Down
8 changes: 8 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12521,6 +12521,14 @@ describe('document', function() {
assert.strictEqual(attachmentSchemaPreValidateCalls, 1);
});

it('returns constructor if using $model() with no args (gh-13878)', async function() {
const testSchema = new Schema({ name: String });
const Test = db.model('Test', testSchema);

const doc = new Test();
assert.strictEqual(doc.$model(), Test);
});

it('avoids creating separate subpaths entry for every element in array (gh-13874)', async function() {
const tradeSchema = new mongoose.Schema({ tradeId: Number, content: String });

Expand Down
10 changes: 10 additions & 0 deletions test/types/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ function gh12290() {
user.isDirectModified('name');
}

function gh13878() {
const schema = new Schema({
name: String,
age: Number
});
const User = model('User', schema);
const user = new User({ name: 'John', age: 30 });
expectType<typeof User>(user.$model());
}

function gh13094() {
type UserDocumentNever = HydratedDocument<{ name: string }, Record<string, never>>;

Expand Down
1 change: 1 addition & 0 deletions types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ declare module 'mongoose' {

/** Returns the model with the given name on this document's associated connection. */
$model<ModelType = Model<unknown>>(name: string): ModelType;
$model<ModelType = Model<DocType>>(): ModelType;

/**
* A string containing the current operation that Mongoose is executing
Expand Down

0 comments on commit 430f7ad

Please sign in to comment.