trackTransaction.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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() {
  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.states.modify));
  23. initialState.atomics = _getAtomics(this);
  24. session[sessionNewDocuments].set(this, initialState);
  25. } else {
  26. const state = session[sessionNewDocuments].get(this);
  27. for (const path of Object.keys(this.$__.activePaths.states.modify)) {
  28. state.modifiedPaths.add(path);
  29. }
  30. state.atomics = _getAtomics(this, state.atomics);
  31. }
  32. });
  33. };
  34. function _getAtomics(doc, previous) {
  35. const pathToAtomics = new Map();
  36. previous = previous || new Map();
  37. const pathsToCheck = Object.keys(doc.$__.activePaths.init).concat(Object.keys(doc.$__.activePaths.modify));
  38. for (const path of pathsToCheck) {
  39. const val = doc.$__getValue(path);
  40. if (val != null &&
  41. Array.isArray(val) &&
  42. utils.isMongooseDocumentArray(val) &&
  43. val.length &&
  44. val[arrayAtomicsSymbol] != null &&
  45. Object.keys(val[arrayAtomicsSymbol]).length !== 0) {
  46. const existing = previous.get(path) || {};
  47. pathToAtomics.set(path, mergeAtomics(existing, val[arrayAtomicsSymbol]));
  48. }
  49. }
  50. const dirty = doc.$__dirty();
  51. for (const dirt of dirty) {
  52. const path = dirt.path;
  53. const val = dirt.value;
  54. if (val != null && val[arrayAtomicsSymbol] != null && Object.keys(val[arrayAtomicsSymbol]).length !== 0) {
  55. const existing = previous.get(path) || {};
  56. pathToAtomics.set(path, mergeAtomics(existing, val[arrayAtomicsSymbol]));
  57. }
  58. }
  59. return pathToAtomics;
  60. }
  61. function mergeAtomics(destination, source) {
  62. destination = destination || {};
  63. if (source.$pullAll != null) {
  64. destination.$pullAll = (destination.$pullAll || []).concat(source.$pullAll);
  65. }
  66. if (source.$push != null) {
  67. destination.$push = destination.$push || {};
  68. destination.$push.$each = (destination.$push.$each || []).concat(source.$push.$each);
  69. }
  70. if (source.$addToSet != null) {
  71. destination.$addToSet = (destination.$addToSet || []).concat(source.$addToSet);
  72. }
  73. if (source.$set != null) {
  74. destination.$set = Object.assign(destination.$set || {}, source.$set);
  75. }
  76. return destination;
  77. }