setDefaultsOnInsert.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. const get = require('./get');
  3. /**
  4. * Applies defaults to update and findOneAndUpdate operations.
  5. *
  6. * @param {Object} filter
  7. * @param {Schema} schema
  8. * @param {Object} castedDoc
  9. * @param {Object} options
  10. * @method setDefaultsOnInsert
  11. * @api private
  12. */
  13. module.exports = function(filter, schema, castedDoc, options) {
  14. options = options || {};
  15. const shouldSetDefaultsOnInsert = options.setDefaultsOnInsert ?? schema.base.options.setDefaultsOnInsert;
  16. if (!options.upsert || shouldSetDefaultsOnInsert === false) {
  17. return castedDoc;
  18. }
  19. const keys = Object.keys(castedDoc || {});
  20. const updatedKeys = {};
  21. const updatedValues = {};
  22. const numKeys = keys.length;
  23. let hasDollarUpdate = false;
  24. for (let i = 0; i < numKeys; ++i) {
  25. if (keys[i].charAt(0) === '$') {
  26. hasDollarUpdate = true;
  27. break;
  28. }
  29. }
  30. const paths = Object.keys(filter);
  31. const numPaths = paths.length;
  32. for (let i = 0; i < numPaths; ++i) {
  33. const path = paths[i];
  34. const condition = filter[path];
  35. if (condition && typeof condition === 'object') {
  36. const conditionKeys = Object.keys(condition);
  37. const numConditionKeys = conditionKeys.length;
  38. let hasDollarKey = false;
  39. for (let j = 0; j < numConditionKeys; ++j) {
  40. if (conditionKeys[j].charAt(0) === '$') {
  41. hasDollarKey = true;
  42. break;
  43. }
  44. }
  45. if (hasDollarKey) {
  46. continue;
  47. }
  48. }
  49. updatedKeys[path] = true;
  50. }
  51. if (options?.overwrite && !hasDollarUpdate) {
  52. // Defaults will be set later, since we're overwriting we'll cast
  53. // the whole update to a document
  54. return castedDoc;
  55. }
  56. schema.eachPath(function(path, schemaType) {
  57. // Skip single nested paths if underneath a map
  58. if (schemaType.path === '_id' && schemaType.options.auto) {
  59. return;
  60. }
  61. const def = schemaType.getDefault(null, true);
  62. if (typeof def === 'undefined') {
  63. return;
  64. }
  65. const pathPieces = schemaType.splitPath();
  66. if (pathPieces.includes('$*')) {
  67. // Skip defaults underneath maps. We should never do `$setOnInsert` on a path with `$*`
  68. return;
  69. }
  70. if (isModified(castedDoc, updatedKeys, path, pathPieces, hasDollarUpdate)) {
  71. return;
  72. }
  73. castedDoc = castedDoc || {};
  74. castedDoc.$setOnInsert = castedDoc.$setOnInsert || {};
  75. if (get(castedDoc, path) == null) {
  76. castedDoc.$setOnInsert[path] = def;
  77. }
  78. updatedValues[path] = def;
  79. });
  80. return castedDoc;
  81. };
  82. function isModified(castedDoc, updatedKeys, path, pathPieces, hasDollarUpdate) {
  83. // Check if path is in filter (updatedKeys)
  84. if (updatedKeys[path]) {
  85. return true;
  86. }
  87. // Check if any parent path is in filter
  88. let cur = pathPieces[0];
  89. for (let i = 1; i < pathPieces.length; ++i) {
  90. if (updatedKeys[cur]) {
  91. return true;
  92. }
  93. cur += '.' + pathPieces[i];
  94. }
  95. // Check if path is modified in the update
  96. if (hasDollarUpdate) {
  97. // Check each update operator
  98. for (const key in castedDoc) {
  99. if (key.charAt(0) === '$') {
  100. if (pathExistsInUpdate(castedDoc[key], path, pathPieces)) {
  101. return true;
  102. }
  103. }
  104. }
  105. } else {
  106. // No dollar operators, check the castedDoc directly
  107. if (pathExistsInUpdate(castedDoc, path, pathPieces)) {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. function pathExistsInUpdate(update, targetPath, pathPieces) {
  114. if (update == null || typeof update !== 'object') {
  115. return false;
  116. }
  117. // Check exact match
  118. if (Object.hasOwn(update, targetPath)) {
  119. return true;
  120. }
  121. // Check if any parent path exists
  122. let cur = pathPieces[0];
  123. for (let i = 1; i < pathPieces.length; ++i) {
  124. if (Object.hasOwn(update, cur)) {
  125. return true;
  126. }
  127. cur += '.' + pathPieces[i];
  128. }
  129. // Check if any child path exists (e.g., path is "a" and update has "a.b")
  130. const prefix = targetPath + '.';
  131. for (const key in update) {
  132. if (key.startsWith(prefix)) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }