1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const EventEmitter = require('events').EventEmitter;
|
8 | const InternalCache = require('./internal');
|
9 | const MongooseError = require('./error/index');
|
10 | const MixedSchema = require('./schema/mixed');
|
11 | const ObjectExpectedError = require('./error/objectExpected');
|
12 | const ObjectParameterError = require('./error/objectParameter');
|
13 | const ParallelValidateError = require('./error/parallelValidate');
|
14 | const Schema = require('./schema');
|
15 | const StrictModeError = require('./error/strict');
|
16 | const ValidationError = require('./error/validation');
|
17 | const ValidatorError = require('./error/validator');
|
18 | const VirtualType = require('./virtualtype');
|
19 | const promiseOrCallback = require('./helpers/promiseOrCallback');
|
20 | const cleanModifiedSubpaths = require('./helpers/document/cleanModifiedSubpaths');
|
21 | const compile = require('./helpers/document/compile').compile;
|
22 | const defineKey = require('./helpers/document/compile').defineKey;
|
23 | const flatten = require('./helpers/common').flatten;
|
24 | const get = require('./helpers/get');
|
25 | const getEmbeddedDiscriminatorPath = require('./helpers/document/getEmbeddedDiscriminatorPath');
|
26 | const handleSpreadDoc = require('./helpers/document/handleSpreadDoc');
|
27 | const idGetter = require('./plugins/idGetter');
|
28 | const isDefiningProjection = require('./helpers/projection/isDefiningProjection');
|
29 | const isExclusive = require('./helpers/projection/isExclusive');
|
30 | const inspect = require('util').inspect;
|
31 | const internalToObjectOptions = require('./options').internalToObjectOptions;
|
32 | const mpath = require('mpath');
|
33 | const utils = require('./utils');
|
34 |
|
35 | const clone = utils.clone;
|
36 | const deepEqual = utils.deepEqual;
|
37 | const isMongooseObject = utils.isMongooseObject;
|
38 |
|
39 | const arrayAtomicsSymbol = require('./helpers/symbols').arrayAtomicsSymbol;
|
40 | const documentArrayParent = require('./helpers/symbols').documentArrayParent;
|
41 | const documentSchemaSymbol = require('./helpers/symbols').documentSchemaSymbol;
|
42 | const getSymbol = require('./helpers/symbols').getSymbol;
|
43 | const populateModelSymbol = require('./helpers/symbols').populateModelSymbol;
|
44 |
|
45 | let DocumentArray;
|
46 | let MongooseArray;
|
47 | let Embedded;
|
48 |
|
49 | const specialProperties = utils.specialProperties;
|
50 |
|
51 | /**
|
52 | * The core Mongoose document constructor. You should not call this directly,
|
53 | * the Mongoose [Model constructor](./api.html#Model) calls this for you.
|
54 | *
|
55 | * @param {Object} obj the values to set
|
56 | * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
|
57 | * @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
|
58 | * @inherits NodeJS EventEmitter http:
|
59 | * @event `init`: Emitted on a document after it has been retrieved from the db and fully hydrated by Mongoose.
|
60 | * @event `save`: Emitted when the document is successfully saved
|
61 | * @api private
|
62 | */
|
63 |
|
64 | function Document(obj, fields, skipId, options) {
|
65 | if (typeof skipId === 'object' && skipId != null) {
|
66 | options = skipId;
|
67 | skipId = options.skipId;
|
68 | }
|
69 | options = options || {};
|
70 |
|
71 |
|
72 | if (this.schema == null) {
|
73 | const _schema = utils.isObject(fields) && !fields.instanceOfSchema ?
|
74 | new Schema(fields) :
|
75 | fields;
|
76 | this.$__setSchema(_schema);
|
77 | fields = skipId;
|
78 | skipId = options;
|
79 | options = arguments[4] || {};
|
80 | }
|
81 |
|
82 | this.$__ = new InternalCache;
|
83 | this.$__.emitter = new EventEmitter();
|
84 | this.isNew = 'isNew' in options ? options.isNew : true;
|
85 | this.errors = undefined;
|
86 | this.$__.$options = options || {};
|
87 |
|
88 | if (obj != null && typeof obj !== 'object') {
|
89 | throw new ObjectParameterError(obj, 'obj', 'Document');
|
90 | }
|
91 |
|
92 | const schema = this.schema;
|
93 |
|
94 | if (typeof fields === 'boolean' || fields === 'throw') {
|
95 | this.$__.strictMode = fields;
|
96 | fields = undefined;
|
97 | } else {
|
98 | this.$__.strictMode = schema.options.strict;
|
99 | this.$__.selected = fields;
|
100 | }
|
101 |
|
102 | const required = schema.requiredPaths(true);
|
103 | for (let i = 0; i < required.length; ++i) {
|
104 | this.$__.activePaths.require(required[i]);
|
105 | }
|
106 |
|
107 | this.$__.emitter.setMaxListeners(0);
|
108 |
|
109 | let exclude = null;
|
110 |
|
111 |
|
112 |
|
113 | if (utils.isPOJO(fields)) {
|
114 | exclude = isExclusive(fields);
|
115 | }
|
116 |
|
117 | const hasIncludedChildren = exclude === false && fields ?
|
118 | $__hasIncludedChildren(fields) :
|
119 | {};
|
120 |
|
121 | if (this._doc == null) {
|
122 | this.$__buildDoc(obj, fields, skipId, exclude, hasIncludedChildren, false);
|
123 |
|
124 |
|
125 |
|
126 | $__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, true, {
|
127 | isNew: this.isNew
|
128 | });
|
129 | }
|
130 |
|
131 | if (obj) {
|
132 |
|
133 | if (this.$__original_set) {
|
134 | this.$__original_set(obj, undefined, true);
|
135 | } else {
|
136 | this.$set(obj, undefined, true);
|
137 | }
|
138 |
|
139 | if (obj instanceof Document) {
|
140 | this.isNew = obj.isNew;
|
141 | }
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | if (options.willInit) {
|
148 | EventEmitter.prototype.once.call(this, 'init', () => {
|
149 | $__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, false, options.skipDefaults, {
|
150 | isNew: this.isNew
|
151 | });
|
152 | });
|
153 | } else {
|
154 | $__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, false, options.skipDefaults, {
|
155 | isNew: this.isNew
|
156 | });
|
157 | }
|
158 |
|
159 | this.$__._id = this._id;
|
160 | this.$locals = {};
|
161 | this.$op = null;
|
162 |
|
163 | if (!this.$__.strictMode && obj) {
|
164 | const _this = this;
|
165 | const keys = Object.keys(this._doc);
|
166 |
|
167 | keys.forEach(function(key) {
|
168 | if (!(key in schema.tree)) {
|
169 | defineKey(key, null, _this);
|
170 | }
|
171 | });
|
172 | }
|
173 |
|
174 | applyQueue(this);
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | utils.each(
|
182 | ['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
|
183 | 'removeAllListeners', 'addListener'],
|
184 | function(emitterFn) {
|
185 | Document.prototype[emitterFn] = function() {
|
186 | return this.$__.emitter[emitterFn].apply(this.$__.emitter, arguments);
|
187 | };
|
188 | });
|
189 |
|
190 | Document.prototype.constructor = Document;
|
191 |
|
192 | for (const i in EventEmitter.prototype) {
|
193 | Document[i] = EventEmitter.prototype[i];
|
194 | }
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | Document.prototype.schema;
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | Object.defineProperty(Document.prototype, '$locals', {
|
231 | configurable: false,
|
232 | enumerable: false,
|
233 | writable: true
|
234 | });
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | Document.prototype.isNew;
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | Document.prototype.id;
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | Document.prototype.errors;
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | Document.prototype.$op;
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | function $__hasIncludedChildren(fields) {
|
304 | const hasIncludedChildren = {};
|
305 | const keys = Object.keys(fields);
|
306 | for (let j = 0; j < keys.length; ++j) {
|
307 | const parts = keys[j].split('.');
|
308 | const c = [];
|
309 | for (let k = 0; k < parts.length; ++k) {
|
310 | c.push(parts[k]);
|
311 | hasIncludedChildren[c.join('.')] = 1;
|
312 | }
|
313 | }
|
314 |
|
315 | return hasIncludedChildren;
|
316 | }
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | function $__applyDefaults(doc, fields, skipId, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip) {
|
323 | const paths = Object.keys(doc.schema.paths);
|
324 | const plen = paths.length;
|
325 |
|
326 | for (let i = 0; i < plen; ++i) {
|
327 | let def;
|
328 | let curPath = '';
|
329 | const p = paths[i];
|
330 |
|
331 | if (p === '_id' && skipId) {
|
332 | continue;
|
333 | }
|
334 |
|
335 | const type = doc.schema.paths[p];
|
336 | const path = p.indexOf('.') === -1 ? [p] : p.split('.');
|
337 | const len = path.length;
|
338 | let included = false;
|
339 | let doc_ = doc._doc;
|
340 |
|
341 | for (let j = 0; j < len; ++j) {
|
342 | if (doc_ == null) {
|
343 | break;
|
344 | }
|
345 |
|
346 | const piece = path[j];
|
347 | curPath += (!curPath.length ? '' : '.') + piece;
|
348 |
|
349 | if (exclude === true) {
|
350 | if (curPath in fields) {
|
351 | break;
|
352 | }
|
353 | } else if (exclude === false && fields && !included) {
|
354 | if (curPath in fields) {
|
355 | included = true;
|
356 | } else if (!hasIncludedChildren[curPath]) {
|
357 | break;
|
358 | }
|
359 | }
|
360 |
|
361 | if (j === len - 1) {
|
362 | if (doc_[piece] !== void 0) {
|
363 | break;
|
364 | }
|
365 |
|
366 | if (typeof type.defaultValue === 'function') {
|
367 | if (!type.defaultValue.$runBeforeSetters && isBeforeSetters) {
|
368 | break;
|
369 | }
|
370 | if (type.defaultValue.$runBeforeSetters && !isBeforeSetters) {
|
371 | break;
|
372 | }
|
373 | } else if (!isBeforeSetters) {
|
374 |
|
375 | continue;
|
376 | }
|
377 |
|
378 | if (pathsToSkip && pathsToSkip[curPath]) {
|
379 | break;
|
380 | }
|
381 |
|
382 | if (fields && exclude !== null) {
|
383 | if (exclude === true) {
|
384 |
|
385 | if (p in fields) {
|
386 | continue;
|
387 | }
|
388 |
|
389 | def = type.getDefault(doc, false);
|
390 | if (typeof def !== 'undefined') {
|
391 | doc_[piece] = def;
|
392 | doc.$__.activePaths.default(p);
|
393 | }
|
394 | } else if (included) {
|
395 |
|
396 | def = type.getDefault(doc, false);
|
397 | if (typeof def !== 'undefined') {
|
398 | doc_[piece] = def;
|
399 | doc.$__.activePaths.default(p);
|
400 | }
|
401 | }
|
402 | } else {
|
403 | def = type.getDefault(doc, false);
|
404 | if (typeof def !== 'undefined') {
|
405 | doc_[piece] = def;
|
406 | doc.$__.activePaths.default(p);
|
407 | }
|
408 | }
|
409 | } else {
|
410 | doc_ = doc_[piece];
|
411 | }
|
412 | }
|
413 | }
|
414 | }
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 | Document.prototype.$__buildDoc = function(obj, fields, skipId, exclude, hasIncludedChildren) {
|
429 | const doc = {};
|
430 |
|
431 | const paths = Object.keys(this.schema.paths).
|
432 |
|
433 |
|
434 | filter(p => !p.includes('$*'));
|
435 | const plen = paths.length;
|
436 | let ii = 0;
|
437 |
|
438 | for (; ii < plen; ++ii) {
|
439 | const p = paths[ii];
|
440 |
|
441 | if (p === '_id') {
|
442 | if (skipId) {
|
443 | continue;
|
444 | }
|
445 | if (obj && '_id' in obj) {
|
446 | continue;
|
447 | }
|
448 | }
|
449 |
|
450 | const path = p.split('.');
|
451 | const len = path.length;
|
452 | const last = len - 1;
|
453 | let curPath = '';
|
454 | let doc_ = doc;
|
455 | let included = false;
|
456 |
|
457 | for (let i = 0; i < len; ++i) {
|
458 | const piece = path[i];
|
459 |
|
460 | curPath += (!curPath.length ? '' : '.') + piece;
|
461 |
|
462 |
|
463 | if (exclude === true) {
|
464 | if (curPath in fields) {
|
465 | break;
|
466 | }
|
467 | } else if (exclude === false && fields && !included) {
|
468 | if (curPath in fields) {
|
469 | included = true;
|
470 | } else if (!hasIncludedChildren[curPath]) {
|
471 | break;
|
472 | }
|
473 | }
|
474 |
|
475 | if (i < last) {
|
476 | doc_ = doc_[piece] || (doc_[piece] = {});
|
477 | }
|
478 | }
|
479 | }
|
480 |
|
481 | this._doc = doc;
|
482 | };
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 | Document.prototype.toBSON = function() {
|
489 | return this.toObject(internalToObjectOptions);
|
490 | };
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 | Document.prototype.init = function(doc, opts, fn) {
|
508 | if (typeof opts === 'function') {
|
509 | fn = opts;
|
510 | opts = null;
|
511 | }
|
512 |
|
513 | this.$__init(doc, opts);
|
514 |
|
515 | if (fn) {
|
516 | fn(null, this);
|
517 | }
|
518 |
|
519 | return this;
|
520 | };
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 | Document.prototype.$__init = function(doc, opts) {
|
527 | this.isNew = false;
|
528 | this.$init = true;
|
529 | opts = opts || {};
|
530 |
|
531 |
|
532 |
|
533 | if (doc._id != null && opts.populated && opts.populated.length) {
|
534 | const id = String(doc._id);
|
535 | for (let i = 0; i < opts.populated.length; ++i) {
|
536 | const item = opts.populated[i];
|
537 | if (item.isVirtual) {
|
538 | this.populated(item.path, utils.getValue(item.path, doc), item);
|
539 | } else {
|
540 | this.populated(item.path, item._docs[id], item);
|
541 | }
|
542 | }
|
543 | }
|
544 |
|
545 | init(this, doc, this._doc, opts);
|
546 |
|
547 | markArraySubdocsPopulated(this, opts.populated);
|
548 |
|
549 | this.emit('init', this);
|
550 | this.constructor.emit('init', this);
|
551 |
|
552 | this.$__._id = this._id;
|
553 |
|
554 | return this;
|
555 | };
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 | function markArraySubdocsPopulated(doc, populated) {
|
567 | if (doc._id == null || populated == null || populated.length === 0) {
|
568 | return;
|
569 | }
|
570 |
|
571 | const id = String(doc._id);
|
572 | for (const item of populated) {
|
573 | if (item.isVirtual) {
|
574 | continue;
|
575 | }
|
576 | const path = item.path;
|
577 | const pieces = path.split('.');
|
578 | for (let i = 0; i < pieces.length - 1; ++i) {
|
579 | const subpath = pieces.slice(0, i + 1).join('.');
|
580 | const rest = pieces.slice(i + 1).join('.');
|
581 | const val = doc.get(subpath);
|
582 | if (val == null) {
|
583 | continue;
|
584 | }
|
585 | if (val.isMongooseDocumentArray) {
|
586 | for (let j = 0; j < val.length; ++j) {
|
587 | val[j].populated(rest, item._docs[id] == null ? [] : item._docs[id][j], item);
|
588 | }
|
589 | break;
|
590 | }
|
591 | }
|
592 | }
|
593 | }
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 |
|
604 | function init(self, obj, doc, opts, prefix) {
|
605 | prefix = prefix || '';
|
606 |
|
607 | const keys = Object.keys(obj);
|
608 | const len = keys.length;
|
609 | let schema;
|
610 | let path;
|
611 | let i;
|
612 | let index = 0;
|
613 |
|
614 | while (index < len) {
|
615 | _init(index++);
|
616 | }
|
617 |
|
618 | function _init(index) {
|
619 | i = keys[index];
|
620 | path = prefix + i;
|
621 | schema = self.schema.path(path);
|
622 |
|
623 |
|
624 |
|
625 |
|
626 | if (self.schema.$isRootDiscriminator && !self.isSelected(path)) {
|
627 | return;
|
628 | }
|
629 |
|
630 | if (!schema && utils.isPOJO(obj[i])) {
|
631 |
|
632 | if (!doc[i]) {
|
633 | doc[i] = {};
|
634 | }
|
635 | init(self, obj[i], doc[i], opts, path + '.');
|
636 | } else if (!schema) {
|
637 | doc[i] = obj[i];
|
638 | } else {
|
639 | if (obj[i] === null) {
|
640 | doc[i] = null;
|
641 | } else if (obj[i] !== undefined) {
|
642 | const intCache = obj[i].$__ || {};
|
643 | const wasPopulated = intCache.wasPopulated || null;
|
644 |
|
645 | if (schema && !wasPopulated) {
|
646 | try {
|
647 | doc[i] = schema.cast(obj[i], self, true);
|
648 | } catch (e) {
|
649 | self.invalidate(e.path, new ValidatorError({
|
650 | path: e.path,
|
651 | message: e.message,
|
652 | type: 'cast',
|
653 | value: e.value
|
654 | }));
|
655 | }
|
656 | } else {
|
657 | doc[i] = obj[i];
|
658 | }
|
659 | }
|
660 |
|
661 | if (!self.isModified(path)) {
|
662 | self.$__.activePaths.init(path);
|
663 | }
|
664 | }
|
665 | }
|
666 | }
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 | Document.prototype.update = function update() {
|
690 | const args = utils.args(arguments);
|
691 | args.unshift({ _id: this._id });
|
692 | const query = this.constructor.update.apply(this.constructor, args);
|
693 |
|
694 | if (this.$session() != null) {
|
695 | if (!('session' in query.options)) {
|
696 | query.options.session = this.$session();
|
697 | }
|
698 | }
|
699 |
|
700 | return query;
|
701 | };
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 | Document.prototype.updateOne = function updateOne(doc, options, callback) {
|
729 | const query = this.constructor.updateOne({ _id: this._id }, doc, options);
|
730 | query._pre(cb => {
|
731 | this.constructor._middleware.execPre('updateOne', this, [this], cb);
|
732 | });
|
733 | query._post(cb => {
|
734 | this.constructor._middleware.execPost('updateOne', this, [this], {}, cb);
|
735 | });
|
736 |
|
737 | if (this.$session() != null) {
|
738 | if (!('session' in query.options)) {
|
739 | query.options.session = this.$session();
|
740 | }
|
741 | }
|
742 |
|
743 | if (callback != null) {
|
744 | return query.exec(callback);
|
745 | }
|
746 |
|
747 | return query;
|
748 | };
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 | Document.prototype.replaceOne = function replaceOne() {
|
768 | const args = utils.args(arguments);
|
769 | args.unshift({ _id: this._id });
|
770 | return this.constructor.replaceOne.apply(this.constructor, args);
|
771 | };
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 | Document.prototype.$session = function $session(session) {
|
797 | if (arguments.length === 0) {
|
798 | return this.$__.session;
|
799 | }
|
800 | this.$__.session = session;
|
801 |
|
802 | if (!this.ownerDocument) {
|
803 | const subdocs = this.$__getAllSubdocs();
|
804 | for (const child of subdocs) {
|
805 | child.$session(session);
|
806 | }
|
807 | }
|
808 |
|
809 | return session;
|
810 | };
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 | Document.prototype.overwrite = function overwrite(obj) {
|
826 | const keys = Array.from(new Set(Object.keys(this._doc).concat(Object.keys(obj))));
|
827 |
|
828 | for (const key of keys) {
|
829 | if (key === '_id') {
|
830 | continue;
|
831 | }
|
832 |
|
833 | if (this.schema.options.versionKey && key === this.schema.options.versionKey) {
|
834 | continue;
|
835 | }
|
836 | if (this.schema.options.discriminatorKey && key === this.schema.options.discriminatorKey) {
|
837 | continue;
|
838 | }
|
839 | this.$set(key, obj[key]);
|
840 | }
|
841 |
|
842 | return this;
|
843 | };
|
844 |
|
845 | /**
|
846 | * Alias for `set()`, used internally to avoid conflicts
|
847 | *
|
848 | * @param {String|Object} path path or object of key/vals to set
|
849 | * @param {Any} val the value to set
|
850 | * @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for "on-the-fly" attributes
|
851 | * @param {Object} [options] optionally specify options that modify the behavior of the set
|
852 | * @method $set
|
853 | * @name $set
|
854 | * @memberOf Document
|
855 | * @instance
|
856 | * @api public
|
857 | */
|
858 |
|
859 | Document.prototype.$set = function $set(path, val, type, options) {
|
860 | if (utils.isPOJO(type)) {
|
861 | options = type;
|
862 | type = undefined;
|
863 | }
|
864 |
|
865 | options = options || {};
|
866 | const merge = options.merge;
|
867 | const adhoc = type && type !== true;
|
868 | const constructing = type === true;
|
869 | let adhocs;
|
870 | let keys;
|
871 | let i = 0;
|
872 | let pathtype;
|
873 | let key;
|
874 | let prefix;
|
875 |
|
876 | const strict = 'strict' in options
|
877 | ? options.strict
|
878 | : this.$__.strictMode;
|
879 |
|
880 | if (adhoc) {
|
881 | adhocs = this.$__.adhocPaths || (this.$__.adhocPaths = {});
|
882 | adhocs[path] = this.schema.interpretAsType(path, type, this.schema.options);
|
883 | }
|
884 |
|
885 | if (typeof path !== 'string') {
|
886 |
|
887 | if (path instanceof Document) {
|
888 | if (path.$__isNested) {
|
889 | path = path.toObject();
|
890 | } else {
|
891 | path = path._doc;
|
892 | }
|
893 | }
|
894 |
|
895 | if (path == null) {
|
896 | const _ = path;
|
897 | path = val;
|
898 | val = _;
|
899 | } else {
|
900 | prefix = val ? val + '.' : '';
|
901 |
|
902 | keys = Object.keys(path);
|
903 | const len = keys.length;
|
904 |
|
905 |
|
906 |
|
907 | const _skipMinimizeTopLevel = get(options, '_skipMinimizeTopLevel', false);
|
908 | if (len === 0 && (!this.schema.options.minimize || _skipMinimizeTopLevel)) {
|
909 | delete options._skipMinimizeTopLevel;
|
910 | if (val) {
|
911 | this.$set(val, {});
|
912 | }
|
913 | return this;
|
914 | }
|
915 |
|
916 | while (i < len) {
|
917 | _handleIndex.call(this, i++);
|
918 | }
|
919 |
|
920 | return this;
|
921 | }
|
922 | } else {
|
923 | this.$__.$setCalled.add(path);
|
924 | }
|
925 |
|
926 | function _handleIndex(i) {
|
927 | key = keys[i];
|
928 | const pathName = prefix + key;
|
929 | pathtype = this.schema.pathType(pathName);
|
930 |
|
931 |
|
932 |
|
933 | if (type === true &&
|
934 | !prefix &&
|
935 | path[key] != null &&
|
936 | pathtype === 'nested' &&
|
937 | this._doc[key] != null &&
|
938 | Object.keys(this._doc[key]).length === 0) {
|
939 | delete this._doc[key];
|
940 |
|
941 | options = Object.assign({}, options, { _skipMinimizeTopLevel: true });
|
942 | }
|
943 |
|
944 | if (typeof path[key] === 'object' &&
|
945 | !utils.isNativeObject(path[key]) &&
|
946 | !utils.isMongooseType(path[key]) &&
|
947 | path[key] != null &&
|
948 | pathtype !== 'virtual' &&
|
949 | pathtype !== 'real' &&
|
950 | pathtype !== 'adhocOrUndefined' &&
|
951 | !(this.$__path(pathName) instanceof MixedSchema) &&
|
952 | !(this.schema.paths[pathName] &&
|
953 | this.schema.paths[pathName].options &&
|
954 | this.schema.paths[pathName].options.ref)) {
|
955 | this.$__.$setCalled.add(prefix + key);
|
956 | this.$set(path[key], prefix + key, constructing, options);
|
957 | } else if (strict) {
|
958 |
|
959 | if (constructing && path[key] === void 0 &&
|
960 | this.get(key) !== void 0) {
|
961 | return;
|
962 | }
|
963 |
|
964 | if (pathtype === 'adhocOrUndefined') {
|
965 | pathtype = getEmbeddedDiscriminatorPath(this, pathName, { typeOnly: true });
|
966 | }
|
967 |
|
968 | if (pathtype === 'real' || pathtype === 'virtual') {
|
969 |
|
970 | let p = path[key];
|
971 | if (this.schema.paths[pathName] &&
|
972 | this.schema.paths[pathName].$isSingleNested &&
|
973 | path[key] instanceof Document) {
|
974 | p = p.toObject({ virtuals: false, transform: false });
|
975 | }
|
976 | this.$set(prefix + key, p, constructing, options);
|
977 | } else if (pathtype === 'nested' && path[key] instanceof Document) {
|
978 | this.$set(prefix + key,
|
979 | path[key].toObject({ transform: false }), constructing, options);
|
980 | } else if (strict === 'throw') {
|
981 | if (pathtype === 'nested') {
|
982 | throw new ObjectExpectedError(key, path[key]);
|
983 | } else {
|
984 | throw new StrictModeError(key);
|
985 | }
|
986 | }
|
987 | } else if (path[key] !== void 0) {
|
988 | this.$set(prefix + key, path[key], constructing, options);
|
989 | }
|
990 | }
|
991 |
|
992 | let pathType = this.schema.pathType(path);
|
993 | if (pathType === 'adhocOrUndefined') {
|
994 | pathType = getEmbeddedDiscriminatorPath(this, path, { typeOnly: true });
|
995 | }
|
996 |
|
997 |
|
998 |
|
999 | val = handleSpreadDoc(val);
|
1000 |
|
1001 | if (pathType === 'nested' && val) {
|
1002 | if (typeof val === 'object' && val != null) {
|
1003 | if (!merge) {
|
1004 | this.$__setValue(path, null);
|
1005 | cleanModifiedSubpaths(this, path);
|
1006 | } else {
|
1007 | return this.$set(val, path, constructing);
|
1008 | }
|
1009 |
|
1010 | const keys = Object.keys(val);
|
1011 | this.$__setValue(path, {});
|
1012 | for (const key of keys) {
|
1013 | this.$set(path + '.' + key, val[key], constructing);
|
1014 | }
|
1015 | this.markModified(path);
|
1016 | cleanModifiedSubpaths(this, path, { skipDocArrays: true });
|
1017 | return this;
|
1018 | }
|
1019 | this.invalidate(path, new MongooseError.CastError('Object', val, path));
|
1020 | return this;
|
1021 | }
|
1022 |
|
1023 | let schema;
|
1024 | const parts = path.indexOf('.') === -1 ? [path] : path.split('.');
|
1025 |
|
1026 |
|
1027 | if (typeof this.schema.aliases[parts[0]] == 'string') {
|
1028 | parts[0] = this.schema.aliases[parts[0]];
|
1029 | }
|
1030 |
|
1031 | if (pathType === 'adhocOrUndefined' && strict) {
|
1032 |
|
1033 | let mixed;
|
1034 |
|
1035 | for (i = 0; i < parts.length; ++i) {
|
1036 | const subpath = parts.slice(0, i + 1).join('.');
|
1037 |
|
1038 |
|
1039 | if (i + 1 < parts.length && this.schema.pathType(subpath) === 'virtual') {
|
1040 | mpath.set(path, val, this);
|
1041 | return this;
|
1042 | }
|
1043 |
|
1044 | schema = this.schema.path(subpath);
|
1045 | if (schema == null) {
|
1046 | continue;
|
1047 | }
|
1048 |
|
1049 | if (schema instanceof MixedSchema) {
|
1050 |
|
1051 | mixed = true;
|
1052 | break;
|
1053 | }
|
1054 | }
|
1055 |
|
1056 | if (schema == null) {
|
1057 |
|
1058 | schema = getEmbeddedDiscriminatorPath(this, path);
|
1059 | }
|
1060 |
|
1061 | if (!mixed && !schema) {
|
1062 | if (strict === 'throw') {
|
1063 | throw new StrictModeError(path);
|
1064 | }
|
1065 | return this;
|
1066 | }
|
1067 | } else if (pathType === 'virtual') {
|
1068 | schema = this.schema.virtualpath(path);
|
1069 | schema.applySetters(val, this);
|
1070 | return this;
|
1071 | } else {
|
1072 | schema = this.$__path(path);
|
1073 | }
|
1074 |
|
1075 |
|
1076 | let cur = this._doc;
|
1077 | let curPath = '';
|
1078 | for (i = 0; i < parts.length - 1; ++i) {
|
1079 | cur = cur[parts[i]];
|
1080 | curPath += (curPath.length > 0 ? '.' : '') + parts[i];
|
1081 | if (!cur) {
|
1082 | this.$set(curPath, {});
|
1083 |
|
1084 |
|
1085 |
|
1086 |
|
1087 |
|
1088 | if (!this.isSelected(curPath)) {
|
1089 | this.unmarkModified(curPath);
|
1090 | }
|
1091 | cur = this.$__getValue(curPath);
|
1092 | }
|
1093 | }
|
1094 |
|
1095 | let pathToMark;
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 | if (parts.length <= 1) {
|
1101 | pathToMark = path;
|
1102 | } else {
|
1103 | for (i = 0; i < parts.length; ++i) {
|
1104 | const subpath = parts.slice(0, i + 1).join('.');
|
1105 | if (this.get(subpath, null, { getters: false }) === null) {
|
1106 | pathToMark = subpath;
|
1107 | break;
|
1108 | }
|
1109 | }
|
1110 |
|
1111 | if (!pathToMark) {
|
1112 | pathToMark = path;
|
1113 | }
|
1114 | }
|
1115 |
|
1116 |
|
1117 | const priorVal = (() => {
|
1118 | if (this.$__.$options.priorDoc != null) {
|
1119 | return this.$__.$options.priorDoc.$__getValue(path);
|
1120 | }
|
1121 | if (constructing) {
|
1122 | return void 0;
|
1123 | }
|
1124 | return this.$__getValue(path);
|
1125 | })();
|
1126 |
|
1127 | if (!schema) {
|
1128 | this.$__set(pathToMark, path, constructing, parts, schema, val, priorVal);
|
1129 | return this;
|
1130 | }
|
1131 |
|
1132 | if (schema.$isSingleNested && val != null && merge) {
|
1133 | if (val instanceof Document) {
|
1134 | val = val.toObject({ virtuals: false, transform: false });
|
1135 | }
|
1136 | const keys = Object.keys(val);
|
1137 | for (const key of keys) {
|
1138 | this.$set(path + '.' + key, val[key], constructing, options);
|
1139 | }
|
1140 |
|
1141 | return this;
|
1142 | }
|
1143 |
|
1144 | let shouldSet = true;
|
1145 | try {
|
1146 |
|
1147 |
|
1148 | const refMatches = (() => {
|
1149 | if (schema.options == null) {
|
1150 | return false;
|
1151 | }
|
1152 | if (!(val instanceof Document)) {
|
1153 | return false;
|
1154 | }
|
1155 | const model = val.constructor;
|
1156 |
|
1157 |
|
1158 | const ref = schema.options.ref;
|
1159 | if (ref != null && (ref === model.modelName || ref === model.baseModelName)) {
|
1160 | return true;
|
1161 | }
|
1162 |
|
1163 |
|
1164 | const refPath = schema.options.refPath;
|
1165 | if (refPath == null) {
|
1166 | return false;
|
1167 | }
|
1168 | const modelName = val.get(refPath);
|
1169 | return modelName === model.modelName || modelName === model.baseModelName;
|
1170 | })();
|
1171 |
|
1172 | let didPopulate = false;
|
1173 | if (refMatches && val instanceof Document) {
|
1174 | this.populated(path, val._id, { [populateModelSymbol]: val.constructor });
|
1175 | didPopulate = true;
|
1176 | }
|
1177 |
|
1178 | let popOpts;
|
1179 | if (schema.options &&
|
1180 | Array.isArray(schema.options[this.schema.options.typeKey]) &&
|
1181 | schema.options[this.schema.options.typeKey].length &&
|
1182 | schema.options[this.schema.options.typeKey][0].ref &&
|
1183 | _isManuallyPopulatedArray(val, schema.options[this.schema.options.typeKey][0].ref)) {
|
1184 | if (this.ownerDocument) {
|
1185 | popOpts = { [populateModelSymbol]: val[0].constructor };
|
1186 | this.ownerDocument().populated(this.$__fullPath(path),
|
1187 | val.map(function(v) { return v._id; }), popOpts);
|
1188 | } else {
|
1189 | popOpts = { [populateModelSymbol]: val[0].constructor };
|
1190 | this.populated(path, val.map(function(v) { return v._id; }), popOpts);
|
1191 | }
|
1192 | didPopulate = true;
|
1193 | }
|
1194 |
|
1195 | if (this.schema.singleNestedPaths[path] == null) {
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 | val = schema.applySetters(val, this, false, priorVal);
|
1201 | }
|
1202 |
|
1203 | if (schema.$isMongooseDocumentArray &&
|
1204 | Array.isArray(val) &&
|
1205 | val.length > 0 &&
|
1206 | val[0] != null &&
|
1207 | val[0].$__ != null &&
|
1208 | val[0].$__.populated != null) {
|
1209 | const populatedPaths = Object.keys(val[0].$__.populated);
|
1210 | for (const populatedPath of populatedPaths) {
|
1211 | this.populated(path + '.' + populatedPath,
|
1212 | val.map(v => v.populated(populatedPath)),
|
1213 | val[0].$__.populated[populatedPath].options);
|
1214 | }
|
1215 | didPopulate = true;
|
1216 | }
|
1217 |
|
1218 | if (!didPopulate && this.$__.populated) {
|
1219 |
|
1220 |
|
1221 | if (Array.isArray(val) && this.$__.populated[path]) {
|
1222 | for (let i = 0; i < val.length; ++i) {
|
1223 | if (val[i] instanceof Document) {
|
1224 | val[i] = val[i]._id;
|
1225 | }
|
1226 | }
|
1227 | }
|
1228 | delete this.$__.populated[path];
|
1229 | }
|
1230 |
|
1231 | this.$markValid(path);
|
1232 | } catch (e) {
|
1233 | if (e instanceof MongooseError.StrictModeError && e.isImmutableError) {
|
1234 | this.invalidate(path, e);
|
1235 | } else {
|
1236 | this.invalidate(path,
|
1237 | new MongooseError.CastError(schema.instance, val, path, e));
|
1238 | }
|
1239 | shouldSet = false;
|
1240 | }
|
1241 |
|
1242 | if (shouldSet) {
|
1243 | this.$__set(pathToMark, path, constructing, parts, schema, val, priorVal);
|
1244 | }
|
1245 |
|
1246 | if (schema.$isSingleNested && (this.isDirectModified(path) || val == null)) {
|
1247 | cleanModifiedSubpaths(this, path);
|
1248 | }
|
1249 |
|
1250 | return this;
|
1251 | };
|
1252 |
|
1253 |
|
1254 |
|
1255 |
|
1256 |
|
1257 | function _isManuallyPopulatedArray(val, ref) {
|
1258 | if (!Array.isArray(val)) {
|
1259 | return false;
|
1260 | }
|
1261 | if (val.length === 0) {
|
1262 | return false;
|
1263 | }
|
1264 |
|
1265 | for (const el of val) {
|
1266 | if (!(el instanceof Document)) {
|
1267 | return false;
|
1268 | }
|
1269 | const modelName = el.constructor.modelName;
|
1270 | if (modelName == null) {
|
1271 | return false;
|
1272 | }
|
1273 | if (el.constructor.modelName != ref && el.constructor.baseModelName != ref) {
|
1274 | return false;
|
1275 | }
|
1276 | }
|
1277 |
|
1278 | return true;
|
1279 | }
|
1280 |
|
1281 |
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 | Document.prototype.set = Document.prototype.$set;
|
1317 |
|
1318 | /**
|
1319 | * Determine if we should mark this change as modified.
|
1320 | *
|
1321 | * @return {Boolean}
|
1322 | * @api private
|
1323 | * @method $__shouldModify
|
1324 | * @memberOf Document
|
1325 | * @instance
|
1326 | */
|
1327 |
|
1328 | Document.prototype.$__shouldModify = function(pathToMark, path, constructing, parts, schema, val, priorVal) {
|
1329 | if (this.isNew) {
|
1330 | return true;
|
1331 | }
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 | if (this.schema.singleNestedPaths[path] != null) {
|
1337 | return false;
|
1338 | }
|
1339 |
|
1340 | if (val === void 0 && !this.isSelected(path)) {
|
1341 |
|
1342 |
|
1343 | return true;
|
1344 | }
|
1345 |
|
1346 | if (val === void 0 && path in this.$__.activePaths.states.default) {
|
1347 |
|
1348 | return false;
|
1349 | }
|
1350 |
|
1351 |
|
1352 |
|
1353 | if (this.populated(path) &&
|
1354 | val instanceof Document &&
|
1355 | deepEqual(val._id, priorVal)) {
|
1356 | return false;
|
1357 | }
|
1358 |
|
1359 | if (!deepEqual(val, priorVal || this.get(path))) {
|
1360 | return true;
|
1361 | }
|
1362 |
|
1363 | if (!constructing &&
|
1364 | val !== null &&
|
1365 | val !== undefined &&
|
1366 | path in this.$__.activePaths.states.default &&
|
1367 | deepEqual(val, schema.getDefault(this, constructing))) {
|
1368 |
|
1369 |
|
1370 | return true;
|
1371 | }
|
1372 | return false;
|
1373 | };
|
1374 |
|
1375 |
|
1376 |
|
1377 |
|
1378 |
|
1379 |
|
1380 |
|
1381 |
|
1382 |
|
1383 |
|
1384 | Document.prototype.$__set = function(pathToMark, path, constructing, parts, schema, val, priorVal) {
|
1385 | Embedded = Embedded || require('./types/embedded');
|
1386 |
|
1387 | const shouldModify = this.$__shouldModify(pathToMark, path, constructing, parts,
|
1388 | schema, val, priorVal);
|
1389 | const _this = this;
|
1390 |
|
1391 | if (shouldModify) {
|
1392 | this.markModified(pathToMark);
|
1393 |
|
1394 |
|
1395 | MongooseArray || (MongooseArray = require('./types/array'));
|
1396 | if (val && val.isMongooseArray) {
|
1397 | val._registerAtomic('$set', val);
|
1398 |
|
1399 |
|
1400 | if (val.isMongooseDocumentArray) {
|
1401 | val.forEach(function(item) {
|
1402 | item && item.__parentArray && (item.__parentArray = val);
|
1403 | });
|
1404 | }
|
1405 |
|
1406 |
|
1407 |
|
1408 | this.$__.activePaths.forEach(function(modifiedPath) {
|
1409 | if (modifiedPath.startsWith(path + '.')) {
|
1410 | _this.$__.activePaths.ignore(modifiedPath);
|
1411 | }
|
1412 | });
|
1413 | }
|
1414 | }
|
1415 |
|
1416 | let obj = this._doc;
|
1417 | let i = 0;
|
1418 | const l = parts.length;
|
1419 | let cur = '';
|
1420 |
|
1421 | for (; i < l; i++) {
|
1422 | const next = i + 1;
|
1423 | const last = next === l;
|
1424 | cur += (cur ? '.' + parts[i] : parts[i]);
|
1425 | if (specialProperties.has(parts[i])) {
|
1426 | return;
|
1427 | }
|
1428 |
|
1429 | if (last) {
|
1430 | if (obj instanceof Map) {
|
1431 | obj.set(parts[i], val);
|
1432 | } else {
|
1433 | obj[parts[i]] = val;
|
1434 | }
|
1435 | } else {
|
1436 | if (utils.isPOJO(obj[parts[i]])) {
|
1437 | obj = obj[parts[i]];
|
1438 | } else if (obj[parts[i]] && obj[parts[i]] instanceof Embedded) {
|
1439 | obj = obj[parts[i]];
|
1440 | } else if (obj[parts[i]] && obj[parts[i]].$isSingleNested) {
|
1441 | obj = obj[parts[i]];
|
1442 | } else if (obj[parts[i]] && Array.isArray(obj[parts[i]])) {
|
1443 | obj = obj[parts[i]];
|
1444 | } else {
|
1445 | obj[parts[i]] = obj[parts[i]] || {};
|
1446 | obj = obj[parts[i]];
|
1447 | }
|
1448 | }
|
1449 | }
|
1450 | };
|
1451 |
|
1452 |
|
1453 |
|
1454 |
|
1455 |
|
1456 |
|
1457 |
|
1458 |
|
1459 | Document.prototype.$__getValue = function(path) {
|
1460 | return utils.getValue(path, this._doc);
|
1461 | };
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 | Document.prototype.$__setValue = function(path, val) {
|
1472 | utils.setValue(path, val, this._doc);
|
1473 | return this;
|
1474 | };
|
1475 |
|
1476 |
|
1477 |
|
1478 |
|
1479 |
|
1480 |
|
1481 |
|
1482 |
|
1483 |
|
1484 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
|
1489 |
|
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 |
|
1495 | Document.prototype.get = function(path, type, options) {
|
1496 | let adhoc;
|
1497 | options = options || {};
|
1498 | if (type) {
|
1499 | adhoc = this.schema.interpretAsType(path, type, this.schema.options);
|
1500 | }
|
1501 |
|
1502 | let schema = this.$__path(path);
|
1503 | if (schema == null) {
|
1504 | schema = this.schema.virtualpath(path);
|
1505 | }
|
1506 | if (schema instanceof MixedSchema) {
|
1507 | const virtual = this.schema.virtualpath(path);
|
1508 | if (virtual != null) {
|
1509 | schema = virtual;
|
1510 | }
|
1511 | }
|
1512 | const pieces = path.split('.');
|
1513 | let obj = this._doc;
|
1514 |
|
1515 | if (schema instanceof VirtualType) {
|
1516 | if (schema.getters.length === 0) {
|
1517 | return void 0;
|
1518 | }
|
1519 | return schema.applyGetters(null, this);
|
1520 | }
|
1521 |
|
1522 |
|
1523 | if (typeof this.schema.aliases[pieces[0]] == 'string') {
|
1524 | pieces[0] = this.schema.aliases[pieces[0]];
|
1525 | }
|
1526 |
|
1527 | for (let i = 0, l = pieces.length; i < l; i++) {
|
1528 | if (obj && obj._doc) {
|
1529 | obj = obj._doc;
|
1530 | }
|
1531 |
|
1532 | if (obj == null) {
|
1533 | obj = void 0;
|
1534 | } else if (obj instanceof Map) {
|
1535 | obj = obj.get(pieces[i], { getters: false });
|
1536 | } else if (i === l - 1) {
|
1537 | obj = utils.getValue(pieces[i], obj);
|
1538 | } else {
|
1539 | obj = obj[pieces[i]];
|
1540 | }
|
1541 | }
|
1542 |
|
1543 | if (adhoc) {
|
1544 | obj = adhoc.cast(obj);
|
1545 | }
|
1546 |
|
1547 | if (schema != null && options.getters !== false) {
|
1548 | obj = schema.applyGetters(obj, this);
|
1549 | } else if (this.schema.nested[path] && options.virtuals) {
|
1550 |
|
1551 | return applyVirtuals(this, utils.clone(obj) || {}, { path: path });
|
1552 | }
|
1553 |
|
1554 | return obj;
|
1555 | };
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
1560 |
|
1561 | Document.prototype[getSymbol] = Document.prototype.get;
|
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 |
|
1573 | Document.prototype.$__path = function(path) {
|
1574 | const adhocs = this.$__.adhocPaths;
|
1575 | const adhocType = adhocs && adhocs.hasOwnProperty(path) ? adhocs[path] : null;
|
1576 |
|
1577 | if (adhocType) {
|
1578 | return adhocType;
|
1579 | }
|
1580 | return this.schema.path(path);
|
1581 | };
|
1582 |
|
1583 |
|
1584 |
|
1585 |
|
1586 |
|
1587 |
|
1588 |
|
1589 |
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 | Document.prototype.markModified = function(path, scope) {
|
1600 | this.$__.activePaths.modify(path);
|
1601 | if (scope != null && !this.ownerDocument) {
|
1602 | this.$__.pathsToScopes[path] = scope;
|
1603 | }
|
1604 | };
|
1605 |
|
1606 |
|
1607 |
|
1608 |
|
1609 |
|
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 |
|
1617 |
|
1618 |
|
1619 | Document.prototype.unmarkModified = function(path) {
|
1620 | this.$__.activePaths.init(path);
|
1621 | delete this.$__.pathsToScopes[path];
|
1622 | };
|
1623 |
|
1624 |
|
1625 |
|
1626 |
|
1627 |
|
1628 |
|
1629 |
|
1630 |
|
1631 |
|
1632 |
|
1633 |
|
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 |
|
1639 |
|
1640 | Document.prototype.$ignore = function(path) {
|
1641 | this.$__.activePaths.ignore(path);
|
1642 | };
|
1643 |
|
1644 |
|
1645 |
|
1646 |
|
1647 |
|
1648 |
|
1649 |
|
1650 |
|
1651 |
|
1652 |
|
1653 |
|
1654 |
|
1655 |
|
1656 |
|
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 |
|
1662 |
|
1663 |
|
1664 |
|
1665 |
|
1666 | Document.prototype.directModifiedPaths = function() {
|
1667 | return Object.keys(this.$__.activePaths.states.modify);
|
1668 | };
|
1669 |
|
1670 |
|
1671 |
|
1672 |
|
1673 |
|
1674 |
|
1675 |
|
1676 |
|
1677 |
|
1678 |
|
1679 |
|
1680 |
|
1681 |
|
1682 |
|
1683 |
|
1684 |
|
1685 |
|
1686 |
|
1687 |
|
1688 |
|
1689 |
|
1690 |
|
1691 |
|
1692 |
|
1693 | Document.prototype.$isEmpty = function(path) {
|
1694 | const isEmptyOptions = {
|
1695 | minimize: true,
|
1696 | virtuals: false,
|
1697 | getters: false,
|
1698 | transform: false
|
1699 | };
|
1700 |
|
1701 | if (arguments.length > 0) {
|
1702 | const v = this.get(path);
|
1703 | if (v == null) {
|
1704 | return true;
|
1705 | }
|
1706 | if (typeof v !== 'object') {
|
1707 | return false;
|
1708 | }
|
1709 | if (utils.isPOJO(v)) {
|
1710 | return _isEmpty(v);
|
1711 | }
|
1712 | return Object.keys(v.toObject(isEmptyOptions)).length === 0;
|
1713 | }
|
1714 |
|
1715 | return Object.keys(this.toObject(isEmptyOptions)).length === 0;
|
1716 | };
|
1717 |
|
1718 | function _isEmpty(v) {
|
1719 | if (v == null) {
|
1720 | return true;
|
1721 | }
|
1722 | if (typeof v !== 'object' || Array.isArray(v)) {
|
1723 | return false;
|
1724 | }
|
1725 | for (const key of Object.keys(v)) {
|
1726 | if (!_isEmpty(v[key])) {
|
1727 | return false;
|
1728 | }
|
1729 | }
|
1730 | return true;
|
1731 | }
|
1732 |
|
1733 |
|
1734 |
|
1735 |
|
1736 |
|
1737 |
|
1738 |
|
1739 |
|
1740 |
|
1741 |
|
1742 | Document.prototype.modifiedPaths = function(options) {
|
1743 | options = options || {};
|
1744 | const directModifiedPaths = Object.keys(this.$__.activePaths.states.modify);
|
1745 | const _this = this;
|
1746 | return directModifiedPaths.reduce(function(list, path) {
|
1747 | const parts = path.split('.');
|
1748 | list = list.concat(parts.reduce(function(chains, part, i) {
|
1749 | return chains.concat(parts.slice(0, i).concat(part).join('.'));
|
1750 | }, []).filter(function(chain) {
|
1751 | return (list.indexOf(chain) === -1);
|
1752 | }));
|
1753 |
|
1754 | if (!options.includeChildren) {
|
1755 | return list;
|
1756 | }
|
1757 |
|
1758 | let cur = _this.get(path);
|
1759 | if (cur != null && typeof cur === 'object') {
|
1760 | if (cur._doc) {
|
1761 | cur = cur._doc;
|
1762 | }
|
1763 | if (Array.isArray(cur)) {
|
1764 | const len = cur.length;
|
1765 | for (let i = 0; i < len; ++i) {
|
1766 | if (list.indexOf(path + '.' + i) === -1) {
|
1767 | list.push(path + '.' + i);
|
1768 | if (cur[i] != null && cur[i].$__) {
|
1769 | const modified = cur[i].modifiedPaths();
|
1770 | for (const childPath of modified) {
|
1771 | list.push(path + '.' + i + '.' + childPath);
|
1772 | }
|
1773 | }
|
1774 | }
|
1775 | }
|
1776 | } else {
|
1777 | Object.keys(cur).
|
1778 | filter(function(key) {
|
1779 | return list.indexOf(path + '.' + key) === -1;
|
1780 | }).
|
1781 | forEach(function(key) {
|
1782 | list.push(path + '.' + key);
|
1783 | });
|
1784 | }
|
1785 | }
|
1786 |
|
1787 | return list;
|
1788 | }, []);
|
1789 | };
|
1790 |
|
1791 |
|
1792 |
|
1793 |
|
1794 |
|
1795 |
|
1796 |
|
1797 |
|
1798 |
|
1799 |
|
1800 |
|
1801 |
|
1802 |
|
1803 |
|
1804 |
|
1805 |
|
1806 |
|
1807 |
|
1808 |
|
1809 |
|
1810 | Document.prototype.isModified = function(paths, modifiedPaths) {
|
1811 | if (paths) {
|
1812 | if (!Array.isArray(paths)) {
|
1813 | paths = paths.split(' ');
|
1814 | }
|
1815 | const modified = modifiedPaths || this.modifiedPaths();
|
1816 | const directModifiedPaths = Object.keys(this.$__.activePaths.states.modify);
|
1817 | const isModifiedChild = paths.some(function(path) {
|
1818 | return !!~modified.indexOf(path);
|
1819 | });
|
1820 | return isModifiedChild || paths.some(function(path) {
|
1821 | return directModifiedPaths.some(function(mod) {
|
1822 | return mod === path || path.startsWith(mod + '.');
|
1823 | });
|
1824 | });
|
1825 | }
|
1826 | return this.$__.activePaths.some('modify');
|
1827 | };
|
1828 |
|
1829 |
|
1830 |
|
1831 |
|
1832 |
|
1833 |
|
1834 |
|
1835 |
|
1836 |
|
1837 |
|
1838 |
|
1839 |
|
1840 |
|
1841 |
|
1842 |
|
1843 |
|
1844 |
|
1845 |
|
1846 | Document.prototype.$isDefault = function(path) {
|
1847 | return (path in this.$__.activePaths.states.default);
|
1848 | };
|
1849 |
|
1850 |
|
1851 |
|
1852 |
|
1853 |
|
1854 |
|
1855 |
|
1856 |
|
1857 |
|
1858 |
|
1859 |
|
1860 |
|
1861 |
|
1862 |
|
1863 |
|
1864 |
|
1865 |
|
1866 |
|
1867 |
|
1868 |
|
1869 |
|
1870 |
|
1871 | Document.prototype.$isDeleted = function(val) {
|
1872 | if (arguments.length === 0) {
|
1873 | return !!this.$__.isDeleted;
|
1874 | }
|
1875 |
|
1876 | this.$__.isDeleted = !!val;
|
1877 | return this;
|
1878 | };
|
1879 |
|
1880 |
|
1881 |
|
1882 |
|
1883 |
|
1884 |
|
1885 |
|
1886 |
|
1887 |
|
1888 |
|
1889 |
|
1890 |
|
1891 |
|
1892 |
|
1893 |
|
1894 | Document.prototype.isDirectModified = function(path) {
|
1895 | return (path in this.$__.activePaths.states.modify);
|
1896 | };
|
1897 |
|
1898 |
|
1899 |
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 |
|
1905 |
|
1906 | Document.prototype.isInit = function(path) {
|
1907 | return (path in this.$__.activePaths.states.init);
|
1908 | };
|
1909 |
|
1910 |
|
1911 |
|
1912 |
|
1913 |
|
1914 |
|
1915 |
|
1916 |
|
1917 |
|
1918 |
|
1919 |
|
1920 |
|
1921 |
|
1922 |
|
1923 |
|
1924 |
|
1925 | Document.prototype.isSelected = function isSelected(path) {
|
1926 | if (this.$__.selected) {
|
1927 | if (path === '_id') {
|
1928 | return this.$__.selected._id !== 0;
|
1929 | }
|
1930 |
|
1931 | const paths = Object.keys(this.$__.selected);
|
1932 | let i = paths.length;
|
1933 | let inclusive = null;
|
1934 | let cur;
|
1935 |
|
1936 | if (i === 1 && paths[0] === '_id') {
|
1937 |
|
1938 | return this.$__.selected._id === 0;
|
1939 | }
|
1940 |
|
1941 | while (i--) {
|
1942 | cur = paths[i];
|
1943 | if (cur === '_id') {
|
1944 | continue;
|
1945 | }
|
1946 | if (!isDefiningProjection(this.$__.selected[cur])) {
|
1947 | continue;
|
1948 | }
|
1949 | inclusive = !!this.$__.selected[cur];
|
1950 | break;
|
1951 | }
|
1952 |
|
1953 | if (inclusive === null) {
|
1954 | return true;
|
1955 | }
|
1956 |
|
1957 | if (path in this.$__.selected) {
|
1958 | return inclusive;
|
1959 | }
|
1960 |
|
1961 | i = paths.length;
|
1962 | const pathDot = path + '.';
|
1963 |
|
1964 | while (i--) {
|
1965 | cur = paths[i];
|
1966 | if (cur === '_id') {
|
1967 | continue;
|
1968 | }
|
1969 |
|
1970 | if (cur.startsWith(pathDot)) {
|
1971 | return inclusive || cur !== pathDot;
|
1972 | }
|
1973 |
|
1974 | if (pathDot.startsWith(cur + '.')) {
|
1975 | return inclusive;
|
1976 | }
|
1977 | }
|
1978 |
|
1979 | return !inclusive;
|
1980 | }
|
1981 |
|
1982 | return true;
|
1983 | };
|
1984 |
|
1985 |
|
1986 |
|
1987 |
|
1988 |
|
1989 |
|
1990 |
|
1991 |
|
1992 |
|
1993 |
|
1994 |
|
1995 |
|
1996 |
|
1997 |
|
1998 |
|
1999 |
|
2000 |
|
2001 |
|
2002 | Document.prototype.isDirectSelected = function isDirectSelected(path) {
|
2003 | if (this.$__.selected) {
|
2004 | if (path === '_id') {
|
2005 | return this.$__.selected._id !== 0;
|
2006 | }
|
2007 |
|
2008 | const paths = Object.keys(this.$__.selected);
|
2009 | let i = paths.length;
|
2010 | let inclusive = null;
|
2011 | let cur;
|
2012 |
|
2013 | if (i === 1 && paths[0] === '_id') {
|
2014 |
|
2015 | return this.$__.selected._id === 0;
|
2016 | }
|
2017 |
|
2018 | while (i--) {
|
2019 | cur = paths[i];
|
2020 | if (cur === '_id') {
|
2021 | continue;
|
2022 | }
|
2023 | if (!isDefiningProjection(this.$__.selected[cur])) {
|
2024 | continue;
|
2025 | }
|
2026 | inclusive = !!this.$__.selected[cur];
|
2027 | break;
|
2028 | }
|
2029 |
|
2030 | if (inclusive === null) {
|
2031 | return true;
|
2032 | }
|
2033 |
|
2034 | if (path in this.$__.selected) {
|
2035 | return inclusive;
|
2036 | }
|
2037 |
|
2038 | return !inclusive;
|
2039 | }
|
2040 |
|
2041 | return true;
|
2042 | };
|
2043 |
|
2044 |
|
2045 |
|
2046 |
|
2047 |
|
2048 |
|
2049 |
|
2050 |
|
2051 |
|
2052 |
|
2053 |
|
2054 |
|
2055 |
|
2056 |
|
2057 |
|
2058 |
|
2059 |
|
2060 |
|
2061 |
|
2062 |
|
2063 |
|
2064 |
|
2065 | Document.prototype.validate = function(pathsToValidate, options, callback) {
|
2066 | let parallelValidate;
|
2067 | this.$op = 'validate';
|
2068 |
|
2069 | if (this.ownerDocument != null) {
|
2070 |
|
2071 | } else if (this.$__.validating) {
|
2072 | parallelValidate = new ParallelValidateError(this, {
|
2073 | parentStack: options && options.parentStack,
|
2074 | conflictStack: this.$__.validating.stack
|
2075 | });
|
2076 | } else {
|
2077 | this.$__.validating = new ParallelValidateError(this, { parentStack: options && options.parentStack });
|
2078 | }
|
2079 |
|
2080 | if (typeof pathsToValidate === 'function') {
|
2081 | callback = pathsToValidate;
|
2082 | options = null;
|
2083 | pathsToValidate = null;
|
2084 | } else if (typeof options === 'function') {
|
2085 | callback = options;
|
2086 | options = pathsToValidate;
|
2087 | pathsToValidate = null;
|
2088 | }
|
2089 |
|
2090 | return promiseOrCallback(callback, cb => {
|
2091 | if (parallelValidate != null) {
|
2092 | return cb(parallelValidate);
|
2093 | }
|
2094 |
|
2095 | this.$__validate(pathsToValidate, options, (error) => {
|
2096 | this.$op = null;
|
2097 | cb(error);
|
2098 | });
|
2099 | }, this.constructor.events);
|
2100 | };
|
2101 |
|
2102 |
|
2103 |
|
2104 |
|
2105 |
|
2106 | function _evaluateRequiredFunctions(doc) {
|
2107 | Object.keys(doc.$__.activePaths.states.require).forEach(path => {
|
2108 | const p = doc.schema.path(path);
|
2109 |
|
2110 | if (p != null && typeof p.originalRequiredValue === 'function') {
|
2111 | doc.$__.cachedRequired[path] = p.originalRequiredValue.call(doc);
|
2112 | }
|
2113 | });
|
2114 | }
|
2115 |
|
2116 |
|
2117 |
|
2118 |
|
2119 |
|
2120 | function _getPathsToValidate(doc) {
|
2121 | const skipSchemaValidators = {};
|
2122 |
|
2123 | _evaluateRequiredFunctions(doc);
|
2124 |
|
2125 |
|
2126 | let paths = new Set(Object.keys(doc.$__.activePaths.states.require).filter(function(path) {
|
2127 | if (!doc.isSelected(path) && !doc.isModified(path)) {
|
2128 | return false;
|
2129 | }
|
2130 | if (path in doc.$__.cachedRequired) {
|
2131 | return doc.$__.cachedRequired[path];
|
2132 | }
|
2133 | return true;
|
2134 | }));
|
2135 |
|
2136 |
|
2137 | function addToPaths(p) { paths.add(p); }
|
2138 | Object.keys(doc.$__.activePaths.states.init).forEach(addToPaths);
|
2139 | Object.keys(doc.$__.activePaths.states.modify).forEach(addToPaths);
|
2140 | Object.keys(doc.$__.activePaths.states.default).forEach(addToPaths);
|
2141 |
|
2142 | const subdocs = doc.$__getAllSubdocs();
|
2143 | const modifiedPaths = doc.modifiedPaths();
|
2144 | for (const subdoc of subdocs) {
|
2145 | if (subdoc.$basePath) {
|
2146 |
|
2147 |
|
2148 | for (const p of paths) {
|
2149 | if (p === null || p.startsWith(subdoc.$basePath + '.')) {
|
2150 | paths.delete(p);
|
2151 | }
|
2152 | }
|
2153 |
|
2154 | if (doc.isModified(subdoc.$basePath, modifiedPaths) &&
|
2155 | !doc.isDirectModified(subdoc.$basePath) &&
|
2156 | !doc.$isDefault(subdoc.$basePath)) {
|
2157 | paths.add(subdoc.$basePath);
|
2158 |
|
2159 | skipSchemaValidators[subdoc.$basePath] = true;
|
2160 | }
|
2161 | }
|
2162 | }
|
2163 |
|
2164 |
|
2165 |
|
2166 |
|
2167 |
|
2168 | for (const path of paths) {
|
2169 | const _pathType = doc.schema.path(path);
|
2170 | if (!_pathType ||
|
2171 | !_pathType.$isMongooseArray ||
|
2172 |
|
2173 |
|
2174 |
|
2175 | (_pathType.$isMongooseDocumentArray && !get(_pathType, 'schemaOptions.required'))) {
|
2176 | continue;
|
2177 | }
|
2178 |
|
2179 | const val = doc.$__getValue(path);
|
2180 | _pushNestedArrayPaths(val, paths, path);
|
2181 | }
|
2182 |
|
2183 | function _pushNestedArrayPaths(val, paths, path) {
|
2184 | if (val != null) {
|
2185 | const numElements = val.length;
|
2186 | for (let j = 0; j < numElements; ++j) {
|
2187 | if (Array.isArray(val[j])) {
|
2188 | _pushNestedArrayPaths(val[j], paths, path + '.' + j);
|
2189 | } else {
|
2190 | paths.add(path + '.' + j);
|
2191 | }
|
2192 | }
|
2193 | }
|
2194 | }
|
2195 |
|
2196 | const flattenOptions = { skipArrays: true };
|
2197 | for (const pathToCheck of paths) {
|
2198 | if (doc.schema.nested[pathToCheck]) {
|
2199 | let _v = doc.$__getValue(pathToCheck);
|
2200 | if (isMongooseObject(_v)) {
|
2201 | _v = _v.toObject({ transform: false });
|
2202 | }
|
2203 | const flat = flatten(_v, pathToCheck, flattenOptions, doc.schema);
|
2204 | Object.keys(flat).forEach(addToPaths);
|
2205 | }
|
2206 | }
|
2207 |
|
2208 |
|
2209 | for (const path of paths) {
|
2210 |
|
2211 |
|
2212 |
|
2213 | if (doc.schema.singleNestedPaths.hasOwnProperty(path)) {
|
2214 | paths.delete(path);
|
2215 | continue;
|
2216 | }
|
2217 | const _pathType = doc.schema.path(path);
|
2218 | if (!_pathType || !_pathType.$isSchemaMap) {
|
2219 | continue;
|
2220 | }
|
2221 |
|
2222 | const val = doc.$__getValue(path);
|
2223 | if (val == null) {
|
2224 | continue;
|
2225 | }
|
2226 | for (const key of val.keys()) {
|
2227 | paths.add(path + '.' + key);
|
2228 | }
|
2229 | }
|
2230 |
|
2231 | paths = Array.from(paths);
|
2232 | return [paths, skipSchemaValidators];
|
2233 | }
|
2234 |
|
2235 |
|
2236 |
|
2237 |
|
2238 |
|
2239 | Document.prototype.$__validate = function(pathsToValidate, options, callback) {
|
2240 | if (typeof pathsToValidate === 'function') {
|
2241 | callback = pathsToValidate;
|
2242 | options = null;
|
2243 | pathsToValidate = null;
|
2244 | } else if (typeof options === 'function') {
|
2245 | callback = options;
|
2246 | options = null;
|
2247 | }
|
2248 |
|
2249 | const hasValidateModifiedOnlyOption = options &&
|
2250 | (typeof options === 'object') &&
|
2251 | ('validateModifiedOnly' in options);
|
2252 |
|
2253 | let shouldValidateModifiedOnly;
|
2254 | if (hasValidateModifiedOnlyOption) {
|
2255 | shouldValidateModifiedOnly = !!options.validateModifiedOnly;
|
2256 | } else {
|
2257 | shouldValidateModifiedOnly = this.schema.options.validateModifiedOnly;
|
2258 | }
|
2259 |
|
2260 | const _this = this;
|
2261 | const _complete = () => {
|
2262 | let validationError = this.$__.validationError;
|
2263 | this.$__.validationError = undefined;
|
2264 |
|
2265 | if (shouldValidateModifiedOnly && validationError != null) {
|
2266 |
|
2267 | const errors = Object.keys(validationError.errors);
|
2268 | for (const errPath of errors) {
|
2269 | if (!this.isModified(errPath)) {
|
2270 | delete validationError.errors[errPath];
|
2271 | }
|
2272 | }
|
2273 | if (Object.keys(validationError.errors).length === 0) {
|
2274 | validationError = void 0;
|
2275 | }
|
2276 | }
|
2277 |
|
2278 | this.$__.cachedRequired = {};
|
2279 | this.emit('validate', _this);
|
2280 | this.constructor.emit('validate', _this);
|
2281 |
|
2282 | this.$__.validating = null;
|
2283 | if (validationError) {
|
2284 | for (const key in validationError.errors) {
|
2285 |
|
2286 | if (!this[documentArrayParent] &&
|
2287 | validationError.errors[key] instanceof MongooseError.CastError) {
|
2288 | this.invalidate(key, validationError.errors[key]);
|
2289 | }
|
2290 | }
|
2291 |
|
2292 | return validationError;
|
2293 | }
|
2294 | };
|
2295 |
|
2296 |
|
2297 | const pathDetails = _getPathsToValidate(this);
|
2298 | let paths = shouldValidateModifiedOnly ?
|
2299 | pathDetails[0].filter((path) => this.isModified(path)) :
|
2300 | pathDetails[0];
|
2301 | const skipSchemaValidators = pathDetails[1];
|
2302 |
|
2303 | if (Array.isArray(pathsToValidate)) {
|
2304 | paths = _handlePathsToValidate(paths, pathsToValidate);
|
2305 | }
|
2306 |
|
2307 | if (paths.length === 0) {
|
2308 | return process.nextTick(function() {
|
2309 | const error = _complete();
|
2310 | if (error) {
|
2311 | return _this.schema.s.hooks.execPost('validate:error', _this, [_this], { error: error }, function(error) {
|
2312 | callback(error);
|
2313 | });
|
2314 | }
|
2315 | callback(null, _this);
|
2316 | });
|
2317 | }
|
2318 |
|
2319 | const validated = {};
|
2320 | let total = 0;
|
2321 |
|
2322 | const complete = function() {
|
2323 | const error = _complete();
|
2324 | if (error) {
|
2325 | return _this.schema.s.hooks.execPost('validate:error', _this, [_this], { error: error }, function(error) {
|
2326 | callback(error);
|
2327 | });
|
2328 | }
|
2329 | callback(null, _this);
|
2330 | };
|
2331 |
|
2332 | const validatePath = function(path) {
|
2333 | if (path == null || validated[path]) {
|
2334 | return;
|
2335 | }
|
2336 |
|
2337 | validated[path] = true;
|
2338 | total++;
|
2339 |
|
2340 | process.nextTick(function() {
|
2341 | const p = _this.schema.path(path);
|
2342 |
|
2343 | if (!p) {
|
2344 | return --total || complete();
|
2345 | }
|
2346 |
|
2347 |
|
2348 | if (!_this.$isValid(path)) {
|
2349 | --total || complete();
|
2350 | return;
|
2351 | }
|
2352 |
|
2353 | let val = _this.$__getValue(path);
|
2354 |
|
2355 |
|
2356 |
|
2357 |
|
2358 | let pop;
|
2359 | if (val == null && (pop = _this.populated(path))) {
|
2360 | val = pop;
|
2361 | }
|
2362 | const scope = path in _this.$__.pathsToScopes ?
|
2363 | _this.$__.pathsToScopes[path] :
|
2364 | _this;
|
2365 |
|
2366 | const doValidateOptions = {
|
2367 | skipSchemaValidators: skipSchemaValidators[path],
|
2368 | path: path
|
2369 | };
|
2370 | p.doValidate(val, function(err) {
|
2371 | if (err && (!p.$isMongooseDocumentArray || err.$isArrayValidatorError)) {
|
2372 | if (p.$isSingleNested &&
|
2373 | err instanceof ValidationError &&
|
2374 | p.schema.options.storeSubdocValidationError === false) {
|
2375 | return --total || complete();
|
2376 | }
|
2377 | _this.invalidate(path, err, undefined, true);
|
2378 | }
|
2379 | --total || complete();
|
2380 | }, scope, doValidateOptions);
|
2381 | });
|
2382 | };
|
2383 |
|
2384 | const numPaths = paths.length;
|
2385 | for (let i = 0; i < numPaths; ++i) {
|
2386 | validatePath(paths[i]);
|
2387 | }
|
2388 | };
|
2389 |
|
2390 |
|
2391 |
|
2392 |
|
2393 |
|
2394 | function _handlePathsToValidate(paths, pathsToValidate) {
|
2395 | const _pathsToValidate = new Set(pathsToValidate);
|
2396 | const parentPaths = new Map([]);
|
2397 | for (const path of pathsToValidate) {
|
2398 | if (path.indexOf('.') === -1) {
|
2399 | continue;
|
2400 | }
|
2401 | const pieces = path.split('.');
|
2402 | let cur = pieces[0];
|
2403 | for (let i = 1; i < pieces.length; ++i) {
|
2404 |
|
2405 |
|
2406 |
|
2407 | parentPaths.set(cur, path);
|
2408 | cur = cur + '.' + pieces[i];
|
2409 | }
|
2410 | }
|
2411 |
|
2412 | const ret = [];
|
2413 | for (const path of paths) {
|
2414 | if (_pathsToValidate.has(path)) {
|
2415 | ret.push(path);
|
2416 | } else if (parentPaths.has(path)) {
|
2417 | ret.push(parentPaths.get(path));
|
2418 | }
|
2419 | }
|
2420 | return ret;
|
2421 | }
|
2422 |
|
2423 |
|
2424 |
|
2425 |
|
2426 |
|
2427 |
|
2428 |
|
2429 |
|
2430 |
|
2431 |
|
2432 |
|
2433 |
|
2434 |
|
2435 |
|
2436 |
|
2437 |
|
2438 |
|
2439 |
|
2440 |
|
2441 |
|
2442 |
|
2443 |
|
2444 | Document.prototype.validateSync = function(pathsToValidate, options) {
|
2445 | const _this = this;
|
2446 |
|
2447 | const hasValidateModifiedOnlyOption = options &&
|
2448 | (typeof options === 'object') &&
|
2449 | ('validateModifiedOnly' in options);
|
2450 |
|
2451 | let shouldValidateModifiedOnly;
|
2452 | if (hasValidateModifiedOnlyOption) {
|
2453 | shouldValidateModifiedOnly = !!options.validateModifiedOnly;
|
2454 | } else {
|
2455 | shouldValidateModifiedOnly = this.schema.options.validateModifiedOnly;
|
2456 | }
|
2457 |
|
2458 | if (typeof pathsToValidate === 'string') {
|
2459 | pathsToValidate = pathsToValidate.split(' ');
|
2460 | }
|
2461 |
|
2462 |
|
2463 | const pathDetails = _getPathsToValidate(this);
|
2464 | let paths = shouldValidateModifiedOnly ?
|
2465 | pathDetails[0].filter((path) => this.isModified(path)) :
|
2466 | pathDetails[0];
|
2467 | const skipSchemaValidators = pathDetails[1];
|
2468 |
|
2469 | if (Array.isArray(pathsToValidate)) {
|
2470 | paths = _handlePathsToValidate(paths, pathsToValidate);
|
2471 | }
|
2472 |
|
2473 | const validating = {};
|
2474 |
|
2475 | paths.forEach(function(path) {
|
2476 | if (validating[path]) {
|
2477 | return;
|
2478 | }
|
2479 |
|
2480 | validating[path] = true;
|
2481 |
|
2482 | const p = _this.schema.path(path);
|
2483 | if (!p) {
|
2484 | return;
|
2485 | }
|
2486 | if (!_this.$isValid(path)) {
|
2487 | return;
|
2488 | }
|
2489 |
|
2490 | const val = _this.$__getValue(path);
|
2491 | const err = p.doValidateSync(val, _this, {
|
2492 | skipSchemaValidators: skipSchemaValidators[path],
|
2493 | path: path
|
2494 | });
|
2495 | if (err && (!p.$isMongooseDocumentArray || err.$isArrayValidatorError)) {
|
2496 | if (p.$isSingleNested &&
|
2497 | err instanceof ValidationError &&
|
2498 | p.schema.options.storeSubdocValidationError === false) {
|
2499 | return;
|
2500 | }
|
2501 | _this.invalidate(path, err, undefined, true);
|
2502 | }
|
2503 | });
|
2504 |
|
2505 | const err = _this.$__.validationError;
|
2506 | _this.$__.validationError = undefined;
|
2507 | _this.emit('validate', _this);
|
2508 | _this.constructor.emit('validate', _this);
|
2509 |
|
2510 | if (err) {
|
2511 | for (const key in err.errors) {
|
2512 |
|
2513 | if (err.errors[key] instanceof MongooseError.CastError) {
|
2514 | _this.invalidate(key, err.errors[key]);
|
2515 | }
|
2516 | }
|
2517 | }
|
2518 |
|
2519 | return err;
|
2520 | };
|
2521 |
|
2522 |
|
2523 |
|
2524 |
|
2525 |
|
2526 |
|
2527 |
|
2528 |
|
2529 |
|
2530 |
|
2531 |
|
2532 |
|
2533 |
|
2534 |
|
2535 |
|
2536 |
|
2537 |
|
2538 |
|
2539 |
|
2540 |
|
2541 |
|
2542 |
|
2543 |
|
2544 |
|
2545 |
|
2546 |
|
2547 |
|
2548 |
|
2549 |
|
2550 |
|
2551 |
|
2552 |
|
2553 | Document.prototype.invalidate = function(path, err, val, kind) {
|
2554 | if (!this.$__.validationError) {
|
2555 | this.$__.validationError = new ValidationError(this);
|
2556 | }
|
2557 |
|
2558 | if (this.$__.validationError.errors[path]) {
|
2559 | return;
|
2560 | }
|
2561 |
|
2562 | if (!err || typeof err === 'string') {
|
2563 | err = new ValidatorError({
|
2564 | path: path,
|
2565 | message: err,
|
2566 | type: kind || 'user defined',
|
2567 | value: val
|
2568 | });
|
2569 | }
|
2570 |
|
2571 | if (this.$__.validationError === err) {
|
2572 | return this.$__.validationError;
|
2573 | }
|
2574 |
|
2575 | this.$__.validationError.addError(path, err);
|
2576 | return this.$__.validationError;
|
2577 | };
|
2578 |
|
2579 |
|
2580 |
|
2581 |
|
2582 |
|
2583 |
|
2584 |
|
2585 |
|
2586 |
|
2587 |
|
2588 |
|
2589 | Document.prototype.$markValid = function(path) {
|
2590 | if (!this.$__.validationError || !this.$__.validationError.errors[path]) {
|
2591 | return;
|
2592 | }
|
2593 |
|
2594 | delete this.$__.validationError.errors[path];
|
2595 | if (Object.keys(this.$__.validationError.errors).length === 0) {
|
2596 | this.$__.validationError = null;
|
2597 | }
|
2598 | };
|
2599 |
|
2600 |
|
2601 |
|
2602 |
|
2603 |
|
2604 |
|
2605 |
|
2606 |
|
2607 |
|
2608 |
|
2609 |
|
2610 |
|
2611 |
|
2612 |
|
2613 |
|
2614 |
|
2615 |
|
2616 |
|
2617 |
|
2618 |
|
2619 |
|
2620 |
|
2621 |
|
2622 |
|
2623 |
|
2624 |
|
2625 |
|
2626 |
|
2627 |
|
2628 |
|
2629 |
|
2630 |
|
2631 |
|
2632 |
|
2633 |
|
2634 |
|
2635 |
|
2636 |
|
2637 |
|
2638 |
|
2639 |
|
2640 |
|
2641 |
|
2642 |
|
2643 | Document.prototype.$isValid = function(path) {
|
2644 | return !this.$__.validationError || !this.$__.validationError.errors[path];
|
2645 | };
|
2646 |
|
2647 |
|
2648 |
|
2649 |
|
2650 |
|
2651 |
|
2652 |
|
2653 |
|
2654 |
|
2655 |
|
2656 |
|
2657 | Document.prototype.$__reset = function reset() {
|
2658 | let _this = this;
|
2659 | DocumentArray || (DocumentArray = require('./types/documentarray'));
|
2660 |
|
2661 | this.$__.activePaths
|
2662 | .map('init', 'modify', function(i) {
|
2663 | return _this.$__getValue(i);
|
2664 | })
|
2665 | .filter(function(val) {
|
2666 | return val && val instanceof Array && val.isMongooseDocumentArray && val.length;
|
2667 | })
|
2668 | .forEach(function(array) {
|
2669 | let i = array.length;
|
2670 | while (i--) {
|
2671 | const doc = array[i];
|
2672 | if (!doc) {
|
2673 | continue;
|
2674 | }
|
2675 | doc.$__reset();
|
2676 | }
|
2677 |
|
2678 | _this.$__.activePaths.init(array.$path());
|
2679 |
|
2680 | array[arrayAtomicsSymbol] = {};
|
2681 | });
|
2682 |
|
2683 | this.$__.activePaths.
|
2684 | map('init', 'modify', function(i) {
|
2685 | return _this.$__getValue(i);
|
2686 | }).
|
2687 | filter(function(val) {
|
2688 | return val && val.$isSingleNested;
|
2689 | }).
|
2690 | forEach(function(doc) {
|
2691 | doc.$__reset();
|
2692 | _this.$__.activePaths.init(doc.$basePath);
|
2693 | });
|
2694 |
|
2695 |
|
2696 | this.$__dirty().forEach(function(dirt) {
|
2697 | const type = dirt.value;
|
2698 |
|
2699 | if (type && type[arrayAtomicsSymbol]) {
|
2700 | type[arrayAtomicsSymbol] = {};
|
2701 | }
|
2702 | });
|
2703 |
|
2704 |
|
2705 | this.$__.activePaths.clear('modify');
|
2706 | this.$__.activePaths.clear('default');
|
2707 | this.$__.validationError = undefined;
|
2708 | this.errors = undefined;
|
2709 | _this = this;
|
2710 | this.schema.requiredPaths().forEach(function(path) {
|
2711 | _this.$__.activePaths.require(path);
|
2712 | });
|
2713 |
|
2714 | return this;
|
2715 | };
|
2716 |
|
2717 |
|
2718 |
|
2719 |
|
2720 |
|
2721 |
|
2722 |
|
2723 |
|
2724 |
|
2725 |
|
2726 | Document.prototype.$__dirty = function() {
|
2727 | const _this = this;
|
2728 |
|
2729 | let all = this.$__.activePaths.map('modify', function(path) {
|
2730 | return {
|
2731 | path: path,
|
2732 | value: _this.$__getValue(path),
|
2733 | schema: _this.$__path(path)
|
2734 | };
|
2735 | });
|
2736 |
|
2737 |
|
2738 |
|
2739 | all = all.concat(this.$__.activePaths.map('default', function(path) {
|
2740 | if (path === '_id' || _this.$__getValue(path) == null) {
|
2741 | return;
|
2742 | }
|
2743 | return {
|
2744 | path: path,
|
2745 | value: _this.$__getValue(path),
|
2746 | schema: _this.$__path(path)
|
2747 | };
|
2748 | }));
|
2749 |
|
2750 |
|
2751 | all.sort(function(a, b) {
|
2752 | return (a.path < b.path ? -1 : (a.path > b.path ? 1 : 0));
|
2753 | });
|
2754 |
|
2755 |
|
2756 | const minimal = [];
|
2757 | let lastPath;
|
2758 | let top;
|
2759 |
|
2760 | all.forEach(function(item) {
|
2761 | if (!item) {
|
2762 | return;
|
2763 | }
|
2764 | if (lastPath == null || item.path.indexOf(lastPath) !== 0) {
|
2765 | lastPath = item.path + '.';
|
2766 | minimal.push(item);
|
2767 | top = item;
|
2768 | } else if (top != null &&
|
2769 | top.value != null &&
|
2770 | top.value[arrayAtomicsSymbol] != null &&
|
2771 | top.value.hasAtomics()) {
|
2772 |
|
2773 |
|
2774 |
|
2775 |
|
2776 | top.value[arrayAtomicsSymbol] = {};
|
2777 | top.value[arrayAtomicsSymbol].$set = top.value;
|
2778 | }
|
2779 | });
|
2780 |
|
2781 | top = lastPath = null;
|
2782 | return minimal;
|
2783 | };
|
2784 |
|
2785 | /**
|
2786 | * Assigns/compiles `schema` into this documents prototype.
|
2787 | *
|
2788 | * @param {Schema} schema
|
2789 | * @api private
|
2790 | * @method $__setSchema
|
2791 | * @memberOf Document
|
2792 | * @instance
|
2793 | */
|
2794 |
|
2795 | Document.prototype.$__setSchema = function(schema) {
|
2796 | schema.plugin(idGetter, { deduplicate: true });
|
2797 | compile(schema.tree, this, undefined, schema.options);
|
2798 |
|
2799 |
|
2800 | for (const key of Object.keys(schema.virtuals)) {
|
2801 | schema.virtuals[key]._applyDefaultGetters();
|
2802 | }
|
2803 |
|
2804 | this.schema = schema;
|
2805 | this[documentSchemaSymbol] = schema;
|
2806 | };
|
2807 |
|
2808 |
|
2809 |
|
2810 |
|
2811 |
|
2812 |
|
2813 |
|
2814 |
|
2815 |
|
2816 |
|
2817 |
|
2818 | Document.prototype.$__getArrayPathsToValidate = function() {
|
2819 | DocumentArray || (DocumentArray = require('./types/documentarray'));
|
2820 |
|
2821 |
|
2822 | return this.$__.activePaths
|
2823 | .map('init', 'modify', function(i) {
|
2824 | return this.$__getValue(i);
|
2825 | }.bind(this))
|
2826 | .filter(function(val) {
|
2827 | return val && val instanceof Array && val.isMongooseDocumentArray && val.length;
|
2828 | }).reduce(function(seed, array) {
|
2829 | return seed.concat(array);
|
2830 | }, [])
|
2831 | .filter(function(doc) {
|
2832 | return doc;
|
2833 | });
|
2834 | };
|
2835 |
|
2836 |
|
2837 |
|
2838 |
|
2839 |
|
2840 |
|
2841 |
|
2842 |
|
2843 |
|
2844 |
|
2845 |
|
2846 | Document.prototype.$__getAllSubdocs = function() {
|
2847 | DocumentArray || (DocumentArray = require('./types/documentarray'));
|
2848 | Embedded = Embedded || require('./types/embedded');
|
2849 |
|
2850 | function docReducer(doc, seed, path) {
|
2851 | let val = doc;
|
2852 | if (path) {
|
2853 | if (doc instanceof Document && doc[documentSchemaSymbol].paths[path]) {
|
2854 | val = doc._doc[path];
|
2855 | } else {
|
2856 | val = doc[path];
|
2857 | }
|
2858 | }
|
2859 | if (val instanceof Embedded) {
|
2860 | seed.push(val);
|
2861 | } else if (val instanceof Map) {
|
2862 | seed = Array.from(val.keys()).reduce(function(seed, path) {
|
2863 | return docReducer(val.get(path), seed, null);
|
2864 | }, seed);
|
2865 | } else if (val && val.$isSingleNested) {
|
2866 | seed = Object.keys(val._doc).reduce(function(seed, path) {
|
2867 | return docReducer(val._doc, seed, path);
|
2868 | }, seed);
|
2869 | seed.push(val);
|
2870 | } else if (val && val.isMongooseDocumentArray) {
|
2871 | val.forEach(function _docReduce(doc) {
|
2872 | if (!doc || !doc._doc) {
|
2873 | return;
|
2874 | }
|
2875 | seed = Object.keys(doc._doc).reduce(function(seed, path) {
|
2876 | return docReducer(doc._doc, seed, path);
|
2877 | }, seed);
|
2878 | if (doc instanceof Embedded) {
|
2879 | seed.push(doc);
|
2880 | }
|
2881 | });
|
2882 | } else if (val instanceof Document && val.$__isNested) {
|
2883 | seed = Object.keys(val).reduce(function(seed, path) {
|
2884 | return docReducer(val, seed, path);
|
2885 | }, seed);
|
2886 | }
|
2887 | return seed;
|
2888 | }
|
2889 |
|
2890 | const _this = this;
|
2891 | const subDocs = Object.keys(this._doc).reduce(function(seed, path) {
|
2892 | return docReducer(_this, seed, path);
|
2893 | }, []);
|
2894 |
|
2895 | return subDocs;
|
2896 | };
|
2897 |
|
2898 |
|
2899 |
|
2900 |
|
2901 |
|
2902 | function applyQueue(doc) {
|
2903 | const q = doc.schema && doc.schema.callQueue;
|
2904 | if (!q.length) {
|
2905 | return;
|
2906 | }
|
2907 | let pair;
|
2908 |
|
2909 | for (let i = 0; i < q.length; ++i) {
|
2910 | pair = q[i];
|
2911 | if (pair[0] !== 'pre' && pair[0] !== 'post' && pair[0] !== 'on') {
|
2912 | doc[pair[0]].apply(doc, pair[1]);
|
2913 | }
|
2914 | }
|
2915 | }
|
2916 |
|
2917 |
|
2918 |
|
2919 |
|
2920 |
|
2921 | Document.prototype.$__handleReject = function handleReject(err) {
|
2922 |
|
2923 | if (this.listeners('error').length) {
|
2924 | this.emit('error', err);
|
2925 | } else if (this.constructor.listeners && this.constructor.listeners('error').length) {
|
2926 | this.constructor.emit('error', err);
|
2927 | } else if (this.listeners && this.listeners('error').length) {
|
2928 | this.emit('error', err);
|
2929 | }
|
2930 | };
|
2931 |
|
2932 |
|
2933 |
|
2934 |
|
2935 |
|
2936 |
|
2937 |
|
2938 |
|
2939 |
|
2940 |
|
2941 | Document.prototype.$toObject = function(options, json) {
|
2942 | let defaultOptions = {
|
2943 | transform: true,
|
2944 | flattenDecimals: true
|
2945 | };
|
2946 |
|
2947 | const path = json ? 'toJSON' : 'toObject';
|
2948 | const baseOptions = get(this, 'constructor.base.options.' + path, {});
|
2949 | const schemaOptions = get(this, 'schema.options', {});
|
2950 |
|
2951 |
|
2952 | defaultOptions = utils.options(defaultOptions, clone(baseOptions));
|
2953 | defaultOptions = utils.options(defaultOptions, clone(schemaOptions[path] || {}));
|
2954 |
|
2955 |
|
2956 | options = utils.isPOJO(options) ? clone(options) : {};
|
2957 |
|
2958 | if (!('flattenMaps' in options)) {
|
2959 | options.flattenMaps = defaultOptions.flattenMaps;
|
2960 | }
|
2961 |
|
2962 | let _minimize;
|
2963 | if (options.minimize != null) {
|
2964 | _minimize = options.minimize;
|
2965 | } else if (defaultOptions.minimize != null) {
|
2966 | _minimize = defaultOptions.minimize;
|
2967 | } else {
|
2968 | _minimize = schemaOptions.minimize;
|
2969 | }
|
2970 |
|
2971 |
|
2972 |
|
2973 |
|
2974 |
|
2975 | const cloneOptions = Object.assign(utils.clone(options), {
|
2976 | _isNested: true,
|
2977 | json: json,
|
2978 | minimize: _minimize
|
2979 | });
|
2980 |
|
2981 | if (utils.hasUserDefinedProperty(options, 'getters')) {
|
2982 | cloneOptions.getters = options.getters;
|
2983 | }
|
2984 | if (utils.hasUserDefinedProperty(options, 'virtuals')) {
|
2985 | cloneOptions.virtuals = options.virtuals;
|
2986 | }
|
2987 |
|
2988 | const depopulate = options.depopulate ||
|
2989 | get(options, '_parentOptions.depopulate', false);
|
2990 | // _isNested will only be true if this is not the top level document, we
|
2991 | // should never depopulate
|
2992 | if (depopulate && options._isNested && this.$__.wasPopulated) {
|
2993 |
|
2994 | return clone(this._id, cloneOptions);
|
2995 | }
|
2996 |
|
2997 |
|
2998 | options = utils.options(defaultOptions, options);
|
2999 | options._isNested = true;
|
3000 | options.json = json;
|
3001 | options.minimize = _minimize;
|
3002 |
|
3003 | cloneOptions._parentOptions = options;
|
3004 | cloneOptions._skipSingleNestedGetters = true;
|
3005 |
|
3006 | const gettersOptions = Object.assign({}, cloneOptions);
|
3007 | gettersOptions._skipSingleNestedGetters = false;
|
3008 |
|
3009 |
|
3010 |
|
3011 | const originalTransform = options.transform;
|
3012 |
|
3013 | let ret = clone(this._doc, cloneOptions) || {};
|
3014 |
|
3015 | if (options.getters) {
|
3016 | applyGetters(this, ret, gettersOptions);
|
3017 |
|
3018 | if (options.minimize) {
|
3019 | ret = minimize(ret) || {};
|
3020 | }
|
3021 | }
|
3022 |
|
3023 | if (options.virtuals || (options.getters && options.virtuals !== false)) {
|
3024 | applyVirtuals(this, ret, gettersOptions, options);
|
3025 | }
|
3026 |
|
3027 | if (options.versionKey === false && this.schema.options.versionKey) {
|
3028 | delete ret[this.schema.options.versionKey];
|
3029 | }
|
3030 |
|
3031 | let transform = options.transform;
|
3032 |
|
3033 |
|
3034 |
|
3035 |
|
3036 |
|
3037 |
|
3038 | if (transform) {
|
3039 | applySchemaTypeTransforms(this, ret, gettersOptions, options);
|
3040 | }
|
3041 |
|
3042 | if (transform === true || (schemaOptions.toObject && transform)) {
|
3043 | const opts = options.json ? schemaOptions.toJSON : schemaOptions.toObject;
|
3044 |
|
3045 | if (opts) {
|
3046 | transform = (typeof options.transform === 'function' ? options.transform : opts.transform);
|
3047 | }
|
3048 | } else {
|
3049 | options.transform = originalTransform;
|
3050 | }
|
3051 |
|
3052 | if (typeof transform === 'function') {
|
3053 | const xformed = transform(this, ret, options);
|
3054 | if (typeof xformed !== 'undefined') {
|
3055 | ret = xformed;
|
3056 | }
|
3057 | }
|
3058 |
|
3059 | return ret;
|
3060 | };
|
3061 |
|
3062 |
|
3063 |
|
3064 |
|
3065 |
|
3066 |
|
3067 |
|
3068 |
|
3069 |
|
3070 |
|
3071 |
|
3072 |
|
3073 |
|
3074 |
|
3075 |
|
3076 |
|
3077 |
|
3078 |
|
3079 |
|
3080 |
|
3081 |
|
3082 |
|
3083 |
|
3084 |
|
3085 |
|
3086 |
|
3087 |
|
3088 |
|
3089 |
|
3090 |
|
3091 |
|
3092 |
|
3093 |
|
3094 |
|
3095 |
|
3096 |
|
3097 |
|
3098 |
|
3099 |
|
3100 |
|
3101 |
|
3102 |
|
3103 |
|
3104 |
|
3105 |
|
3106 |
|
3107 |
|
3108 |
|
3109 |
|
3110 |
|
3111 |
|
3112 |
|
3113 |
|
3114 |
|
3115 |
|
3116 |
|
3117 |
|
3118 |
|
3119 |
|
3120 |
|
3121 |
|
3122 |
|
3123 |
|
3124 |
|
3125 |
|
3126 |
|
3127 |
|
3128 |
|
3129 |
|
3130 |
|
3131 |
|
3132 |
|
3133 |
|
3134 |
|
3135 |
|
3136 |
|
3137 |
|
3138 |
|
3139 |
|
3140 |
|
3141 |
|
3142 |
|
3143 |
|
3144 |
|
3145 |
|
3146 |
|
3147 |
|
3148 |
|
3149 |
|
3150 |
|
3151 |
|
3152 |
|
3153 |
|
3154 |
|
3155 |
|
3156 |
|
3157 |
|
3158 |
|
3159 |
|
3160 |
|
3161 |
|
3162 |
|
3163 |
|
3164 |
|
3165 |
|
3166 |
|
3167 |
|
3168 |
|
3169 |
|
3170 |
|
3171 |
|
3172 |
|
3173 |
|
3174 |
|
3175 |
|
3176 |
|
3177 |
|
3178 |
|
3179 |
|
3180 |
|
3181 |
|
3182 |
|
3183 |
|
3184 |
|
3185 |
|
3186 |
|
3187 |
|
3188 |
|
3189 |
|
3190 |
|
3191 |
|
3192 |
|
3193 |
|
3194 |
|
3195 |
|
3196 |
|
3197 |
|
3198 |
|
3199 |
|
3200 |
|
3201 |
|
3202 |
|
3203 |
|
3204 |
|
3205 |
|
3206 | Document.prototype.toObject = function(options) {
|
3207 | return this.$toObject(options);
|
3208 | };
|
3209 |
|
3210 |
|
3211 |
|
3212 |
|
3213 |
|
3214 |
|
3215 |
|
3216 |
|
3217 | function minimize(obj) {
|
3218 | const keys = Object.keys(obj);
|
3219 | let i = keys.length;
|
3220 | let hasKeys;
|
3221 | let key;
|
3222 | let val;
|
3223 |
|
3224 | while (i--) {
|
3225 | key = keys[i];
|
3226 | val = obj[key];
|
3227 |
|
3228 | if (utils.isObject(val) && !Buffer.isBuffer(val)) {
|
3229 | obj[key] = minimize(val);
|
3230 | }
|
3231 |
|
3232 | if (undefined === obj[key]) {
|
3233 | delete obj[key];
|
3234 | continue;
|
3235 | }
|
3236 |
|
3237 | hasKeys = true;
|
3238 | }
|
3239 |
|
3240 | return hasKeys
|
3241 | ? obj
|
3242 | : undefined;
|
3243 | }
|
3244 |
|
3245 |
|
3246 |
|
3247 |
|
3248 |
|
3249 | function applyVirtuals(self, json, options, toObjectOptions) {
|
3250 | const schema = self.schema;
|
3251 | const paths = Object.keys(schema.virtuals);
|
3252 | let i = paths.length;
|
3253 | const numPaths = i;
|
3254 | let path;
|
3255 | let assignPath;
|
3256 | let cur = self._doc;
|
3257 | let v;
|
3258 | const aliases = get(toObjectOptions, 'aliases', true);
|
3259 |
|
3260 | if (!cur) {
|
3261 | return json;
|
3262 | }
|
3263 |
|
3264 | options = options || {};
|
3265 | for (i = 0; i < numPaths; ++i) {
|
3266 | path = paths[i];
|
3267 |
|
3268 |
|
3269 | if (!aliases && schema.aliases.hasOwnProperty(path)) {
|
3270 | continue;
|
3271 | }
|
3272 |
|
3273 |
|
3274 |
|
3275 |
|
3276 | assignPath = path;
|
3277 | if (options.path != null) {
|
3278 | if (!path.startsWith(options.path + '.')) {
|
3279 | continue;
|
3280 | }
|
3281 | assignPath = path.substr(options.path.length + 1);
|
3282 | }
|
3283 | const parts = assignPath.split('.');
|
3284 | v = clone(self.get(path), options);
|
3285 | if (v === void 0) {
|
3286 | continue;
|
3287 | }
|
3288 | const plen = parts.length;
|
3289 | cur = json;
|
3290 | for (let j = 0; j < plen - 1; ++j) {
|
3291 | cur[parts[j]] = cur[parts[j]] || {};
|
3292 | cur = cur[parts[j]];
|
3293 | }
|
3294 | cur[parts[plen - 1]] = v;
|
3295 | }
|
3296 |
|
3297 | return json;
|
3298 | }
|
3299 |
|
3300 |
|
3301 |
|
3302 |
|
3303 |
|
3304 |
|
3305 |
|
3306 |
|
3307 |
|
3308 | function applyGetters(self, json, options) {
|
3309 | const schema = self.schema;
|
3310 | const paths = Object.keys(schema.paths);
|
3311 | let i = paths.length;
|
3312 | let path;
|
3313 | let cur = self._doc;
|
3314 | let v;
|
3315 |
|
3316 | if (!cur) {
|
3317 | return json;
|
3318 | }
|
3319 |
|
3320 | while (i--) {
|
3321 | path = paths[i];
|
3322 |
|
3323 | const parts = path.split('.');
|
3324 | const plen = parts.length;
|
3325 | const last = plen - 1;
|
3326 | let branch = json;
|
3327 | let part;
|
3328 | cur = self._doc;
|
3329 |
|
3330 | if (!self.isSelected(path)) {
|
3331 | continue;
|
3332 | }
|
3333 |
|
3334 | for (let ii = 0; ii < plen; ++ii) {
|
3335 | part = parts[ii];
|
3336 | v = cur[part];
|
3337 | if (ii === last) {
|
3338 | const val = self.get(path);
|
3339 | branch[part] = clone(val, options);
|
3340 | } else if (v == null) {
|
3341 | if (part in cur) {
|
3342 | branch[part] = v;
|
3343 | }
|
3344 | break;
|
3345 | } else {
|
3346 | branch = branch[part] || (branch[part] = {});
|
3347 | }
|
3348 | cur = v;
|
3349 | }
|
3350 | }
|
3351 |
|
3352 | return json;
|
3353 | }
|
3354 |
|
3355 |
|
3356 |
|
3357 |
|
3358 |
|
3359 |
|
3360 |
|
3361 |
|
3362 |
|
3363 | function applySchemaTypeTransforms(self, json) {
|
3364 | const schema = self.schema;
|
3365 | const paths = Object.keys(schema.paths || {});
|
3366 | const cur = self._doc;
|
3367 |
|
3368 | if (!cur) {
|
3369 | return json;
|
3370 | }
|
3371 |
|
3372 | for (const path of paths) {
|
3373 | const schematype = schema.paths[path];
|
3374 | if (typeof schematype.options.transform === 'function') {
|
3375 | const val = self.get(path);
|
3376 | json[path] = schematype.options.transform.call(self, val);
|
3377 | } else if (schematype.$embeddedSchemaType != null &&
|
3378 | typeof schematype.$embeddedSchemaType.options.transform === 'function') {
|
3379 | const vals = [].concat(self.get(path));
|
3380 | const transform = schematype.$embeddedSchemaType.options.transform;
|
3381 | for (let i = 0; i < vals.length; ++i) {
|
3382 | vals[i] = transform.call(self, vals[i]);
|
3383 | }
|
3384 |
|
3385 | json[path] = vals;
|
3386 | }
|
3387 | }
|
3388 |
|
3389 | return json;
|
3390 | }
|
3391 |
|
3392 |
|
3393 |
|
3394 |
|
3395 |
|
3396 |
|
3397 |
|
3398 |
|
3399 |
|
3400 |
|
3401 |
|
3402 |
|
3403 |
|
3404 |
|
3405 |
|
3406 |
|
3407 |
|
3408 |
|
3409 |
|
3410 | Document.prototype.toJSON = function(options) {
|
3411 | return this.$toObject(options, true);
|
3412 | };
|
3413 |
|
3414 |
|
3415 |
|
3416 |
|
3417 |
|
3418 |
|
3419 |
|
3420 |
|
3421 |
|
3422 |
|
3423 | Document.prototype.inspect = function(options) {
|
3424 | const isPOJO = utils.isPOJO(options);
|
3425 | let opts;
|
3426 | if (isPOJO) {
|
3427 | opts = options;
|
3428 | opts.minimize = false;
|
3429 | }
|
3430 | const ret = this.toObject(opts);
|
3431 |
|
3432 | if (ret == null) {
|
3433 |
|
3434 |
|
3435 | return 'MongooseDocument { ' + ret + ' }';
|
3436 | }
|
3437 |
|
3438 | return ret;
|
3439 | };
|
3440 |
|
3441 | if (inspect.custom) {
|
3442 | |
3443 |
|
3444 |
|
3445 |
|
3446 | Document.prototype[inspect.custom] = Document.prototype.inspect;
|
3447 | }
|
3448 |
|
3449 |
|
3450 |
|
3451 |
|
3452 |
|
3453 |
|
3454 |
|
3455 |
|
3456 |
|
3457 |
|
3458 | Document.prototype.toString = function() {
|
3459 | const ret = this.inspect();
|
3460 | if (typeof ret === 'string') {
|
3461 | return ret;
|
3462 | }
|
3463 | return inspect(ret);
|
3464 | };
|
3465 |
|
3466 |
|
3467 |
|
3468 |
|
3469 |
|
3470 |
|
3471 |
|
3472 |
|
3473 |
|
3474 |
|
3475 |
|
3476 |
|
3477 |
|
3478 |
|
3479 |
|
3480 | Document.prototype.equals = function(doc) {
|
3481 | if (!doc) {
|
3482 | return false;
|
3483 | }
|
3484 |
|
3485 | const tid = this.get('_id');
|
3486 | const docid = doc.get ? doc.get('_id') : doc;
|
3487 | if (!tid && !docid) {
|
3488 | return deepEqual(this, doc);
|
3489 | }
|
3490 | return tid && tid.equals
|
3491 | ? tid.equals(docid)
|
3492 | : tid === docid;
|
3493 | };
|
3494 |
|
3495 |
|
3496 |
|
3497 |
|
3498 |
|
3499 |
|
3500 |
|
3501 |
|
3502 |
|
3503 |
|
3504 |
|
3505 |
|
3506 |
|
3507 |
|
3508 |
|
3509 |
|
3510 |
|
3511 |
|
3512 |
|
3513 |
|
3514 |
|
3515 |
|
3516 |
|
3517 |
|
3518 |
|
3519 |
|
3520 |
|
3521 |
|
3522 |
|
3523 |
|
3524 |
|
3525 |
|
3526 |
|
3527 |
|
3528 |
|
3529 |
|
3530 |
|
3531 |
|
3532 |
|
3533 |
|
3534 |
|
3535 |
|
3536 |
|
3537 |
|
3538 |
|
3539 |
|
3540 | Document.prototype.populate = function populate() {
|
3541 | if (arguments.length === 0) {
|
3542 | return this;
|
3543 | }
|
3544 |
|
3545 | const pop = this.$__.populate || (this.$__.populate = {});
|
3546 | const args = utils.args(arguments);
|
3547 | let fn;
|
3548 |
|
3549 | if (typeof args[args.length - 1] === 'function') {
|
3550 | fn = args.pop();
|
3551 | }
|
3552 |
|
3553 |
|
3554 | if (args.length) {
|
3555 |
|
3556 | const res = utils.populate.apply(null, args);
|
3557 | for (let i = 0; i < res.length; ++i) {
|
3558 | pop[res[i].path] = res[i];
|
3559 | }
|
3560 | }
|
3561 |
|
3562 | if (fn) {
|
3563 | const paths = utils.object.vals(pop);
|
3564 | this.$__.populate = undefined;
|
3565 | let topLevelModel = this.constructor;
|
3566 | if (this.$__isNested) {
|
3567 | topLevelModel = this.$__.scope.constructor;
|
3568 | const nestedPath = this.$__.nestedPath;
|
3569 | paths.forEach(function(populateOptions) {
|
3570 | populateOptions.path = nestedPath + '.' + populateOptions.path;
|
3571 | });
|
3572 | }
|
3573 |
|
3574 |
|
3575 |
|
3576 | if (this.$session() != null) {
|
3577 | const session = this.$session();
|
3578 | paths.forEach(path => {
|
3579 | if (path.options == null) {
|
3580 | path.options = { session: session };
|
3581 | return;
|
3582 | }
|
3583 | if (!('session' in path.options)) {
|
3584 | path.options.session = session;
|
3585 | }
|
3586 | });
|
3587 | }
|
3588 |
|
3589 | topLevelModel.populate(this, paths, fn);
|
3590 | }
|
3591 |
|
3592 | return this;
|
3593 | };
|
3594 |
|
3595 |
|
3596 |
|
3597 |
|
3598 |
|
3599 |
|
3600 |
|
3601 |
|
3602 |
|
3603 |
|
3604 |
|
3605 |
|
3606 |
|
3607 |
|
3608 |
|
3609 |
|
3610 |
|
3611 |
|
3612 |
|
3613 |
|
3614 |
|
3615 |
|
3616 |
|
3617 |
|
3618 |
|
3619 |
|
3620 |
|
3621 |
|
3622 |
|
3623 |
|
3624 | Document.prototype.execPopulate = function(callback) {
|
3625 | return promiseOrCallback(callback, cb => {
|
3626 | this.populate(cb);
|
3627 | }, this.constructor.events);
|
3628 | };
|
3629 |
|
3630 |
|
3631 |
|
3632 |
|
3633 |
|
3634 |
|
3635 |
|
3636 |
|
3637 |
|
3638 |
|
3639 |
|
3640 |
|
3641 |
|
3642 |
|
3643 |
|
3644 |
|
3645 |
|
3646 |
|
3647 |
|
3648 |
|
3649 | Document.prototype.populated = function(path, val, options) {
|
3650 |
|
3651 | if (val === null || val === void 0) {
|
3652 | if (!this.$__.populated) {
|
3653 | return undefined;
|
3654 | }
|
3655 | const v = this.$__.populated[path];
|
3656 | if (v) {
|
3657 | return v.value;
|
3658 | }
|
3659 | return undefined;
|
3660 | }
|
3661 |
|
3662 |
|
3663 | if (val === true) {
|
3664 | if (!this.$__.populated) {
|
3665 | return undefined;
|
3666 | }
|
3667 | return this.$__.populated[path];
|
3668 | }
|
3669 |
|
3670 | this.$__.populated || (this.$__.populated = {});
|
3671 | this.$__.populated[path] = { value: val, options: options };
|
3672 |
|
3673 |
|
3674 |
|
3675 | const pieces = path.split('.');
|
3676 | for (let i = 0; i < pieces.length - 1; ++i) {
|
3677 | const subpath = pieces.slice(0, i + 1).join('.');
|
3678 | const subdoc = this.get(subpath);
|
3679 | if (subdoc != null && subdoc.$__ != null && this.populated(subpath)) {
|
3680 | const rest = pieces.slice(i + 1).join('.');
|
3681 | subdoc.populated(rest, val, options);
|
3682 |
|
3683 |
|
3684 | break;
|
3685 | }
|
3686 | }
|
3687 |
|
3688 | return val;
|
3689 | };
|
3690 |
|
3691 |
|
3692 |
|
3693 |
|
3694 |
|
3695 |
|
3696 |
|
3697 |
|
3698 |
|
3699 |
|
3700 |
|
3701 |
|
3702 |
|
3703 |
|
3704 |
|
3705 |
|
3706 |
|
3707 |
|
3708 |
|
3709 |
|
3710 |
|
3711 |
|
3712 | Document.prototype.depopulate = function(path) {
|
3713 | if (typeof path === 'string') {
|
3714 | path = path.split(' ');
|
3715 | }
|
3716 | let populatedIds;
|
3717 | const virtualKeys = this.$$populatedVirtuals ? Object.keys(this.$$populatedVirtuals) : [];
|
3718 | const populated = get(this, '$__.populated', {});
|
3719 |
|
3720 | if (arguments.length === 0) {
|
3721 |
|
3722 | for (let i = 0; i < virtualKeys.length; i++) {
|
3723 | delete this.$$populatedVirtuals[virtualKeys[i]];
|
3724 | delete this._doc[virtualKeys[i]];
|
3725 | delete populated[virtualKeys[i]];
|
3726 | }
|
3727 |
|
3728 | const keys = Object.keys(populated);
|
3729 |
|
3730 | for (let i = 0; i < keys.length; i++) {
|
3731 | populatedIds = this.populated(keys[i]);
|
3732 | if (!populatedIds) {
|
3733 | continue;
|
3734 | }
|
3735 | delete populated[keys[i]];
|
3736 | this.$set(keys[i], populatedIds);
|
3737 | }
|
3738 | return this;
|
3739 | }
|
3740 |
|
3741 | for (let i = 0; i < path.length; i++) {
|
3742 | populatedIds = this.populated(path[i]);
|
3743 | delete populated[path[i]];
|
3744 |
|
3745 | if (virtualKeys.indexOf(path[i]) !== -1) {
|
3746 | delete this.$$populatedVirtuals[path[i]];
|
3747 | delete this._doc[path[i]];
|
3748 | } else if (populatedIds) {
|
3749 | this.$set(path[i], populatedIds);
|
3750 | }
|
3751 | }
|
3752 | return this;
|
3753 | };
|
3754 |
|
3755 |
|
3756 | /**
|
3757 | * Returns the full path to this document.
|
3758 | *
|
3759 | * @param {String} [path]
|
3760 | * @return {String}
|
3761 | * @api private
|
3762 | * @method $__fullPath
|
3763 | * @memberOf Document
|
3764 | * @instance
|
3765 | */
|
3766 |
|
3767 | Document.prototype.$__fullPath = function(path) {
|
3768 |
|
3769 | return path || '';
|
3770 | };
|
3771 |
|
3772 |
|
3773 |
|
3774 |
|
3775 |
|
3776 | Document.ValidationError = ValidationError;
|
3777 | module.exports = exports = Document;
|