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.

applyTimestampsToUpdate.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. const get = require('../get');
  6. module.exports = applyTimestampsToUpdate;
  7. /*!
  8. * ignore
  9. */
  10. function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, options) {
  11. const updates = currentUpdate;
  12. let _updates = updates;
  13. const overwrite = get(options, 'overwrite', false);
  14. const timestamps = get(options, 'timestamps', true);
  15. // Support skipping timestamps at the query level, see gh-6980
  16. if (!timestamps || updates == null) {
  17. return currentUpdate;
  18. }
  19. if (overwrite) {
  20. if (currentUpdate && currentUpdate.$set) {
  21. currentUpdate = currentUpdate.$set;
  22. updates.$set = {};
  23. _updates = updates.$set;
  24. }
  25. if (updatedAt && !currentUpdate[updatedAt]) {
  26. _updates[updatedAt] = now;
  27. }
  28. if (createdAt && !currentUpdate[createdAt]) {
  29. _updates[createdAt] = now;
  30. }
  31. return updates;
  32. }
  33. updates.$set = updates.$set || {};
  34. currentUpdate = currentUpdate || {};
  35. if (updatedAt &&
  36. (!currentUpdate.$currentDate || !currentUpdate.$currentDate[updatedAt])) {
  37. updates.$set[updatedAt] = now;
  38. }
  39. if (createdAt) {
  40. if (currentUpdate[createdAt]) {
  41. delete currentUpdate[createdAt];
  42. }
  43. if (currentUpdate.$set && currentUpdate.$set[createdAt]) {
  44. delete currentUpdate.$set[createdAt];
  45. }
  46. updates.$setOnInsert = updates.$setOnInsert || {};
  47. updates.$setOnInsert[createdAt] = now;
  48. }
  49. if (Object.keys(updates.$set).length === 0) {
  50. delete updates.$set;
  51. }
  52. return updates;
  53. }