diff options
author | 2017-08-07 11:39:39 +0800 | |
---|---|---|
committer | 2017-08-07 11:39:39 +0800 | |
commit | 21d72c4a80fe2937d0c4ddd20624b27adbcd989b (patch) | |
tree | e5013ee12f74f8452e01cbff16e7b0158bc456cb /common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/setDefaultsOnInsert.js | |
parent | f533e73e2ae32e010b16abdcf7985abaf31ab843 (diff) |
Upload the ESR GUI seed code
Issue-ID: AAI-68
Change-Id: Ia50ce0570c2fabecd77199d4e8454f56fe587c4e
Signed-off-by: lizi00164331 <li.zi30@zte.com.cn>
Diffstat (limited to 'common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/setDefaultsOnInsert.js')
-rw-r--r-- | common/src/main/webapp/usageguide/appserver/node_modules/mongoose/lib/services/setDefaultsOnInsert.js | 96 |
1 files changed, 96 insertions, 0 deletions
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; +}; |