MCPcopy Create free account
hub / github.com/Automattic/mongoose / addModelNamesToMap

Function addModelNamesToMap

lib/helpers/populate/getModelsMapForPopulate.js:606–698  ·  view source on GitHub ↗
(model, map, available, modelNames, options, data, ret, doc, schemaOptions, unpopulatedValue)

Source from the content-addressed store, hash-verified

604 */
605
606function addModelNamesToMap(model, map, available, modelNames, options, data, ret, doc, schemaOptions, unpopulatedValue) {
607 // `PopulateOptions#connection`: if the model is passed as a string, the
608 // connection matters because different connections have different models.
609 const connection = options.connection ?? model.db;
610
611 unpopulatedValue = unpopulatedValue === void 0 ? ret : unpopulatedValue;
612 if (Array.isArray(unpopulatedValue)) {
613 unpopulatedValue = utils.cloneArrays(unpopulatedValue);
614 }
615
616 if (modelNames == null) {
617 return;
618 }
619
620 const flatModelNames = utils.array.flatten(modelNames);
621 let k = flatModelNames.length;
622 while (k--) {
623 let modelName = flatModelNames[k];
624 if (modelName == null) {
625 continue;
626 }
627
628 let Model;
629 if (options.model && options.model[modelSymbol]) {
630 Model = options.model;
631 } else if (modelName[modelSymbol]) {
632 Model = modelName;
633 modelName = Model.modelName;
634 } else {
635 try {
636 Model = _getModelFromConn(connection, modelName);
637 } catch (err) {
638 if (ret !== void 0) {
639 throw err;
640 }
641 Model = null;
642 }
643 }
644
645 let ids = ret;
646
647 const modelNamesForRefPath = data.modelNamesInOrder || modelNames;
648 if (data.isRefPath && Array.isArray(ret) && ret.length === modelNamesForRefPath.length) {
649 ids = matchIdsToRefPaths(ret, modelNamesForRefPath, modelName);
650 }
651
652 if (!available[modelName]) {
653 const currentOptions = {
654 model: Model
655 };
656 if (data.isVirtual && get(data.virtual, 'options.options')) {
657 currentOptions.options = clone(data.virtual.options.options);
658 } else if (schemaOptions != null) {
659 currentOptions.options = Object.assign({}, schemaOptions);
660 }
661 utils.merge(currentOptions, options);
662
663 // Used internally for checking what model was used to populate this

Callers 2

_virtualPopulateFunction · 0.85

Calls 5

_getModelFromConnFunction · 0.85
matchIdsToRefPathsFunction · 0.85
addMethod · 0.80
getFunction · 0.50
cloneFunction · 0.50

Tested by

no test coverage detected