1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const CoreMongooseArray = require('./core_array');
|
8 | const Document = require('../document');
|
9 | const ObjectId = require('./objectid');
|
10 | const castObjectId = require('../cast/objectid');
|
11 | const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
|
12 | const internalToObjectOptions = require('../options').internalToObjectOptions;
|
13 | const util = require('util');
|
14 | const utils = require('../utils');
|
15 |
|
16 | const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
|
17 | const arrayParentSymbol = require('../helpers/symbols').arrayParentSymbol;
|
18 | const arrayPathSymbol = require('../helpers/symbols').arrayPathSymbol;
|
19 | const arraySchemaSymbol = require('../helpers/symbols').arraySchemaSymbol;
|
20 | const documentArrayParent = require('../helpers/symbols').documentArrayParent;
|
21 |
|
22 | const _basePush = Array.prototype.push;
|
23 |
|
24 | class CoreDocumentArray extends CoreMongooseArray {
|
25 | get isMongooseDocumentArray() {
|
26 | return true;
|
27 | }
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 | toBSON() {
|
34 | return this.toObject(internalToObjectOptions);
|
35 | }
|
36 |
|
37 | |
38 |
|
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 |
|
52 |
|
53 |
|
54 |
|
55 |
|
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 |
|
68 | (value && value.constructor && value.constructor.baseCasterConstructor === Constructor)) {
|
69 | if (!(value[documentArrayParent] && value.__parentArray)) {
|
70 |
|
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 |
|
83 |
|
84 |
|
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 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
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 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | toObject(options) {
|
179 |
|
180 |
|
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 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | push() {
|
210 | const ret = super.push.apply(this, arguments);
|
211 |
|
212 | _updateParentPopulated(this);
|
213 |
|
214 | return ret;
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | pull() {
|
227 | const ret = super.pull.apply(this, arguments);
|
228 |
|
229 | _updateParentPopulated(this);
|
230 |
|
231 | return ret;
|
232 | }
|
233 |
|
234 | |
235 |
|
236 |
|
237 |
|
238 | shift() {
|
239 | const ret = super.shift.apply(this, arguments);
|
240 |
|
241 | _updateParentPopulated(this);
|
242 |
|
243 | return ret;
|
244 | }
|
245 |
|
246 | |
247 |
|
248 |
|
249 |
|
250 | splice() {
|
251 | const ret = super.splice.apply(this, arguments);
|
252 |
|
253 | _updateParentPopulated(this);
|
254 |
|
255 | return ret;
|
256 | }
|
257 |
|
258 | |
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | inspect() {
|
267 | return this.toObject();
|
268 | }
|
269 |
|
270 | |
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
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 |
|
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 |
|
317 | case 'save':
|
318 | val = _this[i];
|
319 | break;
|
320 | default:
|
321 |
|
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 |
|
335 | if (util.inspect.custom) {
|
336 | CoreDocumentArray.prototype[util.inspect.custom] =
|
337 | CoreDocumentArray.prototype.inspect;
|
338 | }
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | function _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 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | function MongooseDocumentArray(values, path, doc) {
|
376 |
|
377 |
|
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 |
|
395 |
|
396 |
|
397 |
|
398 | if (doc && doc instanceof Document) {
|
399 | arr[arrayParentSymbol] = doc;
|
400 | arr[arraySchemaSymbol] = doc.schema.path(path);
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
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 |
|
419 |
|
420 |
|
421 | module.exports = MongooseDocumentArray;
|