Cleanup
This commit is contained in:
230
node_modules/mongoose/lib/helpers/update/applyTimestampsToChildren.js
generated
vendored
230
node_modules/mongoose/lib/helpers/update/applyTimestampsToChildren.js
generated
vendored
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user