UNPKG

11.5 kBJavaScriptView Raw
1'use strict';
2
3/*!
4 * Module dependencies.
5 */
6
7const CoreMongooseArray = require('./core_array');
8const Document = require('../document');
9const ObjectId = require('./objectid');
10const castObjectId = require('../cast/objectid');
11const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
12const internalToObjectOptions = require('../options').internalToObjectOptions;
13const util = require('util');
14const utils = require('../utils');
15
16const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
17const arrayParentSymbol = require('../helpers/symbols').arrayParentSymbol;
18const arrayPathSymbol = require('../helpers/symbols').arrayPathSymbol;
19const arraySchemaSymbol = require('../helpers/symbols').arraySchemaSymbol;
20const documentArrayParent = require('../helpers/symbols').documentArrayParent;
21
22const _basePush = Array.prototype.push;
23
24class CoreDocumentArray extends CoreMongooseArray {
25 get isMongooseDocumentArray() {
26 return true;
27 }
28
29 /*!
30 * ignore
31 */
32
33 toBSON() {
34 return this.toObject(internalToObjectOptions);
35 }
36
37 /*!
38 * ignore
39 */
40
41 map() {
42 const ret = super.map.apply(this, arguments);
43 ret[arraySchemaSymbol] = null;
44 ret[arrayPathSymbol] = null;
45 ret[arrayParentSymbol] = null;
46
47 return ret;
48 }
49
50 /**
51 * Overrides MongooseArray#cast
52 *
53 * @method _cast
54 * @api private
55 * @receiver MongooseDocumentArray
56 */
57
58 _cast(value, index) {
59 if (this[arraySchemaSymbol] == null) {
60 return value;
61 }
62 let Constructor = this[arraySchemaSymbol].casterConstructor;
63 const isInstance = Constructor.$isMongooseDocumentArray ?
64 value && value.isMongooseDocumentArray :
65 value instanceof Constructor;
66 if (isInstance ||
67 // Hack re: #5001, see #5005
68 (value && value.constructor && value.constructor.baseCasterConstructor === Constructor)) {
69 if (!(value[documentArrayParent] && value.__parentArray)) {
70 // value may have been created using array.create()
71 value[documentArrayParent] = this[arrayParentSymbol];
72 value.__parentArray = this;
73 }
74 value.$setIndex(index);
75 return value;
76 }
77
78 if (value === undefined || value === null) {
79 return null;
80 }
81
82 // handle cast('string') or cast(ObjectId) etc.
83 // only objects are permitted so we can safely assume that
84 // non-objects are to be interpreted as _id
85 if (Buffer.isBuffer(value) ||
86 value instanceof ObjectId || !utils.isObject(value)) {
87 value = { _id: value };
88 }
89
90 if (value &&
91 Constructor.discriminators &&
92 Constructor.schema &&
93 Constructor.schema.options &&
94 Constructor.schema.options.discriminatorKey) {
95 if (typeof value[Constructor.schema.options.discriminatorKey] === 'string' &&
96 Constructor.discriminators[value[Constructor.schema.options.discriminatorKey]]) {
97 Constructor = Constructor.discriminators[value[Constructor.schema.options.discriminatorKey]];
98 } else {
99 const constructorByValue = getDiscriminatorByValue(Constructor, value[Constructor.schema.options.discriminatorKey]);
100 if (constructorByValue) {
101 Constructor = constructorByValue;
102 }
103 }
104 }
105
106 if (Constructor.$isMongooseDocumentArray) {
107 return Constructor.cast(value, this, undefined, undefined, index);
108 }
109 return new Constructor(value, this, undefined, undefined, index);
110 }
111
112 /**
113 * Searches array items for the first document with a matching _id.
114 *
115 * ####Example:
116 *
117 * var embeddedDoc = m.array.id(some_id);
118 *
119 * @return {EmbeddedDocument|null} the subdocument or null if not found.
120 * @param {ObjectId|String|Number|Buffer} id
121 * @TODO cast to the _id based on schema for proper comparison
122 * @method id
123 * @api public
124 * @receiver MongooseDocumentArray
125 */
126
127 id(id) {
128 let casted;
129 let sid;
130 let _id;
131
132 try {
133 casted = castObjectId(id).toString();
134 } catch (e) {
135 casted = null;
136 }
137
138 for (const val of this) {
139 if (!val) {
140 continue;
141 }
142
143 _id = val.get('_id');
144
145 if (_id === null || typeof _id === 'undefined') {
146 continue;
147 } else if (_id instanceof Document) {
148 sid || (sid = String(id));
149 if (sid == _id._id) {
150 return val;
151 }
152 } else if (!(id instanceof ObjectId) && !(_id instanceof ObjectId)) {
153 if (utils.deepEqual(id, _id)) {
154 return val;
155 }
156 } else if (casted == _id) {
157 return val;
158 }
159 }
160
161 return null;
162 }
163
164 /**
165 * Returns a native js Array of plain js objects
166 *
167 * ####NOTE:
168 *
169 * _Each sub-document is converted to a plain object by calling its `#toObject` method._
170 *
171 * @param {Object} [options] optional options to pass to each documents `toObject` method call during conversion
172 * @return {Array}
173 * @method toObject
174 * @api public
175 * @receiver MongooseDocumentArray
176 */
177
178 toObject(options) {
179 // `[].concat` coerces the return value into a vanilla JS array, rather
180 // than a Mongoose array.
181 return [].concat(this.map(function(doc) {
182 if (doc == null) {
183 return null;
184 }
185 if (typeof doc.toObject !== 'function') {
186 return doc;
187 }
188 return doc.toObject(options);
189 }));
190 }
191
192 slice() {
193 const arr = super.slice.apply(this, arguments);
194 arr[arrayParentSymbol] = this[arrayParentSymbol];
195 arr[arrayPathSymbol] = this[arrayPathSymbol];
196
197 return arr;
198 }
199
200 /**
201 * Wraps [`Array#push`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push) with proper change tracking.
202 *
203 * @param {Object} [args...]
204 * @api public
205 * @method push
206 * @memberOf MongooseDocumentArray
207 */
208
209 push() {
210 const ret = super.push.apply(this, arguments);
211
212 _updateParentPopulated(this);
213
214 return ret;
215 }
216
217 /**
218 * Pulls items from the array atomically.
219 *
220 * @param {Object} [args...]
221 * @api public
222 * @method pull
223 * @memberOf MongooseDocumentArray
224 */
225
226 pull() {
227 const ret = super.pull.apply(this, arguments);
228
229 _updateParentPopulated(this);
230
231 return ret;
232 }
233
234 /**
235 * Wraps [`Array#shift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking.
236 */
237
238 shift() {
239 const ret = super.shift.apply(this, arguments);
240
241 _updateParentPopulated(this);
242
243 return ret;
244 }
245
246 /**
247 * Wraps [`Array#splice`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice) with proper change tracking and casting.
248 */
249
250 splice() {
251 const ret = super.splice.apply(this, arguments);
252
253 _updateParentPopulated(this);
254
255 return ret;
256 }
257
258 /**
259 * Helper for console.log
260 *
261 * @method inspect
262 * @api public
263 * @receiver MongooseDocumentArray
264 */
265
266 inspect() {
267 return this.toObject();
268 }
269
270 /**
271 * Creates a subdocument casted to this schema.
272 *
273 * This is the same subdocument constructor used for casting.
274 *
275 * @param {Object} obj the value to cast to this arrays SubDocument schema
276 * @method create
277 * @api public
278 * @receiver MongooseDocumentArray
279 */
280
281 create(obj) {
282 let Constructor = this[arraySchemaSymbol].casterConstructor;
283 if (obj &&
284 Constructor.discriminators &&
285 Constructor.schema &&
286 Constructor.schema.options &&
287 Constructor.schema.options.discriminatorKey) {
288 if (typeof obj[Constructor.schema.options.discriminatorKey] === 'string' &&
289 Constructor.discriminators[obj[Constructor.schema.options.discriminatorKey]]) {
290 Constructor = Constructor.discriminators[obj[Constructor.schema.options.discriminatorKey]];
291 } else {
292 const constructorByValue = getDiscriminatorByValue(Constructor, obj[Constructor.schema.options.discriminatorKey]);
293 if (constructorByValue) {
294 Constructor = constructorByValue;
295 }
296 }
297 }
298
299 return new Constructor(obj, this);
300 }
301
302 /*!
303 * ignore
304 */
305
306 notify(event) {
307 const _this = this;
308 return function notify(val, _arr) {
309 _arr = _arr || _this;
310 let i = _arr.length;
311 while (i--) {
312 if (_arr[i] == null) {
313 continue;
314 }
315 switch (event) {
316 // only swap for save event for now, we may change this to all event types later
317 case 'save':
318 val = _this[i];
319 break;
320 default:
321 // NO-OP
322 break;
323 }
324
325 if (_arr[i].isMongooseArray) {
326 notify(val, _arr[i]);
327 } else if (_arr[i]) {
328 _arr[i].emit(event, val);
329 }
330 }
331 };
332 }
333}
334
335if (util.inspect.custom) {
336 CoreDocumentArray.prototype[util.inspect.custom] =
337 CoreDocumentArray.prototype.inspect;
338}
339
340/*!
341 * If this is a document array, each element may contain single
342 * populated paths, so we need to modify the top-level document's
343 * populated cache. See gh-8247, gh-8265.
344 */
345
346function _updateParentPopulated(arr) {
347 const parent = arr[arrayParentSymbol];
348 if (!parent || parent.$__.populated == null) return;
349
350 const populatedPaths = Object.keys(parent.$__.populated).
351 filter(p => p.startsWith(arr[arrayPathSymbol] + '.'));
352
353 for (const path of populatedPaths) {
354 const remnant = path.slice((arr[arrayPathSymbol] + '.').length);
355 if (!Array.isArray(parent.$__.populated[path].value)) {
356 continue;
357 }
358
359 parent.$__.populated[path].value = arr.map(val => val.populated(remnant));
360 }
361}
362
363/**
364 * DocumentArray constructor
365 *
366 * @param {Array} values
367 * @param {String} path the path to this array
368 * @param {Document} doc parent document
369 * @api private
370 * @return {MongooseDocumentArray}
371 * @inherits MongooseArray
372 * @see http://bit.ly/f6CnZU
373 */
374
375function MongooseDocumentArray(values, path, doc) {
376 // TODO: replace this with `new CoreDocumentArray().concat()` when we remove
377 // support for node 4.x and 5.x, see https://i.imgur.com/UAAHk4S.png
378 const arr = new CoreDocumentArray();
379
380 arr[arrayAtomicsSymbol] = {};
381 arr[arraySchemaSymbol] = void 0;
382 if (Array.isArray(values)) {
383 if (values instanceof CoreDocumentArray &&
384 values[arrayPathSymbol] === path &&
385 values[arrayParentSymbol] === doc) {
386 arr[arrayAtomicsSymbol] = Object.assign({}, values[arrayAtomicsSymbol]);
387 }
388 values.forEach(v => {
389 _basePush.call(arr, v);
390 });
391 }
392 arr[arrayPathSymbol] = path;
393
394 // Because doc comes from the context of another function, doc === global
395 // can happen if there was a null somewhere up the chain (see #3020 && #3034)
396 // RB Jun 17, 2015 updated to check for presence of expected paths instead
397 // to make more proof against unusual node environments
398 if (doc && doc instanceof Document) {
399 arr[arrayParentSymbol] = doc;
400 arr[arraySchemaSymbol] = doc.schema.path(path);
401
402 // `schema.path()` doesn't drill into nested arrays properly yet, see
403 // gh-6398, gh-6602. This is a workaround because nested arrays are
404 // always plain non-document arrays, so once you get to a document array
405 // nesting is done. Matryoshka code.
406 while (arr != null &&
407 arr[arraySchemaSymbol] != null &&
408 arr[arraySchemaSymbol].$isMongooseArray &&
409 !arr[arraySchemaSymbol].$isMongooseDocumentArray) {
410 arr[arraySchemaSymbol] = arr[arraySchemaSymbol].casterConstructor;
411 }
412 }
413
414 return arr;
415}
416
417/*!
418 * Module exports.
419 */
420
421module.exports = MongooseDocumentArray;