From 506d12511a7a2ee803602be2e28384dcc7662f4c Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 11 Dec 2024 11:59:05 -0500 Subject: [PATCH] refactor: remove unnecessary _populate() re: code review comments --- lib/model.js | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/lib/model.js b/lib/model.js index 813c94bb81..899988c69e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -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;