This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -4,13 +4,17 @@ module.exports = arrayDepth;
function arrayDepth(arr) {
if (!Array.isArray(arr)) {
return { min: 0, max: 0 };
return { min: 0, max: 0, containsNonArrayItem: true };
}
if (arr.length === 0) {
return { min: 1, max: 1 };
return { min: 1, max: 1, containsNonArrayItem: false };
}
if (arr.length === 1 && !Array.isArray(arr[0])) {
return { min: 1, max: 1, containsNonArrayItem: false };
}
const res = arrayDepth(arr[0]);
for (let i = 1; i < arr.length; ++i) {
const _res = arrayDepth(arr[i]);
if (_res.min < res.min) {
@@ -19,6 +23,7 @@ function arrayDepth(arr) {
if (_res.max > res.max) {
res.max = _res.max;
}
res.containsNonArrayItem = res.containsNonArrayItem || _res.containsNonArrayItem;
}
res.min = res.min + 1;

View File

@@ -10,6 +10,7 @@ const getFunctionName = require('./getFunctionName');
const isBsonType = require('./isBsonType');
const isObject = require('./isObject');
const symbols = require('./symbols');
const utils = require('../utils');
/*!
@@ -41,6 +42,11 @@ function clone(obj, options, isArrayChild) {
if (options && options._skipSingleNestedGetters && obj.$isSingleNested) {
options = Object.assign({}, options, { getters: false });
}
if (utils.isPOJO(obj) && obj.$__ != null && obj._doc != null) {
return obj._doc;
}
if (options && options.json && typeof obj.toJSON === 'function') {
return obj.toJSON(options);
}
@@ -105,7 +111,7 @@ function cloneObject(obj, options, isArrayChild) {
const ret = {};
let hasKeys;
for (const k in obj) {
for (const k of Object.keys(obj)) {
if (specialProperties.has(k)) {
continue;
}

View File

@@ -4,6 +4,7 @@
* Module dependencies.
*/
const immediate = require('../immediate');
const promiseOrCallback = require('../promiseOrCallback');
/**
@@ -22,9 +23,22 @@ const promiseOrCallback = require('../promiseOrCallback');
module.exports = function eachAsync(next, fn, options, callback) {
const parallel = options.parallel || 1;
const batchSize = options.batchSize;
const enqueue = asyncQueue();
return promiseOrCallback(callback, cb => {
if (batchSize != null) {
if (typeof batchSize !== 'number') {
throw new TypeError('batchSize must be a number');
}
if (batchSize < 1) {
throw new TypeError('batchSize must be at least 1');
}
if (batchSize !== Math.floor(batchSize)) {
throw new TypeError('batchSize must be a positive integer');
}
}
iterate(cb);
});
@@ -32,6 +46,7 @@ module.exports = function eachAsync(next, fn, options, callback) {
let drained = false;
let handleResultsInProgress = 0;
let currentDocumentIndex = 0;
let documentsBatch = [];
let error = null;
for (let i = 0; i < parallel; ++i) {
@@ -56,6 +71,8 @@ module.exports = function eachAsync(next, fn, options, callback) {
drained = true;
if (handleResultsInProgress <= 0) {
finalCallback(null);
} else if (batchSize != null && documentsBatch.length) {
handleNextResult(documentsBatch, currentDocumentIndex++, handleNextResultCallBack);
}
return done();
}
@@ -64,10 +81,27 @@ module.exports = function eachAsync(next, fn, options, callback) {
// Kick off the subsequent `next()` before handling the result, but
// make sure we know that we still have a result to handle re: #8422
process.nextTick(() => done());
immediate(() => done());
handleNextResult(doc, currentDocumentIndex++, function(err) {
--handleResultsInProgress;
if (batchSize != null) {
documentsBatch.push(doc);
}
// If the current documents size is less than the provided patch size don't process the documents yet
if (batchSize != null && documentsBatch.length !== batchSize) {
setTimeout(() => enqueue(fetch), 0);
return;
}
const docsToProcess = batchSize != null ? documentsBatch : doc;
function handleNextResultCallBack(err) {
if (batchSize != null) {
handleResultsInProgress -= documentsBatch.length;
documentsBatch = [];
} else {
--handleResultsInProgress;
}
if (err != null) {
error = err;
return finalCallback(err);
@@ -77,7 +111,9 @@ module.exports = function eachAsync(next, fn, options, callback) {
}
setTimeout(() => enqueue(fetch), 0);
});
}
handleNextResult(docsToProcess, currentDocumentIndex++, handleNextResultCallBack);
});
}
}

View File

@@ -14,7 +14,7 @@ module.exports = function getConstructor(Constructor, value) {
if (Constructor.discriminators[value[discriminatorKey]]) {
Constructor = Constructor.discriminators[value[discriminatorKey]];
} else {
const constructorByValue = getDiscriminatorByValue(Constructor, value[discriminatorKey]);
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, value[discriminatorKey]);
if (constructorByValue) {
Constructor = constructorByValue;
}

View File

@@ -1,5 +1,7 @@
'use strict';
const areDiscriminatorValuesEqual = require('./areDiscriminatorValuesEqual');
/*!
* returns discriminator by discriminatorMapping.value
*
@@ -7,21 +9,19 @@
* @param {string} value
*/
module.exports = function getDiscriminatorByValue(model, value) {
let discriminator = null;
if (!model.discriminators) {
return discriminator;
module.exports = function getDiscriminatorByValue(discriminators, value) {
if (discriminators == null) {
return null;
}
for (const name in model.discriminators) {
const it = model.discriminators[name];
for (const name of Object.keys(discriminators)) {
const it = discriminators[name];
if (
it.schema &&
it.schema.discriminatorMapping &&
it.schema.discriminatorMapping.value == value
areDiscriminatorValuesEqual(it.schema.discriminatorMapping.value, value)
) {
discriminator = it;
break;
return it;
}
}
return discriminator;
return null;
};

View File

@@ -14,7 +14,7 @@ module.exports = function cleanModifiedSubpaths(doc, path, options) {
}
for (const modifiedPath of Object.keys(doc.$__.activePaths.states.modify)) {
if (skipDocArrays) {
const schemaType = doc.schema.path(modifiedPath);
const schemaType = doc.$__schema.path(modifiedPath);
if (schemaType && schemaType.$isMongooseDocumentArray) {
continue;
}

View File

@@ -2,6 +2,7 @@
const documentSchemaSymbol = require('../../helpers/symbols').documentSchemaSymbol;
const get = require('../../helpers/get');
const internalToObjectOptions = require('../../options').internalToObjectOptions;
const utils = require('../../utils');
let Document;
@@ -73,6 +74,13 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
value: prototype.schema
});
Object.defineProperty(nested, '$__schema', {
enumerable: false,
configurable: true,
writable: false,
value: prototype.schema
});
Object.defineProperty(nested, documentSchemaSymbol, {
enumerable: false,
configurable: true,
@@ -91,6 +99,17 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
}
});
Object.defineProperty(nested, '$__get', {
enumerable: false,
configurable: true,
writable: false,
value: function() {
return _this.get(path, null, {
virtuals: get(this, 'schema.options.toObject.virtuals', null)
});
}
});
Object.defineProperty(nested, 'toJSON', {
enumerable: false,
configurable: true,
@@ -124,6 +143,13 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
}
});
Object.defineProperty(nested, '$__parent', {
enumerable: false,
configurable: true,
writable: false,
value: this
});
compile(subprops, nested, path, options);
this.$__.getters[path] = nested;
}
@@ -131,8 +157,12 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
return this.$__.getters[path];
},
set: function(v) {
if (v instanceof Document) {
v = v.toObject({ transform: false });
if (v != null && v.$__isNested) {
// Convert top-level to POJO, but leave subdocs hydrated so `$set`
// can handle them. See gh-9293.
v = v.$__get();
} else if (v instanceof Document && !v.$__isNested) {
v = v.toObject(internalToObjectOptions);
}
const doc = this.$__[scopeSymbol] || this;
doc.$set(path, v);
@@ -158,13 +188,7 @@ function getOwnPropertyDescriptors(object) {
const result = {};
Object.getOwnPropertyNames(object).forEach(function(key) {
result[key] = Object.getOwnPropertyDescriptor(object, key);
// Assume these are schema paths, ignore them re: #5470
if (result[key].get) {
delete result[key];
return;
}
result[key].enumerable = [
const skip = [
'isNew',
'$__',
'errors',
@@ -175,6 +199,12 @@ function getOwnPropertyDescriptors(object) {
'__index',
'$isDocumentArrayElement'
].indexOf(key) === -1;
if (skip) {
return;
}
result[key] = Object.getOwnPropertyDescriptor(object, key);
result[key].enumerable = false;
});
return result;

View File

@@ -6,7 +6,30 @@
*/
module.exports = function get(obj, path, def) {
const parts = path.split('.');
let parts;
let isPathArray = false;
if (typeof path === 'string') {
if (path.indexOf('.') === -1) {
const _v = getProperty(obj, path);
if (_v == null) {
return def;
}
return _v;
}
parts = path.split('.');
} else {
isPathArray = true;
parts = path;
if (parts.length === 1) {
const _v = getProperty(obj, parts[0]);
if (_v == null) {
return def;
}
return _v;
}
}
let rest = path;
let cur = obj;
for (const part of parts) {
@@ -16,13 +39,15 @@ module.exports = function get(obj, path, def) {
// `lib/cast.js` depends on being able to get dotted paths in updates,
// like `{ $set: { 'a.b': 42 } }`
if (cur[rest] != null) {
if (!isPathArray && cur[rest] != null) {
return cur[rest];
}
cur = getProperty(cur, part);
rest = rest.substr(part.length + 1);
if (!isPathArray) {
rest = rest.substr(part.length + 1);
}
}
return cur == null ? def : cur;

View File

@@ -7,6 +7,8 @@
'use strict';
const nextTick = process.nextTick.bind(process);
module.exports = function immediate(cb) {
return process.nextTick(cb);
return nextTick(cb);
};

View File

@@ -3,8 +3,52 @@
const get = require('../get');
const utils = require('../../utils');
/**
* Given a Mongoose index definition (key + options objects) and a MongoDB server
* index definition, determine if the two indexes are equal.
*
* @param {Object} key the Mongoose index spec
* @param {Object} options the Mongoose index definition's options
* @param {Object} dbIndex the index in MongoDB as returned by `listIndexes()`
* @api private
*/
module.exports = function isIndexEqual(key, options, dbIndex) {
// If these options are different, need to rebuild the index
// Special case: text indexes have a special format in the db. For example,
// `{ name: 'text' }` becomes:
// {
// v: 2,
// key: { _fts: 'text', _ftsx: 1 },
// name: 'name_text',
// ns: 'test.tests',
// background: true,
// weights: { name: 1 },
// default_language: 'english',
// language_override: 'language',
// textIndexVersion: 3
// }
if (dbIndex.textIndexVersion != null) {
const weights = dbIndex.weights;
if (Object.keys(weights).length !== Object.keys(key).length) {
return false;
}
for (const prop of Object.keys(weights)) {
if (!(prop in key)) {
return false;
}
const weight = weights[prop];
if (weight !== get(options, 'weights.' + prop) && !(weight === 1 && get(options, 'weights.' + prop) == null)) {
return false;
}
}
if (options['default_language'] !== dbIndex['default_language']) {
return dbIndex['default_language'] === 'english' && options['default_language'] == null;
}
return true;
}
const optionKeys = [
'unique',
'partialFilterExpression',
@@ -17,6 +61,9 @@ module.exports = function isIndexEqual(key, options, dbIndex) {
continue;
}
if (key === 'collation') {
if (options[key] == null || dbIndex[key] == null) {
return options[key] == null && dbIndex[key] == null;
}
const definedKeys = Object.keys(options.collation);
const schemaCollation = options.collation;
const dbCollation = dbIndex.collation;
@@ -45,4 +92,4 @@ module.exports = function isIndexEqual(key, options, dbIndex) {
}
return true;
};
};

View File

@@ -75,9 +75,12 @@ function applyHooks(model, schema, options) {
if (hook.name === 'updateOne' || hook.name === 'deleteOne') {
return !!hook['document'];
}
if (hook.name === 'remove') {
if (hook.name === 'remove' || hook.name === 'init') {
return hook['document'] == null || !!hook['document'];
}
if (hook.query != null || hook.document != null) {
return hook.document !== false;
}
return true;
}).
filter(hook => {

View File

@@ -20,7 +20,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
const model = decideModelByObject(originalModel, op['insertOne']['document']);
const doc = new model(op['insertOne']['document']);
if (model.schema.options.timestamps != null) {
if (model.schema.options.timestamps) {
doc.initializeTimestamps();
}
if (options.session != null) {
@@ -37,8 +37,12 @@ module.exports = function castBulkWrite(originalModel, op, options) {
} else if (op['updateOne']) {
return (callback) => {
try {
if (!op['updateOne']['filter']) throw new Error('Must provide a filter object.');
if (!op['updateOne']['update']) throw new Error('Must provide an update object.');
if (!op['updateOne']['filter']) {
throw new Error('Must provide a filter object.');
}
if (!op['updateOne']['update']) {
throw new Error('Must provide an update object.');
}
const model = decideModelByObject(originalModel, op['updateOne']['filter']);
const schema = model.schema;
@@ -54,7 +58,6 @@ module.exports = function castBulkWrite(originalModel, op, options) {
applyTimestampsToChildren(now, op['updateOne']['update'], model.schema);
if (op['updateOne'].setDefaultsOnInsert) {
setDefaultsOnInsert(op['updateOne']['filter'], model.schema, op['updateOne']['update'], {
setDefaultsOnInsert: true,
@@ -71,8 +74,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
strict: strict,
overwrite: false,
upsert: op['updateOne'].upsert
});
}, model, op['updateOne']['filter']);
} catch (error) {
return callback(error, null);
}
@@ -82,8 +84,12 @@ module.exports = function castBulkWrite(originalModel, op, options) {
} else if (op['updateMany']) {
return (callback) => {
try {
if (!op['updateMany']['filter']) throw new Error('Must provide a filter object.');
if (!op['updateMany']['update']) throw new Error('Must provide an update object.');
if (!op['updateMany']['filter']) {
throw new Error('Must provide a filter object.');
}
if (!op['updateMany']['update']) {
throw new Error('Must provide an update object.');
}
const model = decideModelByObject(originalModel, op['updateMany']['filter']);
const schema = model.schema;
@@ -115,7 +121,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
strict: strict,
overwrite: false,
upsert: op['updateMany'].upsert
});
}, model, op['updateMany']['filter']);
} catch (error) {
return callback(error, null);
@@ -141,7 +147,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
// set `skipId`, otherwise we get "_id field cannot be changed"
const doc = new model(op['replaceOne']['replacement'], strict, true);
if (model.schema.options.timestamps != null) {
if (model.schema.options.timestamps) {
doc.initializeTimestamps();
}
if (options.session != null) {
@@ -153,6 +159,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
if (error) {
return callback(error, null);
}
op['replaceOne']['replacement'] = op['replaceOne']['replacement'].toBSON();
callback(null);
});
};
@@ -211,7 +218,7 @@ function _addDiscriminatorToObject(schema, obj) {
function decideModelByObject(model, object) {
const discriminatorKey = model.schema.options.discriminatorKey;
if (object != null && object.hasOwnProperty(discriminatorKey)) {
model = getDiscriminatorByValue(model, object[discriminatorKey]) || model;
model = getDiscriminatorByValue(model.discriminators, object[discriminatorKey]) || model;
}
return model;
}
}

View File

@@ -1,5 +1,6 @@
'use strict';
const Mixed = require('../../schema/mixed');
const defineKey = require('../document/compile').defineKey;
const get = require('../get');
const utils = require('../../utils');
@@ -62,7 +63,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
}
let value = name;
if (typeof tiedValue == 'string' && tiedValue.length) {
if ((typeof tiedValue === 'string' && tiedValue.length) || tiedValue != null) {
value = tiedValue;
}
@@ -84,22 +85,25 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
for (const path of baseSchemaPaths) {
if (schema.nested[path]) {
conflictingPaths.push(path);
continue;
}
if (path.indexOf('.') === -1) {
continue;
}
const sp = path.split('.');
const sp = path.split('.').slice(0, -1);
let cur = '';
for (const piece of sp) {
cur += (cur.length ? '.' : '') + piece;
if (schema.paths[cur] || schema.singleNestedPaths[cur]) {
if (schema.paths[cur] instanceof Mixed ||
schema.singleNestedPaths[cur] instanceof Mixed) {
conflictingPaths.push(path);
}
}
}
utils.merge(schema, baseSchema, {
isDiscriminatorSchemaMerge: true,
omit: { discriminators: true, base: true },
omitNested: conflictingPaths.reduce((cur, path) => {
cur['tree.' + path] = true;
@@ -122,17 +126,17 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
default: value,
select: true,
set: function(newName) {
if (newName === value) {
if (newName === value || (Array.isArray(value) && utils.deepEqual(newName, value))) {
return value;
}
throw new Error('Can\'t set discriminator key "' + key + '"');
},
$skipDiscriminatorCheck: true
};
obj[key][schema.options.typeKey] = existingPath ?
existingPath.instance :
String;
obj[key][schema.options.typeKey] = existingPath ? existingPath.options[schema.options.typeKey] : String;
schema.add(obj);
schema.discriminatorMapping = { key: key, value: value, isRoot: false };
if (baseSchema.options.collection) {
@@ -149,11 +153,17 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
for (const _key of keys) {
if (!CUSTOMIZABLE_DISCRIMINATOR_OPTIONS[_key]) {
// Special case: compiling a model sets `pluralization = true` by default. Avoid throwing an error
// for that case. See gh-9238
if (_key === 'pluralization' && schema.options[_key] == true && baseSchema.options[_key] == null) {
continue;
}
if (!utils.deepEqual(schema.options[_key], baseSchema.options[_key])) {
throw new Error('Can\'t customize discriminator option ' + _key +
' (can only modify ' +
Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ') +
')');
' (can only modify ' +
Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ') +
')');
}
}
}

View File

@@ -69,7 +69,12 @@ function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, re
if (recursed) {
if (doc) {
if (sorting) {
newOrder[resultOrder[sid]] = doc;
const _resultOrder = resultOrder[sid];
if (Array.isArray(_resultOrder) && Array.isArray(doc) && _resultOrder.length === doc.length) {
newOrder.push(doc);
} else {
newOrder[_resultOrder] = doc;
}
} else {
newOrder.push(doc);
}

View File

@@ -5,24 +5,27 @@ const assignRawDocsToIdStructure = require('./assignRawDocsToIdStructure');
const get = require('../get');
const getVirtual = require('./getVirtual');
const leanPopulateMap = require('./leanPopulateMap');
const lookupLocalFields = require('./lookupLocalFields');
const mpath = require('mpath');
const sift = require('sift').default;
const utils = require('../../utils');
module.exports = function assignVals(o) {
// Options that aren't explicitly listed in `populateOptions`
const userOptions = get(o, 'allOptions.options.options');
const userOptions = Object.assign({}, get(o, 'allOptions.options.options'), get(o, 'allOptions.options'));
// `o.options` contains options explicitly listed in `populateOptions`, like
// `match` and `limit`.
const populateOptions = Object.assign({}, o.options, userOptions, {
justOne: o.justOne
});
populateOptions.$nullIfNotFound = o.isVirtual;
const populatedModel = o.populatedModel;
const originalIds = [].concat(o.rawIds);
// replace the original ids in our intermediate _ids structure
// with the documents found by query
o.allIds = [].concat(o.allIds);
assignRawDocsToIdStructure(o.rawIds, o.rawDocs, o.rawOrder, populateOptions);
// now update the original documents being populated using the
@@ -31,6 +34,7 @@ module.exports = function assignVals(o) {
const rawIds = o.rawIds;
const options = o.options;
const count = o.count && o.isVirtual;
let i;
function setValue(val) {
if (count) {
@@ -39,17 +43,42 @@ module.exports = function assignVals(o) {
if (val instanceof SkipPopulateValue) {
return val.val;
}
if (o.justOne === true && Array.isArray(val)) {
return valueFilter(val[0], options, populateOptions);
} else if (o.justOne === false && !Array.isArray(val)) {
return valueFilter([val], options, populateOptions);
if (val === void 0) {
return val;
}
return valueFilter(val, options, populateOptions);
const _allIds = o.allIds[i];
if (o.justOne === true && Array.isArray(val)) {
// Might be an embedded discriminator (re: gh-9244) with multiple models, so make sure to pick the right
// model before assigning.
const ret = [];
for (const doc of val) {
const _docPopulatedModel = leanPopulateMap.get(doc);
if (_docPopulatedModel == null || _docPopulatedModel === populatedModel) {
ret.push(doc);
}
}
// Since we don't want to have to create a new mongoosearray, make sure to
// modify the array in place
while (val.length > ret.length) {
Array.prototype.pop.apply(val, []);
}
for (let i = 0; i < ret.length; ++i) {
val[i] = ret[i];
}
return valueFilter(val[0], options, populateOptions, _allIds);
} else if (o.justOne === false && !Array.isArray(val)) {
return valueFilter([val], options, populateOptions, _allIds);
}
return valueFilter(val, options, populateOptions, _allIds);
}
for (let i = 0; i < docs.length; ++i) {
const existingVal = utils.getValue(o.path, docs[i]);
if (existingVal == null && !getVirtual(o.originalModel.schema, o.path)) {
for (i = 0; i < docs.length; ++i) {
const _path = o.path.endsWith('.$*') ? o.path.slice(0, -3) : o.path;
const existingVal = mpath.get(_path, docs[i], lookupLocalFields);
if (existingVal == null && !getVirtual(o.originalModel.schema, _path)) {
continue;
}
@@ -58,8 +87,8 @@ module.exports = function assignVals(o) {
valueToSet = numDocs(rawIds[i]);
} else if (Array.isArray(o.match)) {
valueToSet = Array.isArray(rawIds[i]) ?
sift(o.match[i], rawIds[i]) :
sift(o.match[i], [rawIds[i]])[0];
rawIds[i].filter(sift(o.match[i])) :
[rawIds[i]].filter(sift(o.match[i]))[0];
} else {
valueToSet = rawIds[i];
}
@@ -73,7 +102,7 @@ module.exports = function assignVals(o) {
utils.isPOJO(existingVal);
// If we pass the first check, also make sure the local field's schematype
// is map (re: gh-6460)
isMap = isMap && get(originalSchema._getSchema(o.path), '$isSchemaMap');
isMap = isMap && get(originalSchema._getSchema(_path), '$isSchemaMap');
if (!o.isVirtual && isMap) {
const _keys = existingVal instanceof Map ?
Array.from(existingVal.keys()) :
@@ -84,15 +113,25 @@ module.exports = function assignVals(o) {
}, new Map());
}
if (isDoc && Array.isArray(valueToSet)) {
for (const val of valueToSet) {
if (val != null && val.$__ != null) {
val.$__.parent = docs[i];
}
}
} else if (isDoc && valueToSet != null && valueToSet.$__ != null) {
valueToSet.$__.parent = docs[i];
}
if (o.isVirtual && isDoc) {
docs[i].populated(o.path, o.justOne ? originalIds[0] : originalIds, o.allOptions);
docs[i].populated(_path, o.justOne ? originalIds[0] : originalIds, o.allOptions);
// If virtual populate and doc is already init-ed, need to walk through
// the actual doc to set rather than setting `_doc` directly
mpath.set(o.path, valueToSet, docs[i], setValue);
mpath.set(_path, valueToSet, docs[i], setValue);
continue;
}
const parts = o.path.split('.');
const parts = _path.split('.');
let cur = docs[i];
const curPath = parts[0];
for (let j = 0; j < parts.length - 1; ++j) {
@@ -102,13 +141,17 @@ module.exports = function assignVals(o) {
break;
}
if (parts[j] === '$*') {
break;
}
if (cur[parts[j]] == null) {
// If nothing to set, avoid creating an unnecessary array. Otherwise
// we'll end up with a single doc in the array with only defaults.
// See gh-8342, gh-8455
const schematype = originalSchema._getSchema(curPath);
if (valueToSet == null && schematype != null && schematype.$isMongooseArray) {
return;
break;
}
cur[parts[j]] = {};
}
@@ -116,17 +159,17 @@ module.exports = function assignVals(o) {
// If the property in MongoDB is a primitive, we won't be able to populate
// the nested path, so skip it. See gh-7545
if (typeof cur !== 'object') {
return;
break;
}
}
if (docs[i].$__) {
docs[i].populated(o.path, o.allIds[i], o.allOptions);
docs[i].populated(_path, o.allIds[i], o.allOptions);
}
// If lean, need to check that each individual virtual respects
// `justOne`, because you may have a populated virtual with `justOne`
// underneath an array. See gh-6867
utils.setValue(o.path, valueToSet, docs[i], setValue, false);
mpath.set(_path, valueToSet, docs[i], lookupLocalFields, setValue, false);
}
};
@@ -160,15 +203,20 @@ function numDocs(v) {
* that population mapping can occur.
*/
function valueFilter(val, assignmentOpts, populateOptions) {
function valueFilter(val, assignmentOpts, populateOptions, allIds) {
const userSpecifiedTransform = typeof populateOptions.transform === 'function';
const transform = userSpecifiedTransform ? populateOptions.transform : noop;
if (Array.isArray(val)) {
// find logic
const ret = [];
const numValues = val.length;
for (let i = 0; i < numValues; ++i) {
const subdoc = val[i];
if (!isPopulatedObject(subdoc) && (!populateOptions.retainNullValues || subdoc != null)) {
let subdoc = val[i];
const _allIds = Array.isArray(allIds) ? allIds[i] : allIds;
if (!isPopulatedObject(subdoc) && (!populateOptions.retainNullValues || subdoc != null) && !userSpecifiedTransform) {
continue;
} else if (userSpecifiedTransform) {
subdoc = transform(isPopulatedObject(subdoc) ? subdoc : null, _allIds);
}
maybeRemoveId(subdoc, assignmentOpts);
ret.push(subdoc);
@@ -190,22 +238,19 @@ function valueFilter(val, assignmentOpts, populateOptions) {
}
// findOne
if (isPopulatedObject(val)) {
if (isPopulatedObject(val) || utils.isPOJO(val)) {
maybeRemoveId(val, assignmentOpts);
return val;
return transform(val, allIds);
}
if (val instanceof Map) {
return val;
}
if (populateOptions.justOne === true) {
return (val == null ? val : null);
}
if (populateOptions.justOne === false) {
return [];
}
return val;
return val == null ? transform(val, allIds) : transform(null, allIds);
}
/*!
@@ -213,7 +258,7 @@ function valueFilter(val, assignmentOpts, populateOptions) {
*/
function maybeRemoveId(subdoc, assignmentOpts) {
if (assignmentOpts.excludeId) {
if (subdoc != null && assignmentOpts.excludeId) {
if (typeof subdoc.$__setValue === 'function') {
delete subdoc._doc._id;
} else {
@@ -236,4 +281,8 @@ function isPopulatedObject(obj) {
obj.$isMongooseMap ||
obj.$__ != null ||
leanPopulateMap.has(obj);
}
function noop(v) {
return v;
}

View File

@@ -5,8 +5,11 @@ const SkipPopulateValue = require('./SkipPopulateValue');
const get = require('../get');
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
const isPathExcluded = require('../projection/isPathExcluded');
const getConstructorName = require('../getConstructorName');
const getSchemaTypes = require('./getSchemaTypes');
const getVirtual = require('./getVirtual');
const lookupLocalFields = require('./lookupLocalFields');
const mpath = require('mpath');
const normalizeRefPath = require('./normalizeRefPath');
const util = require('util');
const utils = require('../../utils');
@@ -28,7 +31,6 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
let currentOptions;
let modelNames;
let modelName;
let modelForFindSchema;
const originalModel = options.model;
let isVirtual = false;
@@ -40,7 +42,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
for (i = 0; i < len; i++) {
doc = docs[i];
let justOne = null;
schema = getSchemaTypes(modelSchema, doc, options.path);
// Special case: populating a path that's a DocumentArray unless
// there's an explicit `ref` or `refPath` re: gh-8946
@@ -52,7 +54,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
}
// Populating a nested path should always be a no-op re: #9073.
// People shouldn't do this, but apparently they do.
if (modelSchema.nested[options.path]) {
if (options._localModel != null && options._localModel.schema.nested[options.path]) {
continue;
}
const isUnderneathDocArray = schema && schema.$isUnderneathDocArray;
@@ -64,6 +66,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
modelNames = null;
let isRefPath = !!_firstWithRefPath;
let normalizedRefPath = _firstWithRefPath ? get(_firstWithRefPath, 'options.refPath', null) : null;
let schemaOptions = null;
if (Array.isArray(schema)) {
const schemasArray = schema;
@@ -76,6 +79,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
isRefPath = isRefPath || res.isRefPath;
normalizedRefPath = normalizeRefPath(normalizedRefPath, doc, options.path) ||
res.refPath;
justOne = res.justOne;
} catch (error) {
return error;
}
@@ -99,6 +103,8 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
modelNames = res.modelNames;
isRefPath = res.isRefPath;
normalizedRefPath = res.refPath;
justOne = res.justOne;
schemaOptions = get(schema, 'options.populate', null);
} catch (error) {
return error;
}
@@ -118,6 +124,8 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
_virtualRes.nestedSchemaPath + '.' : '';
if (typeof virtual.options.localField === 'function') {
localField = virtualPrefix + virtual.options.localField.call(doc, doc);
} else if (Array.isArray(virtual.options.localField)) {
localField = virtual.options.localField.map(field => virtualPrefix + field);
} else {
localField = virtualPrefix + virtual.options.localField;
}
@@ -142,7 +150,6 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
// `justOne = null` means we don't know from the schema whether the end
// result should be an array or a single doc. This can result from
// populating a POJO using `Model.populate()`
let justOne = null;
if ('justOne' in options && options.justOne !== void 0) {
justOne = options.justOne;
} else if (virtual && virtual.options && virtual.options.refPath) {
@@ -166,9 +173,11 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
}
} else if (schema && !schema[schemaMixedSymbol]) {
// Skip Mixed types because we explicitly don't do casting on those.
justOne = Array.isArray(schema) ?
schema.every(schema => !schema.$isMongooseArray) :
!schema.$isMongooseArray;
if (options.path.endsWith('.' + schema.path)) {
justOne = Array.isArray(schema) ?
schema.every(schema => !schema.$isMongooseArray) :
!schema.$isMongooseArray;
}
}
if (!modelNames) {
@@ -189,6 +198,27 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
foreignField = foreignField.call(doc);
}
let match = get(options, 'match', null) ||
get(currentOptions, 'match', null) ||
get(options, 'virtual.options.match', null) ||
get(options, 'virtual.options.options.match', null);
let hasMatchFunction = typeof match === 'function';
if (hasMatchFunction) {
match = match.call(doc, doc);
}
if (Array.isArray(localField) && Array.isArray(foreignField) && localField.length === foreignField.length) {
match = Object.assign({}, match);
for (let i = 1; i < localField.length; ++i) {
match[foreignField[i]] = convertTo_id(mpath.get(localField[i], doc, lookupLocalFields), schema);
hasMatchFunction = true;
}
localField = localField[0];
foreignField = foreignField[0];
}
const localFieldPathType = modelSchema._getPathType(localField);
const localFieldPath = localFieldPathType === 'real' ? modelSchema.path(localField) : localFieldPathType.schema;
const localFieldGetters = localFieldPath && localFieldPath.getters ? localFieldPath.getters : [];
@@ -201,31 +231,21 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
options.isVirtual && get(virtual, 'options.getters', false);
if (localFieldGetters.length > 0 && getters) {
const hydratedDoc = (doc.$__ != null) ? doc : model.hydrate(doc);
const localFieldValue = utils.getValue(localField, doc);
const localFieldValue = mpath.get(localField, doc, lookupLocalFields);
if (Array.isArray(localFieldValue)) {
const localFieldHydratedValue = utils.getValue(localField.split('.').slice(0, -1), hydratedDoc);
const localFieldHydratedValue = mpath.get(localField.split('.').slice(0, -1), hydratedDoc, lookupLocalFields);
ret = localFieldValue.map((localFieldArrVal, localFieldArrIndex) =>
localFieldPath.applyGetters(localFieldArrVal, localFieldHydratedValue[localFieldArrIndex]));
} else {
ret = localFieldPath.applyGetters(localFieldValue, hydratedDoc);
}
} else {
ret = convertTo_id(utils.getValue(localField, doc), schema);
ret = convertTo_id(mpath.get(localField, doc, lookupLocalFields), schema);
}
const id = String(utils.getValue(foreignField, doc));
options._docs[id] = Array.isArray(ret) ? ret.slice() : ret;
let match = get(options, 'match', null) ||
get(currentOptions, 'match', null) ||
get(options, 'virtual.options.match', null) ||
get(options, 'virtual.options.options.match', null);
const hasMatchFunction = typeof match === 'function';
if (hasMatchFunction) {
match = match.call(doc, doc);
}
// Re: gh-8452. Embedded discriminators may not have `refPath`, so clear
// out embedded discriminator docs that don't have a `refPath` on the
// populated path.
@@ -280,7 +300,12 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
originalModel :
modelName[modelSymbol] ? modelName : connection.model(modelName);
} catch (error) {
return error;
// If `ret` is undefined, we'll add an empty entry to modelsMap. We shouldn't
// execute a query, but it is necessary to make sure `justOne` gets handled
// correctly for setting an empty array (see gh-8455)
if (ret !== undefined) {
return error;
}
}
let ids = ret;
@@ -290,13 +315,15 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
ids = flat.filter((val, i) => modelNames[i] === modelName);
}
if (!available[modelName] || currentOptions.perDocumentLimit != null) {
if (!available[modelName] || currentOptions.perDocumentLimit != null || get(currentOptions, 'options.perDocumentLimit') != null) {
currentOptions = {
model: Model
};
if (isVirtual && get(virtual, 'options.options')) {
currentOptions.options = utils.clone(virtual.options.options);
} else if (schemaOptions != null) {
currentOptions.options = Object.assign({}, schemaOptions);
}
utils.merge(currentOptions, options);
@@ -332,13 +359,13 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
}
}
}
return map;
function _getModelNames(doc, schema) {
let modelNames;
let discriminatorKey;
let isRefPath = false;
let justOne = null;
if (schema && schema.caster) {
schema = schema.caster;
@@ -378,28 +405,26 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
} else {
let modelForCurrentDoc = model;
let schemaForCurrentDoc;
let discriminatorValue;
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;
}
if (!schema && discriminatorKey && (discriminatorValue = utils.getValue(discriminatorKey, doc))) {
// `modelNameForFind` is the discriminator value, so we might need
// find the discriminated model name
const discriminatorModel = getDiscriminatorByValue(model.discriminators, discriminatorValue) || model;
if (discriminatorModel != null) {
modelForCurrentDoc = discriminatorModel;
} else {
try {
modelForCurrentDoc = model.db.model(discriminatorValue);
} catch (error) {
return error;
}
}
schemaForCurrentDoc = modelForCurrentDoc.schema._getSchema(options.path);
schemaForCurrentDoc = modelForCurrentDoc.schema._getSchema(options.path);
if (schemaForCurrentDoc && schemaForCurrentDoc.caster) {
schemaForCurrentDoc = schemaForCurrentDoc.caster;
}
if (schemaForCurrentDoc && schemaForCurrentDoc.caster) {
schemaForCurrentDoc = schemaForCurrentDoc.caster;
}
} else {
schemaForCurrentDoc = schema;
@@ -407,6 +432,10 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
const _virtualRes = getVirtual(modelForCurrentDoc.schema, options.path);
const virtual = _virtualRes == null ? null : _virtualRes.virtual;
if (schemaForCurrentDoc != null) {
justOne = !schemaForCurrentDoc.$isMongooseArray && !schemaForCurrentDoc._arrayPath;
}
let ref;
let refPath;
@@ -440,14 +469,14 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
}
if (!modelNames) {
return { modelNames: modelNames, isRefPath: isRefPath, refPath: normalizedRefPath };
return { modelNames: modelNames, isRefPath: isRefPath, refPath: normalizedRefPath, justOne: justOne };
}
if (!Array.isArray(modelNames)) {
modelNames = [modelNames];
}
return { modelNames: modelNames, isRefPath: isRefPath, refPath: normalizedRefPath };
return { modelNames: modelNames, isRefPath: isRefPath, refPath: normalizedRefPath, justOne: justOne };
}
};
@@ -470,7 +499,12 @@ function handleRefFunction(ref, doc) {
*/
function convertTo_id(val, schema) {
if (val != null && val.$__ != null) return val._id;
if (val != null && val.$__ != null) {
return val._id;
}
if (val != null && val._id != null && (schema == null || !schema.$isSchemaMap)) {
return val._id;
}
if (Array.isArray(val)) {
for (let i = 0; i < val.length; ++i) {
@@ -479,7 +513,7 @@ function convertTo_id(val, schema) {
}
}
if (val.isMongooseArray && val.$schema()) {
return val.$schema().cast(val, val.$parent());
return val.$schema()._castForPopulate(val, val.$parent());
}
return [].concat(val);
@@ -487,8 +521,7 @@ function convertTo_id(val, schema) {
// `populate('map')` may be an object if populating on a doc that hasn't
// been hydrated yet
if (val != null &&
val.constructor.name === 'Object' &&
if (getConstructorName(val) === 'Object' &&
// The intent here is we should only flatten the object if we expect
// to get a Map in the end. Avoid doing this for mixed types.
(schema == null || schema[schemaMixedSymbol] == null)) {

View File

@@ -20,7 +20,6 @@ const populateModelSymbol = require('../symbols').populateModelSymbol;
module.exports = function getSchemaTypes(schema, doc, path) {
const pathschema = schema.path(path);
const topLevelDoc = doc;
if (pathschema) {
return pathschema;
}
@@ -33,7 +32,6 @@ module.exports = function getSchemaTypes(schema, doc, path) {
while (p--) {
trypath = parts.slice(0, p).join('.');
foundschema = schema.path(trypath);
if (foundschema == null) {
continue;
}
@@ -52,7 +50,8 @@ module.exports = function getSchemaTypes(schema, doc, path) {
const keys = subdoc ? mpath.get(discriminatorKeyPath, subdoc) || [] : [];
schemas = Object.keys(discriminators).
reduce(function(cur, discriminator) {
if (doc == null || keys.indexOf(discriminator) !== -1) {
const tiedValue = discriminators[discriminator].discriminatorMapping.value;
if (doc == null || keys.indexOf(discriminator) !== -1 || keys.indexOf(tiedValue) !== -1) {
cur.push(discriminators[discriminator]);
}
return cur;
@@ -117,24 +116,40 @@ module.exports = function getSchemaTypes(schema, doc, path) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
!foundschema.schema.$isSingleNested;
}
return ret;
}
} else if (p !== parts.length &&
foundschema.$isMongooseArray &&
foundschema.casterConstructor.$isMongooseArray) {
// Nested arrays. Drill down to the bottom of the nested array.
// Ignore discriminators.
let type = foundschema;
while (type.$isMongooseArray && !type.$isMongooseDocumentArray) {
type = type.casterConstructor;
}
return search(
const ret = search(
parts.slice(p),
type.schema,
null,
nestedPath.concat(parts.slice(0, p))
);
if (ret != null) {
return ret;
}
if (type.schema.discriminators) {
const discriminatorPaths = [];
for (const discriminatorName of Object.keys(type.schema.discriminators)) {
const _schema = type.schema.discriminators[discriminatorName] || type.schema;
const ret = search(parts.slice(p), _schema, null, nestedPath.concat(parts.slice(0, p)));
if (ret != null) {
discriminatorPaths.push(ret);
}
}
if (discriminatorPaths.length > 0) {
return discriminatorPaths;
}
}
}
}
@@ -153,7 +168,6 @@ module.exports = function getSchemaTypes(schema, doc, path) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
!model.schema.$isSingleNested;
}
return ret;
}
}
@@ -173,19 +187,16 @@ module.exports = function getSchemaTypes(schema, doc, path) {
nestedPath.concat(parts.slice(0, p))
);
if (ret) {
if (ret != null) {
ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
!schema.$isSingleNested;
return ret;
}
return ret;
}
}
return foundschema;
}
}
// look for arrays
const parts = path.split('.');
for (let i = 0; i < parts.length; ++i) {

View File

@@ -5,4 +5,11 @@ if (typeof jest !== 'undefined' && typeof window !== 'undefined') {
'with Jest\'s default jsdom test environment. Please make sure you read ' +
'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
'http://mongoosejs.com/docs/jest.html');
}
if (typeof jest !== 'undefined' && process.nextTick.toString().indexOf('nextTick') === -1) {
console.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
'with Jest\'s mock timers enabled. Please make sure you read ' +
'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
'http://mongoosejs.com/docs/jest.html');
}

View File

@@ -7,6 +7,10 @@ const isDefiningProjection = require('./isDefiningProjection');
*/
module.exports = function isExclusive(projection) {
if (projection == null) {
return null;
}
const keys = Object.keys(projection);
let ki = keys.length;
let exclude = null;

View File

@@ -1,21 +1,22 @@
'use strict';
const PromiseProvider = require('../promise_provider');
const immediate = require('./immediate');
const emittedSymbol = Symbol.for('mongoose:emitted');
const emittedSymbol = Symbol('mongoose:emitted');
module.exports = function promiseOrCallback(callback, fn, ee) {
module.exports = function promiseOrCallback(callback, fn, ee, Promise) {
if (typeof callback === 'function') {
return fn(function(error) {
if (error != null) {
if (ee != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
error[emittedSymbol] = true;
ee.emit('error', error);
}
try {
callback(error);
} catch (error) {
return process.nextTick(() => {
return immediate(() => {
throw error;
});
}
@@ -25,12 +26,12 @@ module.exports = function promiseOrCallback(callback, fn, ee) {
});
}
const Promise = PromiseProvider.get();
Promise = Promise || PromiseProvider.get();
return new Promise((resolve, reject) => {
fn(function(error, res) {
if (error != null) {
if (ee != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
error[emittedSymbol] = true;
ee.emit('error', error);
}
@@ -42,4 +43,4 @@ module.exports = function promiseOrCallback(callback, fn, ee) {
resolve(res);
});
});
};
};

View File

@@ -56,6 +56,9 @@ function applyQueryMiddleware(Query, model) {
if (hook.name === 'validate' || hook.name === 'remove') {
return !!contexts.query;
}
if (hook.query != null || hook.document != null) {
return !!hook.query;
}
return true;
});

View File

@@ -25,7 +25,7 @@ module.exports = function castFilterPath(query, schematype, val) {
if (nested && schematype && !schematype.caster) {
const _keys = Object.keys(nested);
if (_keys.length && isOperator(_keys[0])) {
for (const key in nested) {
for (const key of Object.keys(nested)) {
nested[key] = schematype.castForQueryWrapper({
$conditional: key,
val: nested[key],

View File

@@ -6,10 +6,12 @@ const StrictModeError = require('../../error/strict');
const ValidationError = require('../../error/validation');
const castNumber = require('../../cast/number');
const cast = require('../../cast');
const getConstructorName = require('../getConstructorName');
const getEmbeddedDiscriminatorPath = require('./getEmbeddedDiscriminatorPath');
const handleImmutable = require('./handleImmutable');
const moveImmutableProperties = require('../update/moveImmutableProperties');
const schemaMixedSymbol = require('../../schema/symbols').schemaMixedSymbol;
const setDottedPath = require('../path/setDottedPath');
const utils = require('../../utils');
/*!
@@ -105,6 +107,14 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
}
}
if (Object.keys(ret).length === 0 &&
options.upsert &&
Object.keys(filter).length > 0) {
// Trick the driver into allowing empty upserts to work around
// https://github.com/mongodb/node-mongodb-native/pull/2490
return { $setOnInsert: filter };
}
return ret;
};
@@ -191,11 +201,19 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
}
}
if (val && val.constructor.name === 'Object') {
if (getConstructorName(val) === 'Object') {
// watch for embedded doc schemas
schematype = schema._getSchema(prefix + key);
if (handleImmutable(schematype, strict, obj, key, prefix + key, context)) {
if (schematype == null) {
const _res = getEmbeddedDiscriminatorPath(schema, obj, filter, prefix + key, options);
if (_res.schematype != null) {
schematype = _res.schematype;
}
}
if (op !== '$setOnInsert' &&
handleImmutable(schematype, strict, obj, key, prefix + key, context)) {
continue;
}
@@ -308,7 +326,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
// If no schema type, check for embedded discriminators because the
// filter or update may imply an embedded discriminator type. See #8378
if (schematype == null) {
const _res = getEmbeddedDiscriminatorPath(schema, obj, filter, checkPath);
const _res = getEmbeddedDiscriminatorPath(schema, obj, filter, checkPath, options);
if (_res.schematype != null) {
schematype = _res.schematype;
pathDetails = _res.type;
@@ -344,7 +362,15 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
}
try {
obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
if (prefix.length === 0 || key.indexOf('.') === -1) {
obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
} else {
// Setting a nested dotted path that's in the schema. We don't allow paths with '.' in
// a schema, so replace the dotted path with a nested object to avoid ending up with
// dotted properties in the updated object. See (gh-10200)
setDottedPath(obj, key, castUpdateVal(schematype, val, op, key, context, prefix + key));
delete obj[key];
}
} catch (error) {
aggregatedError = _handleCastError(error, context, key, aggregatedError);
}

View File

@@ -1,6 +1,7 @@
'use strict';
const helpers = require('../../queryhelpers');
const immediate = require('../immediate');
module.exports = completeMany;
@@ -29,10 +30,10 @@ function completeMany(model, docs, fields, userProvidedFields, opts, callback) {
error = error || _error;
}
if (error != null) {
--count || process.nextTick(() => callback(error));
--count || immediate(() => callback(error));
return;
}
--count || process.nextTick(() => callback(error, arr));
--count || immediate(() => callback(error, arr));
}
for (let i = 0; i < len; ++i) {
@@ -42,6 +43,9 @@ function completeMany(model, docs, fields, userProvidedFields, opts, callback) {
} catch (error) {
init(error);
}
arr[i].$session(opts.session);
if (opts.session != null) {
arr[i].$session(opts.session);
}
}
}

View File

@@ -3,19 +3,23 @@
const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
const get = require('../get');
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
const updatedPathsByArrayFilter = require('../update/updatedPathsByArrayFilter');
/*!
* Like `schema.path()`, except with a document, because impossible to
* determine path type without knowing the embedded discriminator key.
*/
module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, path) {
module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, path, options) {
const parts = path.split('.');
let schematype = null;
let type = 'adhocOrUndefined';
filter = filter || {};
update = update || {};
const arrayFilters = options != null && Array.isArray(options.arrayFilters) ?
options.arrayFilters : [];
const updatedPathsByFilter = updatedPathsByArrayFilter(update);
for (let i = 0; i < parts.length; ++i) {
const subpath = cleanPositionalOperators(parts.slice(0, i + 1).join('.'));
@@ -39,6 +43,7 @@ module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, p
if (discriminatorFilterPath in filter) {
discriminatorKey = filter[discriminatorFilterPath];
}
const wrapperPath = subpath.replace(/\.\d+$/, '');
if (schematype.$isMongooseDocumentArrayElement &&
get(filter[wrapperPath], '$elemMatch.' + key) != null) {
@@ -49,11 +54,22 @@ module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, p
discriminatorKey = update[discriminatorValuePath];
}
for (const filterKey of Object.keys(updatedPathsByFilter)) {
const schemaKey = updatedPathsByFilter[filterKey] + '.' + key;
const arrayFilterKey = filterKey + '.' + key;
if (schemaKey === discriminatorFilterPath) {
const filter = arrayFilters.find(filter => filter.hasOwnProperty(arrayFilterKey));
if (filter != null) {
discriminatorKey = filter[arrayFilterKey];
}
}
}
if (discriminatorKey == null) {
continue;
}
const discriminatorSchema = getDiscriminatorByValue(schematype.caster, discriminatorKey).schema;
const discriminatorSchema = getDiscriminatorByValue(schematype.caster.discriminators, discriminatorKey).schema;
const rest = parts.slice(i + 1).join('.');
schematype = discriminatorSchema.path(rest);

View File

@@ -1,28 +1,31 @@
'use strict';
const isExclusive = require('../projection/isExclusive');
const isInclusive = require('../projection/isInclusive');
/*!
* ignore
*/
module.exports = function selectPopulatedFields(query) {
const opts = query._mongooseOptions;
module.exports = function selectPopulatedFields(fields, userProvidedFields, populateOptions) {
if (populateOptions == null) {
return;
}
if (opts.populate != null) {
const paths = Object.keys(opts.populate);
const userProvidedFields = query._userProvidedFields || {};
if (query.selectedInclusively()) {
for (const path of paths) {
if (!isPathInFields(userProvidedFields, path)) {
query.select(path);
} else if (userProvidedFields[path] === 0) {
delete query._fields[path];
}
const paths = Object.keys(populateOptions);
userProvidedFields = userProvidedFields || {};
if (isInclusive(fields)) {
for (const path of paths) {
if (!isPathInFields(userProvidedFields, path)) {
fields[path] = 1;
} else if (userProvidedFields[path] === 0) {
delete fields[path];
}
} else if (query.selectedExclusively()) {
for (const path of paths) {
if (userProvidedFields[path] == null) {
delete query._fields[path];
}
}
} else if (isExclusive(fields)) {
for (const path of paths) {
if (userProvidedFields[path] == null) {
delete fields[path];
}
}
}
@@ -37,10 +40,10 @@ function isPathInFields(userProvidedFields, path) {
const len = pieces.length;
let cur = pieces[0];
for (let i = 1; i < len; ++i) {
if (userProvidedFields[cur] != null) {
if (userProvidedFields[cur] != null || userProvidedFields[cur + '.$'] != null) {
return true;
}
cur += '.' + pieces[i];
}
return userProvidedFields[cur] != null;
return userProvidedFields[cur] != null || userProvidedFields[cur + '.$'] != null;
}

View File

@@ -4,13 +4,27 @@ const get = require('../get');
module.exports = function applyWriteConcern(schema, options) {
const writeConcern = get(schema, 'options.writeConcern', {});
if (!('w' in options) && writeConcern.w != null) {
options.w = writeConcern.w;
if (Object.keys(writeConcern).length != 0) {
options.writeConcern = {};
if (!('w' in options) && writeConcern.w != null) {
options.writeConcern.w = writeConcern.w;
}
if (!('j' in options) && writeConcern.j != null) {
options.writeConcern.j = writeConcern.j;
}
if (!('wtimeout' in options) && writeConcern.wtimeout != null) {
options.writeConcern.wtimeout = writeConcern.wtimeout;
}
}
if (!('j' in options) && writeConcern.j != null) {
options.j = writeConcern.j;
}
if (!('wtimeout' in options) && writeConcern.wtimeout != null) {
options.wtimeout = writeConcern.wtimeout;
else {
if (!('w' in options) && writeConcern.w != null) {
options.w = writeConcern.w;
}
if (!('j' in options) && writeConcern.j != null) {
options.j = writeConcern.j;
}
if (!('wtimeout' in options) && writeConcern.wtimeout != null) {
options.wtimeout = writeConcern.wtimeout;
}
}
};

View File

@@ -7,6 +7,6 @@
module.exports = function cleanPositionalOperators(path) {
return path.
replace(/\.\$(\[[^\]]*\])?\./g, '.0.').
replace(/\.(\[[^\]]*\])?\$$/g, '.0');
replace(/\.\$(\[[^\]]*\])?(?=\.)/g, '.0').
replace(/\.\$(\[[^\]]*\])?$/g, '.0');
};

View File

@@ -1,7 +1,15 @@
'use strict';
module.exports = function merge(s1, s2) {
s1.add(s2.tree || {});
module.exports = function merge(s1, s2, skipConflictingPaths) {
const paths = Object.keys(s2.tree);
const pathsToAdd = {};
for (const key of paths) {
if (skipConflictingPaths && (s1.paths[key] || s1.nested[key] || s1.singleNestedPaths[key])) {
continue;
}
pathsToAdd[key] = s2.tree[key];
}
s1.add(pathsToAdd);
s1.callQueue = s1.callQueue.concat(s2.callQueue);
s1.method(s2.methods);

View File

@@ -33,11 +33,13 @@ function createImmutableSetter(path, immutable) {
if (!_immutable) {
return v;
}
if (this.$__.strictMode === 'throw' && v !== this[path]) {
const _value = this.$__getValue(path);
if (this.$__.strictMode === 'throw' && v !== _value) {
throw new StrictModeError(path, 'Path `' + path + '` is immutable ' +
'and strict mode is set to throw.', true);
}
return this[path];
return _value;
};
}

View File

@@ -14,6 +14,17 @@ const get = require('./get');
*/
module.exports = function(filter, schema, castedDoc, options) {
options = options || {};
const shouldSetDefaultsOnInsert =
options.setDefaultsOnInsert != null ?
options.setDefaultsOnInsert :
schema.base.options.setDefaultsOnInsert;
if (!options.upsert || !shouldSetDefaultsOnInsert) {
return castedDoc;
}
const keys = Object.keys(castedDoc || {});
const updatedKeys = {};
const updatedValues = {};
@@ -22,12 +33,6 @@ module.exports = function(filter, schema, castedDoc, options) {
let hasDollarUpdate = false;
options = options || {};
if (!options.upsert || !options.setDefaultsOnInsert) {
return castedDoc;
}
for (let i = 0; i < numKeys; ++i) {
if (keys[i].startsWith('$')) {
modifiedPaths(castedDoc[keys[i]], '', modified);

View File

@@ -5,11 +5,15 @@ exports.arrayParentSymbol = Symbol('mongoose#Array#_parent');
exports.arrayPathSymbol = Symbol('mongoose#Array#_path');
exports.arraySchemaSymbol = Symbol('mongoose#Array#_schema');
exports.documentArrayParent = Symbol('mongoose:documentArrayParent');
exports.documentIsSelected = Symbol('mongoose#Document#isSelected');
exports.documentIsModified = Symbol('mongoose#Document#isModified');
exports.documentModifiedPaths = Symbol('mongoose#Document#modifiedPaths');
exports.documentSchemaSymbol = Symbol('mongoose#Document#schema');
exports.getSymbol = Symbol('mongoose#Document#get');
exports.modelSymbol = Symbol('mongoose#Model');
exports.objectIdSymbol = Symbol('mongoose#ObjectId');
exports.populateModelSymbol = Symbol('mongoose.PopulateOptions#Model');
exports.schemaTypeSymbol = Symbol('mongoose#schemaType');
exports.sessionNewDocuments = Symbol('mongoose:ClientSession#newDocuments');
exports.scopeSymbol = Symbol('mongoose#Document#scope');
exports.validatorErrorSymbol = Symbol('mongoose:validatorError');

View File

@@ -1,11 +1,12 @@
'use strict';
const getConstructorName = require('../getConstructorName');
module.exports = function allServersUnknown(topologyDescription) {
if (topologyDescription == null ||
topologyDescription.constructor.name !== 'TopologyDescription') {
if (getConstructorName(topologyDescription) !== 'TopologyDescription') {
return false;
}
return Array.from(topologyDescription.servers.values()).
every(server => server.type === 'Unknown');
const servers = Array.from(topologyDescription.servers.values());
return servers.length > 0 && servers.every(server => server.type === 'Unknown');
};

View File

@@ -1,11 +1,13 @@
'use strict';
const getConstructorName = require('../getConstructorName');
module.exports = function isAtlas(topologyDescription) {
if (topologyDescription == null ||
topologyDescription.constructor.name !== 'TopologyDescription') {
if (getConstructorName(topologyDescription) !== 'TopologyDescription') {
return false;
}
return Array.from(topologyDescription.servers.keys()).
every(host => host.endsWith('.mongodb.net:27017'));
const hostnames = Array.from(topologyDescription.servers.keys());
return hostnames.length > 0 &&
hostnames.every(host => host.endsWith('.mongodb.net:27017'));
};

View File

@@ -15,133 +15,61 @@ function applyTimestampsToChildren(now, update, schema) {
}
const keys = Object.keys(update);
let key;
let createdAt;
let updatedAt;
let timestamps;
let path;
const hasDollarKey = keys.length && keys[0].startsWith('$');
const hasDollarKey = keys.some(key => key.startsWith('$'));
if (hasDollarKey) {
if (update.$push) {
for (key in update.$push) {
const $path = schema.path(key);
if (update.$push[key] &&
$path &&
$path.$isMongooseDocumentArray &&
$path.schema.options.timestamps) {
timestamps = $path.schema.options.timestamps;
createdAt = handleTimestampOption(timestamps, 'createdAt');
updatedAt = handleTimestampOption(timestamps, 'updatedAt');
if (update.$push[key].$each) {
update.$push[key].$each.forEach(function(subdoc) {
if (updatedAt != null) {
subdoc[updatedAt] = now;
}
if (createdAt != null) {
subdoc[createdAt] = now;
}
});
} else {
if (updatedAt != null) {
update.$push[key][updatedAt] = now;
}
if (createdAt != null) {
update.$push[key][createdAt] = now;
}
}
}
}
_applyTimestampToUpdateOperator(update.$push);
}
if (update.$addToSet) {
_applyTimestampToUpdateOperator(update.$addToSet);
}
if (update.$set != null) {
const keys = Object.keys(update.$set);
for (key of keys) {
// Replace positional operator `$` and array filters `$[]` and `$[.*]`
const keyToSearch = cleanPositionalOperators(key);
path = schema.path(keyToSearch);
if (!path) {
continue;
}
const parentSchemaTypes = [];
const pieces = keyToSearch.split('.');
for (let i = pieces.length - 1; i > 0; --i) {
const s = schema.path(pieces.slice(0, i).join('.'));
if (s != null &&
(s.$isMongooseDocumentArray || s.$isSingleNested)) {
parentSchemaTypes.push({ parentPath: key.split('.').slice(0, i).join('.'), parentSchemaType: s });
}
}
if (Array.isArray(update.$set[key]) && path.$isMongooseDocumentArray) {
applyTimestampsToDocumentArray(update.$set[key], path, now);
} else if (update.$set[key] && path.$isSingleNested) {
applyTimestampsToSingleNested(update.$set[key], path, now);
} else if (parentSchemaTypes.length > 0) {
for (const item of parentSchemaTypes) {
const parentPath = item.parentPath;
const parentSchemaType = item.parentSchemaType;
timestamps = parentSchemaType.schema.options.timestamps;
createdAt = handleTimestampOption(timestamps, 'createdAt');
updatedAt = handleTimestampOption(timestamps, 'updatedAt');
if (!timestamps || updatedAt == null) {
continue;
}
if (parentSchemaType.$isSingleNested) {
// Single nested is easy
update.$set[parentPath + '.' + updatedAt] = now;
continue;
}
if (parentSchemaType.$isMongooseDocumentArray) {
let childPath = key.substr(parentPath.length + 1);
if (/^\d+$/.test(childPath)) {
update.$set[parentPath + '.' + childPath][updatedAt] = now;
continue;
}
const firstDot = childPath.indexOf('.');
childPath = firstDot !== -1 ? childPath.substr(0, firstDot) : childPath;
update.$set[parentPath + '.' + childPath + '.' + updatedAt] = now;
}
}
} else if (path.schema != null && path.schema != schema && update.$set[key]) {
timestamps = path.schema.options.timestamps;
createdAt = handleTimestampOption(timestamps, 'createdAt');
updatedAt = handleTimestampOption(timestamps, 'updatedAt');
if (!timestamps) {
continue;
}
if (updatedAt != null) {
update.$set[key][updatedAt] = now;
}
if (createdAt != null) {
update.$set[key][createdAt] = now;
}
}
for (const key of keys) {
applyTimestampsToUpdateKey(schema, key, update.$set, now);
}
}
} else {
const keys = Object.keys(update).filter(key => !key.startsWith('$'));
for (key of keys) {
// Replace positional operator `$` and array filters `$[]` and `$[.*]`
const keyToSearch = cleanPositionalOperators(key);
path = schema.path(keyToSearch);
if (!path) {
continue;
if (update.$setOnInsert != null) {
const keys = Object.keys(update.$setOnInsert);
for (const key of keys) {
applyTimestampsToUpdateKey(schema, key, update.$setOnInsert, now);
}
}
}
if (Array.isArray(update[key]) && path.$isMongooseDocumentArray) {
applyTimestampsToDocumentArray(update[key], path, now);
} else if (update[key] != null && path.$isSingleNested) {
applyTimestampsToSingleNested(update[key], path, now);
const updateKeys = Object.keys(update).filter(key => !key.startsWith('$'));
for (const key of updateKeys) {
applyTimestampsToUpdateKey(schema, key, update, now);
}
function _applyTimestampToUpdateOperator(op) {
for (const key of Object.keys(op)) {
const $path = schema.path(key.replace(/\.\$\./i, '.').replace(/.\$$/, ''));
if (op[key] &&
$path &&
$path.$isMongooseDocumentArray &&
$path.schema.options.timestamps) {
const timestamps = $path.schema.options.timestamps;
const createdAt = handleTimestampOption(timestamps, 'createdAt');
const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
if (op[key].$each) {
op[key].$each.forEach(function(subdoc) {
if (updatedAt != null) {
subdoc[updatedAt] = now;
}
if (createdAt != null) {
subdoc[createdAt] = now;
}
});
} else {
if (updatedAt != null) {
op[key][updatedAt] = now;
}
if (createdAt != null) {
op[key][createdAt] = now;
}
}
}
}
}
@@ -187,3 +115,71 @@ function applyTimestampsToSingleNested(subdoc, schematype, now) {
applyTimestampsToChildren(now, subdoc, schematype.schema);
}
function applyTimestampsToUpdateKey(schema, key, update, now) {
// Replace positional operator `$` and array filters `$[]` and `$[.*]`
const keyToSearch = cleanPositionalOperators(key);
const path = schema.path(keyToSearch);
if (!path) {
return;
}
const parentSchemaTypes = [];
const pieces = keyToSearch.split('.');
for (let i = pieces.length - 1; i > 0; --i) {
const s = schema.path(pieces.slice(0, i).join('.'));
if (s != null &&
(s.$isMongooseDocumentArray || s.$isSingleNested)) {
parentSchemaTypes.push({ parentPath: key.split('.').slice(0, i).join('.'), parentSchemaType: s });
}
}
if (Array.isArray(update[key]) && path.$isMongooseDocumentArray) {
applyTimestampsToDocumentArray(update[key], path, now);
} else if (update[key] && path.$isSingleNested) {
applyTimestampsToSingleNested(update[key], path, now);
} else if (parentSchemaTypes.length > 0) {
for (const item of parentSchemaTypes) {
const parentPath = item.parentPath;
const parentSchemaType = item.parentSchemaType;
const timestamps = parentSchemaType.schema.options.timestamps;
const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
if (!timestamps || updatedAt == null) {
continue;
}
if (parentSchemaType.$isSingleNested) {
// Single nested is easy
update[parentPath + '.' + updatedAt] = now;
} else if (parentSchemaType.$isMongooseDocumentArray) {
let childPath = key.substr(parentPath.length + 1);
if (/^\d+$/.test(childPath)) {
update[parentPath + '.' + childPath][updatedAt] = now;
continue;
}
const firstDot = childPath.indexOf('.');
childPath = firstDot !== -1 ? childPath.substr(0, firstDot) : childPath;
update[parentPath + '.' + childPath + '.' + updatedAt] = now;
}
}
} else if (path.schema != null && path.schema != schema && update[key]) {
const timestamps = path.schema.options.timestamps;
const createdAt = handleTimestampOption(timestamps, 'createdAt');
const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
if (!timestamps) {
return;
}
if (updatedAt != null) {
update[key][updatedAt] = now;
}
if (createdAt != null) {
update[key][createdAt] = now;
}
}
}

View File

@@ -3,7 +3,7 @@
const castFilterPath = require('../query/castFilterPath');
const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
const getPath = require('../schema/getPath');
const modifiedPaths = require('./modifiedPaths');
const updatedPathsByArrayFilter = require('./updatedPathsByArrayFilter');
module.exports = function castArrayFilters(query) {
const arrayFilters = query.options.arrayFilters;
@@ -15,31 +15,13 @@ module.exports = function castArrayFilters(query) {
const schema = query.schema;
const strictQuery = schema.options.strictQuery;
const updatedPaths = modifiedPaths(update);
const updatedPathsByFilter = Object.keys(updatedPaths).reduce((cur, path) => {
const matches = path.match(/\$\[[^\]]+\]/g);
if (matches == null) {
return cur;
}
for (const match of matches) {
const firstMatch = path.indexOf(match);
if (firstMatch !== path.lastIndexOf(match)) {
throw new Error(`Path '${path}' contains the same array filter multiple times`);
}
cur[match.substring(2, match.length - 1)] = path.
substr(0, firstMatch - 1).
replace(/\$\[[^\]]+\]/g, '0');
}
return cur;
}, {});
const updatedPathsByFilter = updatedPathsByArrayFilter(update);
for (const filter of arrayFilters) {
if (filter == null) {
throw new Error(`Got null array filter in ${arrayFilters}`);
}
for (const key in filter) {
for (const key of Object.keys(filter)) {
if (filter[key] == null) {
continue;
}