All files / koa/libs/plugins autopopulate.js

39.66% Statements 23/58
27.66% Branches 13/47
54.55% Functions 6/11
39.66% Lines 23/58

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 131 132 133 134 135 136 137                                                                    3x 3x 3x   3x                                   3x 3x   3x 21x 21x     21x   21x       3x 3x   3x   21x 3x 3x       18x                       3x 3x 3x                 3x                                                       3x        
'use strict'
function handlePrimitive(value, options) {
  if (value) {
    this.populate(options);
  }
}
// function mergeOptions(destination, source) {
//   const keys = Object.keys(source);
//   const numKeys = keys.length;
//   for (let i = 0; i < numKeys; ++i) {
//     destination[keys[i]] = source[keys[i]];
//   }
// }
 
function handleObject(value, optionsToUse) {
  // Special case: support top-level `maxDepth`
  // mergeOptions(optionsToUse, value);
  if (value.maxDepth !== null) {
    optionsToUse.options = optionsToUse.options || {};
    optionsToUse.options.maxDepth = value.maxDepth;
    delete value.maxDepth;
  }
  optionsToUse = Object.assign({}, optionsToUse, value);
  this.populate(optionsToUse);
}
 
/* eslint-disable */
function handleFunction(fn, options) {
  const val = fn.call(this);
  processOption.call(this, val, options);
}
/* eslint-enable */
 
function defaultOptions(pathname, v) {
  const ret = { path: pathname, options: { maxDepth: 10 } };
  Eif (v.ref) {
    ret.model = v.ref;
  }
  return ret
}
 
function processOption(value, options) {
  switch (typeof value) {
    case 'function':
      handleFunction.call(this, value, options);
      break;
    case 'object':
      handleObject.call(this, value, options);
      break;
    default:
      handlePrimitive.call(this, value, options);
      break;
  }
}
 
function eachPathRecursive(schema, handler, path) {
  Eif (!path) {
    path = [];
  }
  schema.eachPath(function (pathname, schemaType) {
    path.push(pathname);
    Iif (schemaType.schema) {
      eachPathRecursive(schemaType.schema, handler, path);
    } else {
      handler(path.join('.'), schemaType);
    }
    path.pop();
  });
}
 
module.exports = function (schema) {
  const pathsToPopulate = [];
 
  eachPathRecursive(schema, function (pathname, schemaType) {
    let option;
    if (schemaType.options && schemaType.options.autopopulate) {
      option = schemaType.options.autopopulate;
      pathsToPopulate.push({
        options: defaultOptions(pathname, schemaType.options),
        autopopulate: option,
      });
    } else Iif (schemaType.options
      && schemaType.options.type
      && schemaType.options.type[0]
      && schemaType.options.type[0].autopopulate) {
      option = schemaType.options.type[0].autopopulate;
      pathsToPopulate.push({
        options: defaultOptions(pathname, schemaType.options.type[0]),
        autopopulate: option,
      });
    }
  });
 
  Eif (schema.virtuals) {
    Object.keys(schema.virtuals).forEach(function (pathname) {
      Iif (schema.virtuals[pathname].options.autopopulate) {
        pathsToPopulate.push({
          options: defaultOptions(pathname, schema.virtuals[pathname].options),
          autopopulate: schema.virtuals[pathname].options.autopopulate,
        });
      }
    });
  }
 
  var autopopulateHandler = function () {
    if (this._mongooseOptions && this._mongooseOptions.lean) {
      return;
    }
 
    const options = this.options || {};
    if (options.autopopulate === false) {
      return;
    }
 
    const depth = options._depth !== null ? options._depth : 0;
    if (options.maxDepth > 0 && depth >= options.maxDepth) {
      return;
    }
 
    const numPaths = pathsToPopulate.length;
    for (let i = 0; i < numPaths; ++i) {
      // processOption.call(this,
      //   pathsToPopulate[i].autopopulate, pathsToPopulate[i].options);
 
      pathsToPopulate[i].options = pathsToPopulate[i].options || {};
      pathsToPopulate[i].options.options = pathsToPopulate[i].options.options || {};
      Object.assign(pathsToPopulate[i].options.options, { _depth: depth + 1 });
      processOption.call(this,
        pathsToPopulate[i].autopopulate, pathsToPopulate[i].options);
    }
  };
 
  schema.
    pre('find', autopopulateHandler).
    pre('findOne', autopopulateHandler);
};