This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -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;
}
}
}

View File

@@ -3,7 +3,7 @@
const castFilterPath = require('../query/castFilterPath');
const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
const getPath = require('../schema/getPath');
const modifiedPaths = require('./modifiedPaths');
const updatedPathsByArrayFilter = require('./updatedPathsByArrayFilter');
module.exports = function castArrayFilters(query) {
const arrayFilters = query.options.arrayFilters;
@@ -15,31 +15,13 @@ module.exports = function castArrayFilters(query) {
const schema = query.schema;
const strictQuery = schema.options.strictQuery;
const updatedPaths = modifiedPaths(update);
const updatedPathsByFilter = Object.keys(updatedPaths).reduce((cur, path) => {
const matches = path.match(/\$\[[^\]]+\]/g);
if (matches == null) {
return cur;
}
for (const match of matches) {
const firstMatch = path.indexOf(match);
if (firstMatch !== path.lastIndexOf(match)) {
throw new Error(`Path '${path}' contains the same array filter multiple times`);
}
cur[match.substring(2, match.length - 1)] = path.
substr(0, firstMatch - 1).
replace(/\$\[[^\]]+\]/g, '0');
}
return cur;
}, {});
const updatedPathsByFilter = updatedPathsByArrayFilter(update);
for (const filter of arrayFilters) {
if (filter == null) {
throw new Error(`Got null array filter in ${arrayFilters}`);
}
for (const key in filter) {
for (const key of Object.keys(filter)) {
if (filter[key] == null) {
continue;
}