From 7a0b15ffb3999d82aadf550c43f3ed4a36710391 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 21 Jan 2018 13:28:22 -0800 Subject: [PATCH] style: fix lint --- lib/model.js | 168 ++++++++++++------------ lib/services/populate/getSchemaTypes.js | 4 +- 2 files changed, 85 insertions(+), 87 deletions(-) diff --git a/lib/model.js b/lib/model.js index 708b0725948..0d95e769914 100644 --- a/lib/model.js +++ b/lib/model.js @@ -3568,101 +3568,21 @@ function getModelsMapForPopulate(model, docs, options) { } if (Array.isArray(schema)) { - for (var i = 0; i < schema.length; ++i) { - var _modelNames = _getModelNames(schema[i]); + for (var j = 0; j < schema.length; ++j) { + var _modelNames = _getModelNames(doc, schema[j]); if (!_modelNames) { continue; } modelNames = (modelNames || []).concat(_modelNames); } } else { - modelNames = _getModelNames(schema); + modelNames = _getModelNames(doc, schema); if (!modelNames) { continue; } } - function _getModelNames(schema) { - var modelNames; - - if (schema && schema.caster) { - schema = schema.caster; - } - - if (!schema && model.discriminators) { - discriminatorKey = model.schema.discriminatorMapping.key; - } - - refPath = schema && schema.options && schema.options.refPath; - - if (refPath) { - modelNames = utils.getValue(refPath, doc); - isRefPathArray = Array.isArray(modelNames); - } else { - if (!modelNameFromQuery) { - var modelForCurrentDoc = model; - var schemaForCurrentDoc; - - if (!schema && discriminatorKey) { - modelForFindSchema = utils.getValue(discriminatorKey, doc); - - if (modelForFindSchema) { - 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; - } - var virtual = modelForCurrentDoc.schema._getVirtual(options.path); - - var ref; - if ((ref = get(schemaForCurrentDoc, 'options.ref')) != null) { - modelNames = [ref]; - } else if ((ref = get(virtual, 'options.ref')) != null) { - if (typeof ref === 'function') { - ref = ref.call(doc, doc); - } - - // When referencing nested arrays, the ref should be an Array - // of modelNames. - if (Array.isArray(ref)) { - modelNames = ref; - } else { - modelNames = [ref]; - } - - isVirtual = true; - } 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]; - } - } else { - modelNames = [modelNameFromQuery]; // query options - } - } - - if (!modelNames) { - return; - } - - if (!Array.isArray(modelNames)) { - modelNames = [modelNames]; - } - - return modelNames; - } - - virtual = model.schema._getVirtual(options.path); + var virtual = model.schema._getVirtual(options.path); var localField; if (virtual && virtual.options) { var virtualPrefix = virtual.$nestedSchemaPath ? @@ -3752,6 +3672,86 @@ function getModelsMapForPopulate(model, docs, options) { } } + function _getModelNames(doc, schema) { + var modelNames; + + if (schema && schema.caster) { + schema = schema.caster; + } + + if (!schema && model.discriminators) { + discriminatorKey = model.schema.discriminatorMapping.key; + } + + refPath = schema && schema.options && schema.options.refPath; + + if (refPath) { + modelNames = utils.getValue(refPath, doc); + isRefPathArray = Array.isArray(modelNames); + } else { + if (!modelNameFromQuery) { + var modelForCurrentDoc = model; + var schemaForCurrentDoc; + + if (!schema && discriminatorKey) { + modelForFindSchema = utils.getValue(discriminatorKey, doc); + + if (modelForFindSchema) { + 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; + } + var virtual = modelForCurrentDoc.schema._getVirtual(options.path); + + var ref; + if ((ref = get(schemaForCurrentDoc, 'options.ref')) != null) { + modelNames = [ref]; + } else if ((ref = get(virtual, 'options.ref')) != null) { + if (typeof ref === 'function') { + ref = ref.call(doc, doc); + } + + // When referencing nested arrays, the ref should be an Array + // of modelNames. + if (Array.isArray(ref)) { + modelNames = ref; + } else { + modelNames = [ref]; + } + + isVirtual = true; + } 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]; + } + } else { + modelNames = [modelNameFromQuery]; // query options + } + } + + if (!modelNames) { + return; + } + + if (!Array.isArray(modelNames)) { + modelNames = [modelNames]; + } + + return modelNames; + } + return map; } diff --git a/lib/services/populate/getSchemaTypes.js b/lib/services/populate/getSchemaTypes.js index d77bb9a74bb..820bad6c0b9 100644 --- a/lib/services/populate/getSchemaTypes.js +++ b/lib/services/populate/getSchemaTypes.js @@ -5,9 +5,7 @@ */ var Mixed = require('../../schema/mixed'); -var get = require('lodash.get'); var mpath = require('mpath'); -var utils = require('../../utils'); /*! * ignore @@ -112,4 +110,4 @@ module.exports = function getSchemaTypes(schema, doc, path) { } } return search(parts, schema); -} +};