1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-12 02:42:48 +00:00

updated bunch of file paths and changed the way posts are loaded

This commit is contained in:
2016-01-05 12:28:04 -06:00
parent 4bb8cae81e
commit 6ab45fe935
13249 changed files with 317868 additions and 2101398 deletions

View File

@@ -7,7 +7,7 @@ var ValidationError = require('../error/validation.js');
var ObjectId = require('../types/objectid');
/**
* Applies validators and defaults to update and fineOneAndUpdate operations,
* Applies validators and defaults to update and findOneAndUpdate operations,
* specifically passing a null doc as `this` to validators and defaults
*
* @param {Query} query
@@ -24,25 +24,30 @@ module.exports = function(query, schema, castedDoc, options) {
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[paths[j]] = flat[paths[j]];
updatedValues[updatedPath] = flat[paths[j]];
} else if (keys[i] === '$unset') {
updatedValues[paths[j]] = undefined;
updatedValues[updatedPath] = undefined;
}
updatedKeys[paths[j]] = true;
updatedKeys[updatedPath] = true;
}
hasDollarUpdate = true;
}
}
if (!hasDollarUpdate) {
modifiedPaths(castedDoc, '', modified);
updatedValues = flatten(castedDoc);
updatedKeys = Object.keys(updatedValues);
}
@@ -50,12 +55,14 @@ module.exports = function(query, schema, castedDoc, options) {
if (options && options.upsert) {
paths = Object.keys(query._conditions);
numPaths = keys.length;
for (var i = 0; i < numPaths; ++i) {
if (typeof query._conditions[paths[i]] === 'object') {
var conditionKeys = Object.keys(query._conditions[paths[i]]);
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) {
for (j = 0; j < numConditionKeys; ++j) {
if (conditionKeys[j].charAt(0) === '$') {
hasDollarKey = true;
break;
@@ -65,7 +72,8 @@ module.exports = function(query, schema, castedDoc, options) {
continue;
}
}
updatedKeys[paths[i]] = true;
updatedKeys[path] = true;
modified[path] = true;
}
if (options.setDefaultsOnInsert) {
@@ -74,11 +82,29 @@ module.exports = function(query, schema, castedDoc, options) {
// Ignore _id for now because it causes bugs in 2.4
return;
}
var def = schemaType.getDefault(null, true);
if (!updatedKeys[path] && typeof def !== 'undefined') {
castedDoc.$setOnInsert = castedDoc.$setOnInsert || {};
castedDoc.$setOnInsert[path] = def;
updatedValues[path] = def;
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.$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.$setOnInsert = castedDoc.$setOnInsert || {};
castedDoc.$setOnInsert[path] = def;
updatedValues[path] = def;
}
}
});
}
@@ -88,19 +114,21 @@ module.exports = function(query, schema, castedDoc, options) {
var numUpdates = updates.length;
var validatorsToExecute = [];
var validationErrors = [];
for (var i = 0; i < numUpdates; ++i) {
for (i = 0; i < numUpdates; ++i) {
(function(i) {
if (schema.path(updates[i])) {
var schemaPath = schema._getSchema(updates[i]);
if (schemaPath) {
validatorsToExecute.push(function(callback) {
schema.path(updates[i]).doValidate(
schemaPath.doValidate(
updatedValues[updates[i]],
function(err) {
if (err) {
err.path = updates[i];
validationErrors.push(err);
}
callback(null);
},
null);
options && options.context === 'query' ? query : null);
});
}
})(i);
@@ -120,6 +148,25 @@ module.exports = function(query, schema, castedDoc, options) {
};
};
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 (shouldFlatten(val)) {
modifiedPaths(val, path + key, result);
}
}
return result;
}
function flatten(update, path) {
var keys = Object.keys(update);
var numKeys = keys.length;
@@ -143,5 +190,10 @@ function flatten(update, path) {
}
function shouldFlatten(val) {
return val && typeof val === 'object' && !(val instanceof ObjectId);
return val &&
typeof val === 'object' &&
!(val instanceof Date) &&
!(val instanceof ObjectId) &&
(!Array.isArray(val) || val.length > 0) &&
!(val instanceof Buffer);
}