Skip to content

Commit

Permalink
refactor: remove unnecessary _this and add comment explaining opToThunk
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 12, 2025
1 parent 5ca86a8 commit a41b821
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const queryOptionMethods = new Set([
'wtimeout'
]);

// Map from operation name to the name of the function that executes the actual operation against MongoDB.
// Called a thunk for legacy reasons, "thunk" means function that takes exactly 1 param, a callback.
// Currently `_countDocuments()`, etc. are async functions that take no params.
const opToThunk = new Map([
['countDocuments', '_countDocuments'],
['distinct', '__distinct'],
Expand Down Expand Up @@ -2353,8 +2356,7 @@ Query.prototype._find = async function _find() {
}

const mongooseOptions = this._mongooseOptions;
const _this = this;
const userProvidedFields = _this._userProvidedFields || {};
const userProvidedFields = this._userProvidedFields || {};

applyGlobalMaxTimeMS(this.options, this.model.db.options, this.model.base.options);
applyGlobalDiskUse(this.options, this.model.db.options, this.model.base.options);
Expand Down Expand Up @@ -2391,17 +2393,17 @@ Query.prototype._find = async function _find() {
});
}
return mongooseOptions.lean ?
_completeManyLean(_this.model.schema, docs, null, completeManyOptions) :
_this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
_completeManyLean(this.model.schema, docs, null, completeManyOptions) :
this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
}

const pop = helpers.preparePopulationOptionsMQ(_this, mongooseOptions);
const pop = helpers.preparePopulationOptionsMQ(this, mongooseOptions);

if (mongooseOptions.lean) {
return _this.model.populate(docs, pop);
return this.model.populate(docs, pop);
}

docs = await _this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
docs = await this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
await this.model.populate(docs, pop);

return docs;
Expand Down

0 comments on commit a41b821

Please sign in to comment.