Skip to content

Commit

Permalink
style: fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 21, 2018
1 parent 7677cf3 commit 7a0b15f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 87 deletions.
168 changes: 84 additions & 84 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 1 addition & 3 deletions lib/services/populate/getSchemaTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*/

var Mixed = require('../../schema/mixed');
var get = require('lodash.get');
var mpath = require('mpath');
var utils = require('../../utils');

/*!
* ignore
Expand Down Expand Up @@ -112,4 +110,4 @@ module.exports = function getSchemaTypes(schema, doc, path) {
}
}
return search(parts, schema);
}
};

0 comments on commit 7a0b15f

Please sign in to comment.