Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

applyTimestampsToChildren.js 5.4KB

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