diff options
Diffstat (limited to 'common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services')
3 files changed, 283 insertions, 0 deletions
diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/common.js b/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/common.js new file mode 100644 index 0000000..d125987 --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/common.js @@ -0,0 +1,87 @@ +'use strict'; + +/*! + * Module dependencies. + */ + +var ObjectId = require('../types/objectid'); +var utils = require('../utils'); + +exports.flatten = flatten; +exports.modifiedPaths = modifiedPaths; + +/*! + * ignore + */ + +function flatten(update, path, options) { + var keys; + if (update && utils.isMongooseObject(update) && !Buffer.isBuffer(update)) { + keys = Object.keys(update.toObject({ transform: false })); + } else { + keys = Object.keys(update || {}); + } + + var numKeys = keys.length; + var result = {}; + path = path ? path + '.' : ''; + + for (var i = 0; i < numKeys; ++i) { + var key = keys[i]; + var val = update[key]; + result[path + key] = val; + if (shouldFlatten(val)) { + if (options && options.skipArrays && Array.isArray(val)) { + continue; + } + var flat = flatten(val, path + key); + for (var k in flat) { + result[k] = flat[k]; + } + if (Array.isArray(val)) { + result[path + key] = val; + } + } + } + + return result; +} + +/*! + * ignore + */ + +function modifiedPaths(update, path, result) { + var keys = Object.keys(update || {}); + var numKeys = keys.length; + result = result || {}; + path = path ? path + '.' : ''; + + for (var i = 0; i < numKeys; ++i) { + var key = keys[i]; + var val = update[key]; + + result[path + key] = true; + if (utils.isMongooseObject(val) && !Buffer.isBuffer(val)) { + val = val.toObject({ transform: false }); + } + if (shouldFlatten(val)) { + modifiedPaths(val, path + key, result); + } + } + + return result; +} + +/*! + * ignore + */ + +function shouldFlatten(val) { + return val && + typeof val === 'object' && + !(val instanceof Date) && + !(val instanceof ObjectId) && + (!Array.isArray(val) || val.length > 0) && + !(val instanceof Buffer); +} diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/setDefaultsOnInsert.js b/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/setDefaultsOnInsert.js new file mode 100644 index 0000000..36b87d7 --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/setDefaultsOnInsert.js @@ -0,0 +1,96 @@ +'use strict'; + +var modifiedPaths = require('./common').modifiedPaths; + +/** + * Applies defaults to update and findOneAndUpdate operations. + * + * @param {Query} query + * @param {Schema} schema + * @param {Object} castedDoc + * @param {Object} options + * @method setDefaultsOnInsert + * @api private + */ + +module.exports = function(query, schema, castedDoc, options) { + var keys = Object.keys(castedDoc || {}); + var updatedKeys = {}; + var updatedValues = {}; + var numKeys = keys.length; + var hasDollarUpdate = false; + var modified = {}; + + if (options && options.upsert) { + for (var i = 0; i < numKeys; ++i) { + if (keys[i].charAt(0) === '$') { + modifiedPaths(castedDoc[keys[i]], '', modified); + hasDollarUpdate = true; + } + } + + if (!hasDollarUpdate) { + modifiedPaths(castedDoc, '', modified); + } + + var paths = Object.keys(query._conditions); + var numPaths = keys.length; + for (i = 0; i < numPaths; ++i) { + var path = paths[i]; + var condition = query._conditions[path]; + if (condition && typeof condition === 'object') { + var conditionKeys = Object.keys(condition); + var numConditionKeys = conditionKeys.length; + var hasDollarKey = false; + for (var j = 0; j < numConditionKeys; ++j) { + if (conditionKeys[j].charAt(0) === '$') { + hasDollarKey = true; + break; + } + } + if (hasDollarKey) { + continue; + } + } + updatedKeys[path] = true; + modified[path] = true; + } + + if (options.setDefaultsOnInsert) { + schema.eachPath(function(path, schemaType) { + if (path === '_id') { + // Ignore _id for now because it causes bugs in 2.4 + return; + } + if (schemaType.$isSingleNested) { + // Only handle nested schemas 1-level deep to avoid infinite + // recursion re: https://github.com/mongodb-js/mongoose-autopopulate/issues/11 + schemaType.schema.eachPath(function(_path, _schemaType) { + if (path === '_id') { + // Ignore _id for now because it causes bugs in 2.4 + return; + } + + var def = _schemaType.getDefault(null, true); + if (!modified[path + '.' + _path] && typeof def !== 'undefined') { + castedDoc = castedDoc || {}; + castedDoc.$setOnInsert = castedDoc.$setOnInsert || {}; + castedDoc.$setOnInsert[path + '.' + _path] = def; + updatedValues[path + '.' + _path] = def; + } + }); + } else { + var def = schemaType.getDefault(null, true); + if (!modified[path] && typeof def !== 'undefined') { + castedDoc = castedDoc || {}; + castedDoc.$setOnInsert = castedDoc.$setOnInsert || {}; + castedDoc.$setOnInsert[path] = def; + updatedValues[path] = def; + } + } + }); + } + } + + return castedDoc; +}; diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/updateValidators.js b/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/updateValidators.js new file mode 100644 index 0000000..162ea73 --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/updateValidators.js @@ -0,0 +1,100 @@ +/*! + * Module dependencies. + */ + +var Mixed = require('../schema/mixed'); +var ValidationError = require('../error/validation'); +var parallel = require('async/parallel'); +var flatten = require('./common').flatten; +var modifiedPaths = require('./common').modifiedPaths; + +/** + * Applies validators and defaults to update and findOneAndUpdate operations, + * specifically passing a null doc as `this` to validators and defaults + * + * @param {Query} query + * @param {Schema} schema + * @param {Object} castedDoc + * @param {Object} options + * @method runValidatorsOnUpdate + * @api private + */ + +module.exports = function(query, schema, castedDoc, options) { + var keys = Object.keys(castedDoc || {}); + var updatedKeys = {}; + var updatedValues = {}; + var numKeys = keys.length; + var hasDollarUpdate = false; + var modified = {}; + + for (var i = 0; i < numKeys; ++i) { + if (keys[i].charAt(0) === '$') { + modifiedPaths(castedDoc[keys[i]], '', modified); + var flat = flatten(castedDoc[keys[i]]); + var paths = Object.keys(flat); + var numPaths = paths.length; + for (var j = 0; j < numPaths; ++j) { + var updatedPath = paths[j].replace('.$.', '.0.'); + updatedPath = updatedPath.replace(/\.\$$/, '.0'); + if (keys[i] === '$set' || keys[i] === '$setOnInsert') { + updatedValues[updatedPath] = flat[paths[j]]; + } else if (keys[i] === '$unset') { + updatedValues[updatedPath] = undefined; + } + updatedKeys[updatedPath] = true; + } + hasDollarUpdate = true; + } + } + + if (!hasDollarUpdate) { + modifiedPaths(castedDoc, '', modified); + updatedValues = flatten(castedDoc); + updatedKeys = Object.keys(updatedValues); + } + + var updates = Object.keys(updatedValues); + var numUpdates = updates.length; + var validatorsToExecute = []; + var validationErrors = []; + function iter(i) { + var schemaPath = schema._getSchema(updates[i]); + if (schemaPath) { + // gh-4305: `_getSchema()` will report all sub-fields of a 'Mixed' path + // as 'Mixed', so avoid double validating them. + if (schemaPath instanceof Mixed && schemaPath.$fullPath !== updates[i]) { + return; + } + validatorsToExecute.push(function(callback) { + schemaPath.doValidate( + updatedValues[updates[i]], + function(err) { + if (err) { + err.path = updates[i]; + validationErrors.push(err); + } + callback(null); + }, + options && options.context === 'query' ? query : null, + {updateValidator: true}); + }); + } + } + for (i = 0; i < numUpdates; ++i) { + iter(i); + } + + return function(callback) { + parallel(validatorsToExecute, function() { + if (validationErrors.length) { + var err = new ValidationError(null); + for (var i = 0; i < validationErrors.length; ++i) { + err.errors[validationErrors[i].path] = validationErrors[i]; + } + return callback(err); + } + callback(null); + }); + }; +}; |