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