applyTimestampsToChildren.js 5.1 KB

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