common.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const Binary = require('mongodb/lib/bson').Binary;
  6. const isBsonType = require('./isBsonType');
  7. const isMongooseObject = require('./isMongooseObject');
  8. const MongooseError = require('../error');
  9. const util = require('util');
  10. exports.flatten = flatten;
  11. exports.modifiedPaths = modifiedPaths;
  12. /*!
  13. * ignore
  14. */
  15. function flatten(update, path, options, schema) {
  16. let keys;
  17. if (update && isMongooseObject(update) && !Buffer.isBuffer(update)) {
  18. keys = Object.keys(update.toObject({ transform: false, virtuals: false }) || {});
  19. } else {
  20. keys = Object.keys(update || {});
  21. }
  22. const numKeys = keys.length;
  23. const result = {};
  24. path = path ? path + '.' : '';
  25. for (let i = 0; i < numKeys; ++i) {
  26. const key = keys[i];
  27. const val = update[key];
  28. result[path + key] = val;
  29. // Avoid going into mixed paths if schema is specified
  30. const keySchema = schema?.path?.(path + key);
  31. const isNested = schema?.nested?.[path + key];
  32. if (keySchema?.instance === 'Mixed') continue;
  33. if (shouldFlatten(val)) {
  34. if (options?.skipArrays && Array.isArray(val)) {
  35. continue;
  36. }
  37. const flat = flatten(val, path + key, options, schema);
  38. for (const k in flat) {
  39. result[k] = flat[k];
  40. }
  41. if (Array.isArray(val)) {
  42. result[path + key] = val;
  43. }
  44. }
  45. if (isNested) {
  46. const paths = Object.keys(schema.paths);
  47. for (const p of paths) {
  48. if (p.startsWith(path + key + '.') && !Object.hasOwn(result, p)) {
  49. result[p] = void 0;
  50. }
  51. }
  52. }
  53. }
  54. return result;
  55. }
  56. /*!
  57. * ignore
  58. */
  59. function modifiedPaths(update, path, result, recursion = null) {
  60. if (update == null || typeof update !== 'object') {
  61. return;
  62. }
  63. if (recursion == null) {
  64. recursion = {
  65. raw: { update, path },
  66. trace: new WeakSet()
  67. };
  68. }
  69. if (recursion.trace.has(update)) {
  70. throw new MongooseError(`a circular reference in the update value, updateValue:
  71. ${util.inspect(recursion.raw.update, { showHidden: false, depth: 1 })}
  72. updatePath: '${recursion.raw.path}'`);
  73. }
  74. recursion.trace.add(update);
  75. const keys = Object.keys(update || {});
  76. const numKeys = keys.length;
  77. result = result || {};
  78. path = path ? path + '.' : '';
  79. for (let i = 0; i < numKeys; ++i) {
  80. const key = keys[i];
  81. let val = update[key];
  82. const _path = path + key;
  83. result[_path] = true;
  84. if (!Buffer.isBuffer(val) && isMongooseObject(val)) {
  85. val = val.toObject({ transform: false, virtuals: false });
  86. }
  87. if (shouldFlatten(val)) {
  88. modifiedPaths(val, path + key, result, recursion);
  89. }
  90. }
  91. recursion.trace.delete(update);
  92. return result;
  93. }
  94. /*!
  95. * ignore
  96. */
  97. function shouldFlatten(val) {
  98. return val &&
  99. typeof val === 'object' &&
  100. !(val instanceof Date) &&
  101. !isBsonType(val, 'ObjectId') &&
  102. (!Array.isArray(val) || val.length !== 0) &&
  103. !(val instanceof Buffer) &&
  104. !isBsonType(val, 'Decimal128') &&
  105. !(val instanceof Binary);
  106. }