Cleanup
This commit is contained in:
35
node_modules/mongoose/lib/queryhelpers.js
generated
vendored
35
node_modules/mongoose/lib/queryhelpers.js
generated
vendored
@@ -31,6 +31,10 @@ exports.preparePopulationOptions = function preparePopulationOptions(query, opti
|
||||
forEach(makeLean(options.lean));
|
||||
}
|
||||
|
||||
pop.forEach(opts => {
|
||||
opts._localModel = query.model;
|
||||
});
|
||||
|
||||
return pop;
|
||||
};
|
||||
|
||||
@@ -71,6 +75,9 @@ exports.preparePopulationOptionsMQ = function preparePopulationOptionsMQ(query,
|
||||
pop.forEach(p => {
|
||||
p._queryProjection = projection;
|
||||
});
|
||||
pop.forEach(opts => {
|
||||
opts._localModel = query.model;
|
||||
});
|
||||
|
||||
return pop;
|
||||
};
|
||||
@@ -85,7 +92,7 @@ exports.preparePopulationOptionsMQ = function preparePopulationOptionsMQ(query,
|
||||
*
|
||||
* @return {Document}
|
||||
*/
|
||||
exports.createModel = function createModel(model, doc, fields, userProvidedFields) {
|
||||
exports.createModel = function createModel(model, doc, fields, userProvidedFields, options) {
|
||||
model.hooks.execPreSync('createModel', doc);
|
||||
const discriminatorMapping = model.schema ?
|
||||
model.schema.discriminatorMapping :
|
||||
@@ -97,18 +104,22 @@ exports.createModel = function createModel(model, doc, fields, userProvidedField
|
||||
|
||||
const value = doc[key];
|
||||
if (key && value && model.discriminators) {
|
||||
const discriminator = model.discriminators[value] || getDiscriminatorByValue(model, value);
|
||||
const discriminator = model.discriminators[value] || getDiscriminatorByValue(model.discriminators, value);
|
||||
if (discriminator) {
|
||||
const _fields = clone(userProvidedFields);
|
||||
exports.applyPaths(_fields, discriminator.schema);
|
||||
return new discriminator(undefined, _fields, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof options === 'undefined') {
|
||||
options = {};
|
||||
options.defaults = true;
|
||||
}
|
||||
return new model(undefined, fields, {
|
||||
skipId: true,
|
||||
isNew: false,
|
||||
willInit: true
|
||||
willInit: true,
|
||||
defaults: options.defaults
|
||||
});
|
||||
};
|
||||
|
||||
@@ -199,7 +210,11 @@ exports.applyPaths = function applyPaths(fields, schema) {
|
||||
schema.eachPath(function(path, type) {
|
||||
if (prefix) path = prefix + '.' + path;
|
||||
|
||||
const addedPath = analyzePath(path, type);
|
||||
let addedPath = analyzePath(path, type);
|
||||
// arrays
|
||||
if (addedPath == null && type.$isMongooseArray && !type.$isMongooseDocumentArray) {
|
||||
addedPath = analyzePath(path, type.caster);
|
||||
}
|
||||
if (addedPath != null) {
|
||||
addedPaths.push(addedPath);
|
||||
}
|
||||
@@ -261,7 +276,7 @@ exports.applyPaths = function applyPaths(fields, schema) {
|
||||
let cur = '';
|
||||
for (let i = 0; i < pieces.length; ++i) {
|
||||
cur += (cur.length === 0 ? '' : '.') + pieces[i];
|
||||
const projection = get(fields, cur, false);
|
||||
const projection = get(fields, cur, false) || get(fields, cur + '.$', false);
|
||||
if (projection && typeof projection !== 'object') {
|
||||
return;
|
||||
}
|
||||
@@ -282,6 +297,14 @@ exports.applyPaths = function applyPaths(fields, schema) {
|
||||
function makeLean(val) {
|
||||
return function(option) {
|
||||
option.options || (option.options = {});
|
||||
|
||||
if (val != null && Array.isArray(val.virtuals)) {
|
||||
val = Object.assign({}, val);
|
||||
val.virtuals = val.virtuals.
|
||||
filter(path => typeof path === 'string' && path.startsWith(option.path + '.')).
|
||||
map(path => path.slice(option.path.length + 1));
|
||||
}
|
||||
|
||||
option.options.lean = val;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user