All files / mongoose/lib/plugins trackTransaction.js

17.39% Statements 8/46
1.96% Branches 1/51
50% Functions 2/4
17.39% Lines 8/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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