applyTimestampsToChildren.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
  3. const handleTimestampOption = require('../schema/handleTimestampOption');
  4. module.exports = applyTimestampsToChildren;
  5. /*!
  6. * ignore
  7. */
  8. function applyTimestampsToChildren(now, update, schema) {
  9. if (update == null) {
  10. return;
  11. }
  12. const keys = Object.keys(update);
  13. const hasDollarKey = keys.some(key => key[0] === '$');
  14. if (hasDollarKey) {
  15. if (update.$push) {
  16. _applyTimestampToUpdateOperator(update.$push);
  17. }
  18. if (update.$addToSet) {
  19. _applyTimestampToUpdateOperator(update.$addToSet);
  20. }
  21. if (update.$set != null) {
  22. const keys = Object.keys(update.$set);
  23. for (const key of keys) {
  24. applyTimestampsToUpdateKey(schema, key, update.$set, now);
  25. }
  26. }
  27. if (update.$setOnInsert != null) {
  28. const keys = Object.keys(update.$setOnInsert);
  29. for (const key of keys) {
  30. applyTimestampsToUpdateKey(schema, key, update.$setOnInsert, now);
  31. }
  32. }
  33. }
  34. const updateKeys = Object.keys(update).filter(key => key[0] !== '$');
  35. for (const key of updateKeys) {
  36. applyTimestampsToUpdateKey(schema, key, update, now);
  37. }
  38. function _applyTimestampToUpdateOperator(op) {
  39. for (const key of Object.keys(op)) {
  40. const $path = schema.path(key.replace(/\.\$\./i, '.').replace(/.\$$/, ''));
  41. if (op[key] &&
  42. $path &&
  43. $path.$isMongooseDocumentArray &&
  44. $path.schema.options.timestamps) {
  45. const timestamps = $path.schema.options.timestamps;
  46. const createdAt = handleTimestampOption(timestamps, 'createdAt');
  47. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  48. if (op[key].$each) {
  49. op[key].$each.forEach(function(subdoc) {
  50. if (updatedAt != null) {
  51. subdoc[updatedAt] = now;
  52. }
  53. if (createdAt != null) {
  54. subdoc[createdAt] = now;
  55. }
  56. applyTimestampsToChildren(now, subdoc, $path.schema);
  57. });
  58. } else {
  59. if (updatedAt != null) {
  60. op[key][updatedAt] = now;
  61. }
  62. if (createdAt != null) {
  63. op[key][createdAt] = now;
  64. }
  65. applyTimestampsToChildren(now, op[key], $path.schema);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. function applyTimestampsToDocumentArray(arr, schematype, now) {
  72. const timestamps = schematype.schema.options.timestamps;
  73. if (!timestamps) {
  74. return;
  75. }
  76. const len = arr.length;
  77. const createdAt = handleTimestampOption(timestamps, 'createdAt');
  78. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  79. for (let i = 0; i < len; ++i) {
  80. if (updatedAt != null) {
  81. arr[i][updatedAt] = now;
  82. }
  83. if (createdAt != null) {
  84. arr[i][createdAt] = now;
  85. }
  86. applyTimestampsToChildren(now, arr[i], schematype.schema);
  87. }
  88. }
  89. function applyTimestampsToSingleNested(subdoc, schematype, now) {
  90. const timestamps = schematype.schema.options.timestamps;
  91. if (!timestamps) {
  92. return;
  93. }
  94. const createdAt = handleTimestampOption(timestamps, 'createdAt');
  95. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  96. if (updatedAt != null) {
  97. subdoc[updatedAt] = now;
  98. }
  99. if (createdAt != null) {
  100. subdoc[createdAt] = now;
  101. }
  102. applyTimestampsToChildren(now, subdoc, schematype.schema);
  103. }
  104. function applyTimestampsToUpdateKey(schema, key, update, now) {
  105. // Replace positional operator `$` and array filters `$[]` and `$[.*]`
  106. const keyToSearch = cleanPositionalOperators(key);
  107. const path = schema.path(keyToSearch);
  108. if (!path) {
  109. return;
  110. }
  111. const parentSchemaTypes = [];
  112. const pieces = keyToSearch.split('.');
  113. for (let i = pieces.length - 1; i > 0; --i) {
  114. const s = schema.path(pieces.slice(0, i).join('.'));
  115. if (s != null &&
  116. (s.$isMongooseDocumentArray || s.$isSingleNested)) {
  117. parentSchemaTypes.push({ parentPath: key.split('.').slice(0, i).join('.'), parentSchemaType: s });
  118. }
  119. }
  120. if (Array.isArray(update[key]) && path.$isMongooseDocumentArray) {
  121. applyTimestampsToDocumentArray(update[key], path, now);
  122. } else if (update[key] && path.$isSingleNested) {
  123. applyTimestampsToSingleNested(update[key], path, now);
  124. } else if (parentSchemaTypes.length > 0) {
  125. for (const item of parentSchemaTypes) {
  126. const parentPath = item.parentPath;
  127. const parentSchemaType = item.parentSchemaType;
  128. const timestamps = parentSchemaType.schema.options.timestamps;
  129. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  130. if (!timestamps || updatedAt == null) {
  131. continue;
  132. }
  133. if (parentSchemaType.$isSingleNested) {
  134. // Single nested is easy
  135. update[parentPath + '.' + updatedAt] = now;
  136. } else if (parentSchemaType.$isMongooseDocumentArray) {
  137. let childPath = key.substring(parentPath.length + 1);
  138. if (/^\d+$/.test(childPath)) {
  139. update[parentPath + '.' + childPath][updatedAt] = now;
  140. continue;
  141. }
  142. const firstDot = childPath.indexOf('.');
  143. childPath = firstDot !== -1 ? childPath.substring(0, firstDot) : childPath;
  144. update[parentPath + '.' + childPath + '.' + updatedAt] = now;
  145. }
  146. }
  147. } else if (path.schema != null && path.schema != schema && update[key]) {
  148. const timestamps = path.schema.options.timestamps;
  149. const createdAt = handleTimestampOption(timestamps, 'createdAt');
  150. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  151. if (!timestamps) {
  152. return;
  153. }
  154. if (updatedAt != null) {
  155. update[key][updatedAt] = now;
  156. }
  157. if (createdAt != null) {
  158. update[key][createdAt] = now;
  159. }
  160. }
  161. }