applyTimestampsToChildren.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. _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.startsWith('$'));
  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. });
  57. } else {
  58. if (updatedAt != null) {
  59. op[key][updatedAt] = now;
  60. }
  61. if (createdAt != null) {
  62. op[key][createdAt] = now;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. function applyTimestampsToDocumentArray(arr, schematype, now) {
  70. const timestamps = schematype.schema.options.timestamps;
  71. if (!timestamps) {
  72. return;
  73. }
  74. const len = arr.length;
  75. const createdAt = handleTimestampOption(timestamps, 'createdAt');
  76. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  77. for (let i = 0; i < len; ++i) {
  78. if (updatedAt != null) {
  79. arr[i][updatedAt] = now;
  80. }
  81. if (createdAt != null) {
  82. arr[i][createdAt] = now;
  83. }
  84. applyTimestampsToChildren(now, arr[i], schematype.schema);
  85. }
  86. }
  87. function applyTimestampsToSingleNested(subdoc, schematype, now) {
  88. const timestamps = schematype.schema.options.timestamps;
  89. if (!timestamps) {
  90. return;
  91. }
  92. const createdAt = handleTimestampOption(timestamps, 'createdAt');
  93. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  94. if (updatedAt != null) {
  95. subdoc[updatedAt] = now;
  96. }
  97. if (createdAt != null) {
  98. subdoc[createdAt] = now;
  99. }
  100. applyTimestampsToChildren(now, subdoc, schematype.schema);
  101. }
  102. function applyTimestampsToUpdateKey(schema, key, update, now) {
  103. // Replace positional operator `$` and array filters `$[]` and `$[.*]`
  104. const keyToSearch = cleanPositionalOperators(key);
  105. const path = schema.path(keyToSearch);
  106. if (!path) {
  107. return;
  108. }
  109. const parentSchemaTypes = [];
  110. const pieces = keyToSearch.split('.');
  111. for (let i = pieces.length - 1; i > 0; --i) {
  112. const s = schema.path(pieces.slice(0, i).join('.'));
  113. if (s != null &&
  114. (s.$isMongooseDocumentArray || s.$isSingleNested)) {
  115. parentSchemaTypes.push({ parentPath: key.split('.').slice(0, i).join('.'), parentSchemaType: s });
  116. }
  117. }
  118. if (Array.isArray(update[key]) && path.$isMongooseDocumentArray) {
  119. applyTimestampsToDocumentArray(update[key], path, now);
  120. } else if (update[key] && path.$isSingleNested) {
  121. applyTimestampsToSingleNested(update[key], path, now);
  122. } else if (parentSchemaTypes.length > 0) {
  123. for (const item of parentSchemaTypes) {
  124. const parentPath = item.parentPath;
  125. const parentSchemaType = item.parentSchemaType;
  126. const timestamps = parentSchemaType.schema.options.timestamps;
  127. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  128. if (!timestamps || updatedAt == null) {
  129. continue;
  130. }
  131. if (parentSchemaType.$isSingleNested) {
  132. // Single nested is easy
  133. update[parentPath + '.' + updatedAt] = now;
  134. } else if (parentSchemaType.$isMongooseDocumentArray) {
  135. let childPath = key.substring(parentPath.length + 1);
  136. if (/^\d+$/.test(childPath)) {
  137. update[parentPath + '.' + childPath][updatedAt] = now;
  138. continue;
  139. }
  140. const firstDot = childPath.indexOf('.');
  141. childPath = firstDot !== -1 ? childPath.substring(0, firstDot) : childPath;
  142. update[parentPath + '.' + childPath + '.' + updatedAt] = now;
  143. }
  144. }
  145. } else if (path.schema != null && path.schema != schema && update[key]) {
  146. const timestamps = path.schema.options.timestamps;
  147. const createdAt = handleTimestampOption(timestamps, 'createdAt');
  148. const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
  149. if (!timestamps) {
  150. return;
  151. }
  152. if (updatedAt != null) {
  153. update[key][updatedAt] = now;
  154. }
  155. if (createdAt != null) {
  156. update[key][createdAt] = now;
  157. }
  158. }
  159. }