database and ajax test added
This commit is contained in:
+173
@@ -0,0 +1,173 @@
|
||||
'use strict';
|
||||
|
||||
const handleTimestampOption = require('../schema/handleTimestampOption');
|
||||
|
||||
module.exports = applyTimestampsToChildren;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function applyTimestampsToChildren(now, update, schema) {
|
||||
if (update == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const keys = Object.keys(update);
|
||||
let key;
|
||||
let createdAt;
|
||||
let updatedAt;
|
||||
let timestamps;
|
||||
let path;
|
||||
|
||||
const hasDollarKey = keys.length && keys[0].charAt(0) === '$';
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (update.$set != null) {
|
||||
const keys = Object.keys(update.$set);
|
||||
for (key of keys) {
|
||||
// Replace positional operator `$` and array filters `$[]` and `$[.*]`
|
||||
const keyToSearch = key.
|
||||
replace(/\.\$(\[[^\]]*\])?\./g, '.0.').
|
||||
replace(/\.(\[[^\]]*\])?\$$/, '.0');
|
||||
path = schema.path(keyToSearch);
|
||||
if (!path) {
|
||||
continue;
|
||||
}
|
||||
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 (path.$parentSchema !== schema && path.$parentSchema != null) {
|
||||
const parentPath = path.$parentSchema.$schemaType;
|
||||
|
||||
if (parentPath == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
timestamps = parentPath.schema.options.timestamps;
|
||||
createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
|
||||
if (updatedAt == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parentPath.$isSingleNested) {
|
||||
// Single nested is easy
|
||||
update.$set[parentPath.path + '.' + updatedAt] = now;
|
||||
continue;
|
||||
}
|
||||
|
||||
let childPath = key.substr(parentPath.path.length + 1);
|
||||
const firstDot = childPath.indexOf('.');
|
||||
|
||||
// Shouldn't happen, but if it does ignore this path
|
||||
if (firstDot === -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
childPath = childPath.substr(0, firstDot);
|
||||
|
||||
update.$set[parentPath.path + '.' + childPath + '.' + updatedAt] = now;
|
||||
} else if (path.schema != null && path.schema != schema) {
|
||||
timestamps = path.schema.options.timestamps;
|
||||
createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
|
||||
if (updatedAt != null) {
|
||||
update.$set[key][updatedAt] = now;
|
||||
}
|
||||
if (createdAt != null) {
|
||||
update.$set[key][createdAt] = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const keys = Object.keys(update).filter(key => !key.startsWith('$'));
|
||||
for (key of keys) {
|
||||
// Replace positional operator `$` and array filters `$[]` and `$[.*]`
|
||||
const keyToSearch = key.
|
||||
replace(/\.\$(\[[^\]]*\])?\./g, '.0.').
|
||||
replace(/\.(\[[^\]]*\])?\$$/, '.0');
|
||||
path = schema.path(keyToSearch);
|
||||
if (!path) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Array.isArray(update[key]) && path.$isMongooseDocumentArray) {
|
||||
applyTimestampsToDocumentArray(update[key], path, now);
|
||||
} else if (update[key] != null && path.$isSingleNested) {
|
||||
applyTimestampsToSingleNested(update[key], path, now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyTimestampsToDocumentArray(arr, schematype, now) {
|
||||
const timestamps = schematype.schema.options.timestamps;
|
||||
|
||||
if (!timestamps) {
|
||||
return;
|
||||
}
|
||||
|
||||
const len = arr.length;
|
||||
|
||||
const createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
for (let i = 0; i < len; ++i) {
|
||||
if (updatedAt != null) {
|
||||
arr[i][updatedAt] = now;
|
||||
}
|
||||
if (createdAt != null) {
|
||||
arr[i][createdAt] = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyTimestampsToSingleNested(subdoc, schematype, now) {
|
||||
const timestamps = schematype.schema.options.timestamps;
|
||||
if (!timestamps) {
|
||||
return;
|
||||
}
|
||||
|
||||
const createdAt = handleTimestampOption(timestamps, 'createdAt');
|
||||
const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
||||
if (updatedAt != null) {
|
||||
subdoc[updatedAt] = now;
|
||||
}
|
||||
if (createdAt != null) {
|
||||
subdoc[createdAt] = now;
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
const get = require('../get');
|
||||
|
||||
module.exports = applyTimestampsToUpdate;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, options) {
|
||||
const updates = currentUpdate;
|
||||
let _updates = updates;
|
||||
const overwrite = get(options, 'overwrite', false);
|
||||
const timestamps = get(options, 'timestamps', true);
|
||||
|
||||
// Support skipping timestamps at the query level, see gh-6980
|
||||
if (!timestamps || updates == null) {
|
||||
return currentUpdate;
|
||||
}
|
||||
|
||||
if (overwrite) {
|
||||
if (currentUpdate && currentUpdate.$set) {
|
||||
currentUpdate = currentUpdate.$set;
|
||||
updates.$set = {};
|
||||
_updates = updates.$set;
|
||||
}
|
||||
if (updatedAt && !currentUpdate[updatedAt]) {
|
||||
_updates[updatedAt] = now;
|
||||
}
|
||||
if (createdAt && !currentUpdate[createdAt]) {
|
||||
_updates[createdAt] = now;
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
updates.$set = updates.$set || {};
|
||||
currentUpdate = currentUpdate || {};
|
||||
|
||||
if (updatedAt &&
|
||||
(!currentUpdate.$currentDate || !currentUpdate.$currentDate[updatedAt])) {
|
||||
updates.$set[updatedAt] = now;
|
||||
}
|
||||
|
||||
if (createdAt) {
|
||||
if (currentUpdate[createdAt]) {
|
||||
delete currentUpdate[createdAt];
|
||||
}
|
||||
if (currentUpdate.$set && currentUpdate.$set[createdAt]) {
|
||||
delete currentUpdate.$set[createdAt];
|
||||
}
|
||||
|
||||
updates.$setOnInsert = updates.$setOnInsert || {};
|
||||
updates.$setOnInsert[createdAt] = now;
|
||||
}
|
||||
|
||||
if (Object.keys(updates.$set).length === 0) {
|
||||
delete updates.$set;
|
||||
}
|
||||
|
||||
return updates;
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
const _modifiedPaths = require('../common').modifiedPaths;
|
||||
|
||||
/**
|
||||
* Given an update document with potential update operators (`$set`, etc.)
|
||||
* returns an object whose keys are the directly modified paths.
|
||||
*
|
||||
* If there are any top-level keys that don't start with `$`, we assume those
|
||||
* will get wrapped in a `$set`. The Mongoose Query is responsible for wrapping
|
||||
* top-level keys in `$set`.
|
||||
*
|
||||
* @param {Object} update
|
||||
* @return {Object} modified
|
||||
*/
|
||||
|
||||
module.exports = function modifiedPaths(update) {
|
||||
const keys = Object.keys(update);
|
||||
const res = {};
|
||||
|
||||
const withoutDollarKeys = {};
|
||||
for (const key of keys) {
|
||||
if (key.startsWith('$')) {
|
||||
_modifiedPaths(update[key], '', res);
|
||||
continue;
|
||||
}
|
||||
withoutDollarKeys[key] = update[key];
|
||||
}
|
||||
|
||||
_modifiedPaths(withoutDollarKeys, '', res);
|
||||
|
||||
return res;
|
||||
};
|
||||
Reference in New Issue
Block a user