All files / mongoose/lib/helpers/document applyDefaults.js

59.45% Statements 44/74
49.35% Branches 38/77
100% Functions 2/2
58.33% Lines 42/72

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130    1x   1x 36x 36x 36x   36x   156x 156x   156x       156x 156x 156x 156x 156x 156x 192x 13x     179x 179x   179x       179x                 179x 143x 44x     99x 99x 19x 2x   17x 1x   80x   29x       67x       67x                                                                 67x 67x           67x 16x 16x       36x                     16x 16x        
'use strict';
 
const isNestedProjection = require('../projection/isNestedProjection');
 
module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip, options) {
  const paths = Object.keys(doc.$__schema.paths);
  const plen = paths.length;
  const skipParentChangeTracking = options && options.skipParentChangeTracking;
 
  for (let i = 0; i < plen; ++i) {
    let def;
    let curPath = '';
    const p = paths[i];
 
    Iif (p === '_id' && doc.$__.skipId) {
      continue;
    }
 
    const type = doc.$__schema.paths[p];
    const path = type.splitPath();
    const len = path.length;
    let included = false;
    let doc_ = doc._doc;
    for (let j = 0; j < len; ++j) {
      if (doc_ == null) {
        break;
      }
 
      const piece = path[j];
      curPath += (!curPath.length ? '' : '.') + piece;
 
      Iif (exclude === true) {
        if (curPath in fields) {
          break;
        }
      } else Iif (exclude === false && fields && !included) {
        const hasSubpaths = type.$isSingleNested || type.$isMongooseDocumentArray;
        if ((curPath in fields && !isNestedProjection(fields[curPath])) || (j === len - 1 && hasSubpaths && hasIncludedChildren != null && hasIncludedChildren[curPath])) {
          included = true;
        } else if (hasIncludedChildren != null && !hasIncludedChildren[curPath]) {
          break;
        }
      }
 
      if (j === len - 1) {
        if (doc_[piece] !== void 0) {
          break;
        }
 
        Eif (isBeforeSetters != null) {
          if (typeof type.defaultValue === 'function') {
            if (!type.defaultValue.$runBeforeSetters && isBeforeSetters) {
              break;
            }
            if (type.defaultValue.$runBeforeSetters && !isBeforeSetters) {
              break;
            }
          } else if (!isBeforeSetters) {
            // Non-function defaults should always run **before** setters
            continue;
          }
        }
 
        Iif (pathsToSkip && pathsToSkip[curPath]) {
          break;
        }
 
        Iif (fields && exclude !== null) {
          if (exclude === true) {
            // apply defaults to all non-excluded fields
            if (p in fields) {
              continue;
            }
 
            try {
              def = type.getDefault(doc, false);
            } catch (err) {
              doc.invalidate(p, err);
              break;
            }
 
            if (typeof def !== 'undefined') {
              doc_[piece] = def;
              applyChangeTracking(doc, p, skipParentChangeTracking);
            }
          } else if (included) {
            // selected field
            try {
              def = type.getDefault(doc, false);
            } catch (err) {
              doc.invalidate(p, err);
              break;
            }
 
            if (typeof def !== 'undefined') {
              doc_[piece] = def;
              applyChangeTracking(doc, p, skipParentChangeTracking);
            }
          }
        } else {
          try {
            def = type.getDefault(doc, false);
          } catch (err) {
            doc.invalidate(p, err);
            break;
          }
 
          if (typeof def !== 'undefined') {
            doc_[piece] = def;
            applyChangeTracking(doc, p, skipParentChangeTracking);
          }
        }
      } else {
        doc_ = doc_[piece];
      }
    }
  }
};
 
/*!
 * ignore
 */
 
function applyChangeTracking(doc, fullPath, skipParentChangeTracking) {
  doc.$__.activePaths.default(fullPath);
  Iif (!skipParentChangeTracking && doc.$isSubdocument && doc.$isSingleNested && doc.$parent() != null) {
    doc.$parent().$__.activePaths.default(doc.$__pathRelativeToParent(fullPath));
  }
}