Skip to content

Commit

Permalink
refactor: remove unnecessary _populate() re: code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 11, 2024
1 parent 951a2cf commit 506d125
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4245,51 +4245,34 @@ Model.populate = async function populate(docs, paths) {
if (typeof paths === 'function' || typeof arguments[2] === 'function') {
throw new MongooseError('Model.populate() no longer accepts a callback');
}
const _this = this;
// normalized paths
paths = utils.populate(paths);

await _populate(_this, docs, paths);
return docs;
};

/**
* Populate helper
*
* @param {Model} model the model to use
* @param {Document|Array} docs Either a single document or array of documents to populate.
* @param {Object} paths
* @param {never} cache Unused
* @param {Function} [callback] Optional callback, executed upon completion. Receives `err` and the `doc(s)`.
* @return {Function}
* @api private
*/

async function _populate(model, docs, paths) {
if (paths.length === 0) {
return;
return docs;
}

// each path has its own query options and must be executed separately
const promises = [];
for (const path of paths) {
promises.push(populate(model, docs, path));
promises.push(_populatePath(this, docs, path));
}

await Promise.all(promises);
}

return docs;
};

/*!
* Populates `docs`
* Populates `docs` for a single `populateOptions` instance.
*/
const excludeIdReg = /\s?-_id\s?/;
const excludeIdRegGlobal = /\s?-_id\s?/g;

async function populate(model, docs, options) {
const populateOptions = options;
if (options.strictPopulate == null) {
if (options._localModel != null && options._localModel.schema._userProvidedOptions.strictPopulate != null) {
populateOptions.strictPopulate = options._localModel.schema._userProvidedOptions.strictPopulate;
} else if (options._localModel != null && model.base.options.strictPopulate != null) {
async function _populatePath(model, docs, populateOptions) {
if (populateOptions.strictPopulate == null) {
if (populateOptions._localModel != null && populateOptions._localModel.schema._userProvidedOptions.strictPopulate != null) {
populateOptions.strictPopulate = populateOptions._localModel.schema._userProvidedOptions.strictPopulate;
} else if (populateOptions._localModel != null && model.base.options.strictPopulate != null) {
populateOptions.strictPopulate = model.base.options.strictPopulate;
} else if (model.base.options.strictPopulate != null) {
populateOptions.strictPopulate = model.base.options.strictPopulate;
Expand Down

0 comments on commit 506d125

Please sign in to comment.