All files / mongoose/lib/types arraySubdocument.js

60.71% Statements 34/56
36.36% Branches 12/33
50% Functions 4/8
60.71% Lines 34/56

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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198            1x 1x 1x   1x                             3x 3x 3x         3x 3x     3x       3x     3x           1x 1x   1x           1x           1x 18x             1x 3x   3x                       1x                   1x                                         1x                                                       1x 9x     9x     9x 4x   5x                     1x 7x                                       1x               1x  
/*!
 * Module dependencies.
 */
 
'use strict';
 
const EventEmitter = require('events').EventEmitter;
const Subdocument = require('./subdocument');
const utils = require('../utils');
 
const documentArrayParent = require('../helpers/symbols').documentArrayParent;
 
/**
 * A constructor.
 *
 * @param {Object} obj js object returned from the db
 * @param {MongooseDocumentArray} parentArr the parent array of this document
 * @param {Boolean} skipId
 * @param {Object} fields
 * @param {Number} index
 * @inherits Document
 * @api private
 */
 
function ArraySubdocument(obj, parentArr, skipId, fields, index) {
  Eif (utils.isMongooseDocumentArray(parentArr)) {
    this.__parentArray = parentArr;
    this[documentArrayParent] = parentArr.$parent();
  } else {
    this.__parentArray = undefined;
    this[documentArrayParent] = undefined;
  }
  this.$setIndex(index);
  this.$__parent = this[documentArrayParent];
 
  let options;
  Iif (typeof skipId === 'object' && skipId != null) {
    options = { isNew: true, ...skipId };
    skipId = undefined;
  } else {
    options = { isNew: true };
  }
 
  Subdocument.call(this, obj, fields, this[documentArrayParent], skipId, options);
}
 
/*!
 * Inherit from Subdocument
 */
ArraySubdocument.prototype = Object.create(Subdocument.prototype);
ArraySubdocument.prototype.constructor = ArraySubdocument;
 
Object.defineProperty(ArraySubdocument.prototype, '$isSingleNested', {
  configurable: false,
  writable: false,
  value: false
});
 
Object.defineProperty(ArraySubdocument.prototype, '$isDocumentArrayElement', {
  configurable: false,
  writable: false,
  value: true
});
 
for (const i in EventEmitter.prototype) {
  ArraySubdocument[i] = EventEmitter.prototype[i];
}
 
/*!
 * ignore
 */
 
ArraySubdocument.prototype.$setIndex = function(index) {
  this.__index = index;
 
  Iif (this.$__ != null && this.$__.validationError != null) {
    const keys = Object.keys(this.$__.validationError.errors);
    for (const key of keys) {
      this.invalidate(key, this.$__.validationError.errors[key]);
    }
  }
};
 
/*!
 * ignore
 */
 
ArraySubdocument.prototype.populate = function() {
  throw new Error('Mongoose does not support calling populate() on nested ' +
    'docs. Instead of `doc.arr[0].populate("path")`, use ' +
    '`doc.populate("arr.0.path")`');
};
 
/*!
 * ignore
 */
 
ArraySubdocument.prototype.$__removeFromParent = function() {
  const _id = this._doc._id;
  if (!_id) {
    throw new Error('For your own good, Mongoose does not know ' +
      'how to remove an ArraySubdocument that has no _id');
  }
  this.__parentArray.pull({ _id: _id });
};
 
/**
 * Returns the full path to this document. If optional `path` is passed, it is appended to the full path.
 *
 * @param {String} [path]
 * @param {Boolean} [skipIndex] Skip adding the array index. For example `arr.foo` instead of `arr.0.foo`.
 * @return {String}
 * @api private
 * @method $__fullPath
 * @memberOf ArraySubdocument
 * @instance
 */
 
ArraySubdocument.prototype.$__fullPath = function(path, skipIndex) {
  if (this.__index == null) {
    return null;
  }
  if (!this.$__.fullPath) {
    this.ownerDocument();
  }
 
  if (skipIndex) {
    return path ?
      this.$__.fullPath + '.' + path :
      this.$__.fullPath;
  }
 
  return path ?
    this.$__.fullPath + '.' + this.__index + '.' + path :
    this.$__.fullPath + '.' + this.__index;
};
 
/**
 * Given a path relative to this document, return the path relative
 * to the top-level document.
 * @method $__pathRelativeToParent
 * @memberOf ArraySubdocument
 * @instance
 * @api private
 */
 
ArraySubdocument.prototype.$__pathRelativeToParent = function(path, skipIndex) {
  Iif (this.__index == null || (!this.__parentArray || !this.__parentArray.$path)) {
    return null;
  }
  Iif (skipIndex) {
    return path == null ? this.__parentArray.$path() : this.__parentArray.$path() + '.' + path;
  }
  if (path == null) {
    return this.__parentArray.$path() + '.' + this.__index;
  }
  return this.__parentArray.$path() + '.' + this.__index + '.' + path;
};
 
/**
 * Returns this sub-documents parent document.
 * @method $parent
 * @memberOf ArraySubdocument
 * @instance
 * @api public
 */
 
ArraySubdocument.prototype.$parent = function() {
  return this[documentArrayParent];
};
 
/**
 * Returns this subdocument's parent array.
 *
 * #### Example:
 *
 *     const Test = mongoose.model('Test', new Schema({
 *       docArr: [{ name: String }]
 *     }));
 *     const doc = new Test({ docArr: [{ name: 'test subdoc' }] });
 *
 *     doc.docArr[0].parentArray() === doc.docArr; // true
 *
 * @api public
 * @method parentArray
 * @returns DocumentArray
 */
 
ArraySubdocument.prototype.parentArray = function() {
  return this.__parentArray;
};
 
/*!
 * Module exports.
 */
 
module.exports = ArraySubdocument;