Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

castArrayFilters.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. const castFilterPath = require('../query/castFilterPath');
  3. const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
  4. const getPath = require('../schema/getPath');
  5. const modifiedPaths = require('./modifiedPaths');
  6. module.exports = function castArrayFilters(query) {
  7. const arrayFilters = query.options.arrayFilters;
  8. if (!Array.isArray(arrayFilters)) {
  9. return;
  10. }
  11. const update = query.getUpdate();
  12. const schema = query.schema;
  13. const strictQuery = schema.options.strictQuery;
  14. const updatedPaths = modifiedPaths(update);
  15. const updatedPathsByFilter = Object.keys(updatedPaths).reduce((cur, path) => {
  16. const matches = path.match(/\$\[[^\]]+\]/g);
  17. if (matches == null) {
  18. return cur;
  19. }
  20. for (const match of matches) {
  21. const firstMatch = path.indexOf(match);
  22. if (firstMatch !== path.lastIndexOf(match)) {
  23. throw new Error(`Path '${path}' contains the same array filter multiple times`);
  24. }
  25. cur[match.substring(2, match.length - 1)] = path.
  26. substr(0, firstMatch - 1).
  27. replace(/\$\[[^\]]+\]/g, '0');
  28. }
  29. return cur;
  30. }, {});
  31. for (const filter of arrayFilters) {
  32. if (filter == null) {
  33. throw new Error(`Got null array filter in ${arrayFilters}`);
  34. }
  35. const firstKey = Object.keys(filter)[0];
  36. if (filter[firstKey] == null) {
  37. continue;
  38. }
  39. const dot = firstKey.indexOf('.');
  40. let filterPath = dot === -1 ?
  41. updatedPathsByFilter[firstKey] + '.0' :
  42. updatedPathsByFilter[firstKey.substr(0, dot)] + '.0' + firstKey.substr(dot);
  43. if (filterPath == null) {
  44. throw new Error(`Filter path not found for ${firstKey}`);
  45. }
  46. // If there are multiple array filters in the path being updated, make sure
  47. // to replace them so we can get the schema path.
  48. filterPath = cleanPositionalOperators(filterPath);
  49. const schematype = getPath(schema, filterPath);
  50. if (schematype == null) {
  51. if (!strictQuery) {
  52. return;
  53. }
  54. // For now, treat `strictQuery = true` and `strictQuery = 'throw'` as
  55. // equivalent for casting array filters. `strictQuery = true` doesn't
  56. // quite work in this context because we never want to silently strip out
  57. // array filters, even if the path isn't in the schema.
  58. throw new Error(`Could not find path "${filterPath}" in schema`);
  59. }
  60. if (typeof filter[firstKey] === 'object') {
  61. filter[firstKey] = castFilterPath(query, schematype, filter[firstKey]);
  62. } else {
  63. filter[firstKey] = schematype.castForQuery(filter[firstKey]);
  64. }
  65. }
  66. };