applyTimestampsToChildren.js 5.2 KB

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