trackTransaction.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
  3. const sessionNewDocuments = require('../helpers/symbols').sessionNewDocuments;
  4. const utils = require('../utils');
  5. module.exports = function trackTransaction(schema) {
  6. schema.pre('save', function trackTransactionPreSave() {
  7. const session = this.$session();
  8. if (session == null) {
  9. return;
  10. }
  11. if (session.transaction == null || session[sessionNewDocuments] == null) {
  12. return;
  13. }
  14. if (!session[sessionNewDocuments].has(this)) {
  15. const initialState = {};
  16. if (this.isNew) {
  17. initialState.isNew = true;
  18. }
  19. if (this.$__schema.options.versionKey) {
  20. initialState.versionKey = this.get(this.$__schema.options.versionKey);
  21. }
  22. initialState.modifiedPaths = new Set(Object.keys(this.$__.activePaths.getStatePaths('modify')));
  23. initialState.atomics = _getAtomics(this);
  24. session[sessionNewDocuments].set(this, initialState);
  25. }
  26. });
  27. };
  28. function _getAtomics(doc, previous) {
  29. const pathToAtomics = new Map();
  30. previous = previous || new Map();
  31. const pathsToCheck = Object.keys(doc.$__.activePaths.init).concat(Object.keys(doc.$__.activePaths.modify));
  32. for (const path of pathsToCheck) {
  33. const val = doc.$__getValue(path);
  34. if (Array.isArray(val) &&
  35. utils.isMongooseDocumentArray(val) &&
  36. val.length &&
  37. val[arrayAtomicsSymbol] != null &&
  38. utils.hasOwnKeys(val[arrayAtomicsSymbol])) {
  39. const existing = previous.get(path) || {};
  40. pathToAtomics.set(path, mergeAtomics(existing, val[arrayAtomicsSymbol]));
  41. }
  42. }
  43. const dirty = doc.$__dirty();
  44. for (const dirt of dirty) {
  45. const path = dirt.path;
  46. const val = dirt.value;
  47. if (val?.[arrayAtomicsSymbol] != null && utils.hasOwnKeys(val[arrayAtomicsSymbol])) {
  48. const existing = previous.get(path) || {};
  49. pathToAtomics.set(path, mergeAtomics(existing, val[arrayAtomicsSymbol]));
  50. }
  51. }
  52. return pathToAtomics;
  53. }
  54. function mergeAtomics(destination, source) {
  55. destination = destination || {};
  56. if (source.$pullAll != null) {
  57. destination.$pullAll = (destination.$pullAll || []).concat(source.$pullAll);
  58. }
  59. if (source.$push != null) {
  60. destination.$push = destination.$push || {};
  61. destination.$push.$each = (destination.$push.$each || []).concat(source.$push.$each);
  62. }
  63. if (source.$addToSet != null) {
  64. destination.$addToSet = (destination.$addToSet || []).concat(source.$addToSet);
  65. }
  66. if (source.$set != null) {
  67. destination.$set = Array.isArray(source.$set) ? [...source.$set] : Object.assign({}, source.$set);
  68. }
  69. return destination;
  70. }