Skip to content

Commit

Permalink
refactor(populate): remove discriminator logic from _getModelNames() …
Browse files Browse the repository at this point in the history
…re: #5124
  • Loading branch information
vkarpov15 committed Jun 8, 2020
1 parent 340ec60 commit 6416535
Showing 1 changed file with 2 additions and 40 deletions.
42 changes: 2 additions & 40 deletions lib/helpers/populate/getModelsMapForPopulate.js
Expand Up @@ -165,7 +165,6 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {

function _getModelNames(doc, schema, modelNameFromQuery) {
let modelNames;
let discriminatorKey;
let isRefPath = false;

if (schema && schema.caster) {
Expand All @@ -175,10 +174,6 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
schema = schema.$__schemaType;
}

if (!schema && model.discriminators) {
discriminatorKey = model.schema.discriminatorMapping.key;
}

const ref = schema && schema.options && schema.options.ref;
refPath = schema && schema.options && schema.options.refPath;
if (schema != null &&
Expand Down Expand Up @@ -212,52 +207,19 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {

isRefPath = true;
} else {
let modelForCurrentDoc = model;
let schemaForCurrentDoc;

if (!schema && discriminatorKey) {
modelForFindSchema = utils.getValue(discriminatorKey, doc);
if (modelForFindSchema) {
// `modelForFindSchema` is the discriminator value, so we might need
// find the discriminated model name
const discriminatorModel = getDiscriminatorByValue(model, modelForFindSchema);
if (discriminatorModel != null) {
modelForCurrentDoc = discriminatorModel;
} else {
try {
modelForCurrentDoc = model.db.model(modelForFindSchema);
} catch (error) {
return error;
}
}

schemaForCurrentDoc = modelForCurrentDoc.schema._getSchema(options.path);

if (schemaForCurrentDoc && schemaForCurrentDoc.caster) {
schemaForCurrentDoc = schemaForCurrentDoc.caster;
}
}
} else {
schemaForCurrentDoc = schema;
}

let ref;
let refPath;

if ((ref = get(schemaForCurrentDoc, 'options.ref')) != null) {
if ((ref = get(schema, 'options.ref')) != null) {
ref = handleRefFunction(ref, doc);
modelNames = [ref];
} else if ((refPath = get(schemaForCurrentDoc, 'options.refPath')) != null) {
} else if ((refPath = get(schema, 'options.refPath')) != null) {
isRefPath = true;
refPath = normalizeRefPath(refPath, doc, options.path);
modelNames = utils.getValue(refPath, doc);
if (Array.isArray(modelNames)) {
modelNames = utils.array.flatten(modelNames);
}
} else {
// We may have a discriminator, in which case we don't want to
// populate using the base model by default
modelNames = discriminatorKey ? null : [model.modelName];
}
}

Expand Down

0 comments on commit 6416535

Please sign in to comment.