1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const EventEmitter = require('events').EventEmitter;
|
8 | const Kareem = require('kareem');
|
9 | const SchemaType = require('./schematype');
|
10 | const VirtualType = require('./virtualtype');
|
11 | const applyTimestampsToChildren = require('./helpers/update/applyTimestampsToChildren');
|
12 | const applyTimestampsToUpdate = require('./helpers/update/applyTimestampsToUpdate');
|
13 | const get = require('./helpers/get');
|
14 | const getIndexes = require('./helpers/schema/getIndexes');
|
15 | const handleTimestampOption = require('./helpers/schema/handleTimestampOption');
|
16 | const merge = require('./helpers/schema/merge');
|
17 | const mpath = require('mpath');
|
18 | const readPref = require('./driver').get().ReadPreference;
|
19 | const symbols = require('./schema/symbols');
|
20 | const util = require('util');
|
21 | const utils = require('./utils');
|
22 | const validateRef = require('./helpers/populate/validateRef');
|
23 |
|
24 | let MongooseTypes;
|
25 |
|
26 | const queryHooks = require('./helpers/query/applyQueryMiddleware').
|
27 | middlewareFunctions;
|
28 | const documentHooks = require('./helpers/model/applyHooks').middlewareFunctions;
|
29 | const hookNames = queryHooks.concat(documentHooks).
|
30 | reduce((s, hook) => s.add(hook), new Set());
|
31 |
|
32 | let id = 0;
|
33 |
|
34 | /**
|
35 | * Schema constructor.
|
36 | *
|
37 | * ####Example:
|
38 | *
|
39 | * var child = new Schema({ name: String });
|
40 | * var schema = new Schema({ name: String, age: Number, children: [child] });
|
41 | * var Tree = mongoose.model('Tree', schema);
|
42 | *
|
43 | *
|
44 | * new Schema({ name: String }, { _id: false, autoIndex: false })
|
45 | *
|
46 | * ####Options:
|
47 | *
|
48 | * - [autoIndex](/docs/guide.html#autoIndex): bool - defaults to null (which means use the connection's autoIndex option)
|
49 | * - [autoCreate](/docs/guide.html#autoCreate): bool - defaults to null (which means use the connection's autoCreate option)
|
50 | * - [bufferCommands](/docs/guide.html#bufferCommands): bool - defaults to true
|
51 | * - [capped](/docs/guide.html#capped): bool - defaults to false
|
52 | * - [collection](/docs/guide.html#collection): string - no default
|
53 | * - [id](/docs/guide.html#id): bool - defaults to true
|
54 | * - [_id](/docs/guide.html#_id): bool - defaults to true
|
55 | * - `minimize`: bool - controls [document#toObject](#document_Document-toObject) behavior when called manually - defaults to true
|
56 | * - [read](/docs/guide.html#read): string
|
57 | * - [writeConcern](/docs/guide.html#writeConcern): object - defaults to null, use to override [the MongoDB server's default write concern settings](https://docs.mongodb.com/manual/reference/write-concern/)
|
58 | * - [shardKey](/docs/guide.html#shardKey): object - defaults to `null`
|
59 | * - [strict](/docs/guide.html#strict): bool - defaults to true
|
60 | * - [toJSON](/docs/guide.html#toJSON) - object - no default
|
61 | * - [toObject](/docs/guide.html#toObject) - object - no default
|
62 | * - [typeKey](/docs/guide.html#typeKey) - string - defaults to 'type'
|
63 | * - [useNestedStrict](/docs/guide.html#useNestedStrict) - boolean - defaults to false
|
64 | * - [validateBeforeSave](/docs/guide.html#validateBeforeSave) - bool - defaults to `true`
|
65 | * - [versionKey](/docs/guide.html#versionKey): string - defaults to "__v"
|
66 | * - [collation](/docs/guide.html#collation): object - defaults to null (which means use no collation)
|
67 | * - [selectPopulatedPaths](/docs/guide.html#selectPopulatedPaths): boolean - defaults to `true`
|
68 | *
|
69 | * ####Note:
|
70 | *
|
71 | * _When nesting schemas, (`children` in the example above), always declare the child schema first before passing it into its parent._
|
72 | *
|
73 | * @param {Object|Schema|Array} [definition] Can be one of: object describing schema paths, or schema to copy, or array of objects and schemas
|
74 | * @param {Object} [options]
|
75 | * @inherits NodeJS EventEmitter http:
|
76 | * @event `init`: Emitted after the schema is compiled into a `Model`.
|
77 | * @api public
|
78 | */
|
79 |
|
80 | function Schema(obj, options) {
|
81 | if (!(this instanceof Schema)) {
|
82 | return new Schema(obj, options);
|
83 | }
|
84 |
|
85 | this.obj = obj;
|
86 | this.paths = {};
|
87 | this.aliases = {};
|
88 | this.subpaths = {};
|
89 | this.virtuals = {};
|
90 | this.singleNestedPaths = {};
|
91 | this.nested = {};
|
92 | this.inherits = {};
|
93 | this.callQueue = [];
|
94 | this._indexes = [];
|
95 | this.methods = {};
|
96 | this.methodOptions = {};
|
97 | this.statics = {};
|
98 | this.tree = {};
|
99 | this.query = {};
|
100 | this.childSchemas = [];
|
101 | this.plugins = [];
|
102 |
|
103 | this.$id = ++id;
|
104 |
|
105 | this.s = {
|
106 | hooks: new Kareem()
|
107 | };
|
108 |
|
109 | this.options = this.defaultOptions(options);
|
110 |
|
111 |
|
112 | if (Array.isArray(obj)) {
|
113 | for (const definition of obj) {
|
114 | this.add(definition);
|
115 | }
|
116 | } else if (obj) {
|
117 | this.add(obj);
|
118 | }
|
119 |
|
120 |
|
121 | const _idSubDoc = obj && obj._id && utils.isObject(obj._id);
|
122 |
|
123 |
|
124 | const auto_id = !this.paths['_id'] &&
|
125 | (!this.options.noId && this.options._id) && !_idSubDoc;
|
126 |
|
127 | if (auto_id) {
|
128 | const _obj = {_id: {auto: true}};
|
129 | _obj._id[this.options.typeKey] = Schema.ObjectId;
|
130 | this.add(_obj);
|
131 | }
|
132 |
|
133 | this.setupTimestamp(this.options.timestamps);
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function aliasFields(schema, paths) {
|
140 | paths = paths || Object.keys(schema.paths);
|
141 | for (const path of paths) {
|
142 | const options = get(schema.paths[path], 'options');
|
143 | if (options == null) {
|
144 | continue;
|
145 | }
|
146 |
|
147 | const prop = schema.paths[path].path;
|
148 | const alias = options.alias;
|
149 |
|
150 | if (!alias) {
|
151 | continue;
|
152 | }
|
153 |
|
154 | if (typeof alias !== 'string') {
|
155 | throw new Error('Invalid value for alias option on ' + prop + ', got ' + alias);
|
156 | }
|
157 |
|
158 | schema.aliases[alias] = prop;
|
159 |
|
160 | schema.
|
161 | virtual(alias).
|
162 | get((function(p) {
|
163 | return function() {
|
164 | if (typeof this.get === 'function') {
|
165 | return this.get(p);
|
166 | }
|
167 | return this[p];
|
168 | };
|
169 | })(prop)).
|
170 | set((function(p) {
|
171 | return function(v) {
|
172 | return this.set(p, v);
|
173 | };
|
174 | })(prop));
|
175 | }
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | Schema.prototype = Object.create(EventEmitter.prototype);
|
182 | Schema.prototype.constructor = Schema;
|
183 | Schema.prototype.instanceOfSchema = true;
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | Object.defineProperty(Schema.prototype, '$schemaType', {
|
190 | configurable: false,
|
191 | enumerable: false,
|
192 | writable: true
|
193 | });
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | Object.defineProperty(Schema.prototype, 'childSchemas', {
|
210 | configurable: false,
|
211 | enumerable: true,
|
212 | writable: true
|
213 | });
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | Schema.prototype.obj;
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | Schema.prototype.paths;
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | Schema.prototype.tree;
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | Schema.prototype.clone = function() {
|
284 | const s = new Schema({}, this._userProvidedOptions);
|
285 | s.base = this.base;
|
286 | s.obj = this.obj;
|
287 | s.options = utils.clone(this.options);
|
288 | s.callQueue = this.callQueue.map(function(f) { return f; });
|
289 | s.methods = utils.clone(this.methods);
|
290 | s.methodOptions = utils.clone(this.methodOptions);
|
291 | s.statics = utils.clone(this.statics);
|
292 | s.query = utils.clone(this.query);
|
293 | s.plugins = Array.prototype.slice.call(this.plugins);
|
294 | s._indexes = utils.clone(this._indexes);
|
295 | s.s.hooks = this.s.hooks.clone();
|
296 | s._originalSchema = this._originalSchema == null ?
|
297 | this._originalSchema :
|
298 | this._originalSchema.clone();
|
299 |
|
300 | s.tree = utils.clone(this.tree);
|
301 | s.paths = utils.clone(this.paths);
|
302 | s.nested = utils.clone(this.nested);
|
303 | s.subpaths = utils.clone(this.subpaths);
|
304 | s.singleNestedPaths = utils.clone(this.singleNestedPaths);
|
305 | s.childSchemas = gatherChildSchemas(s);
|
306 |
|
307 | s.virtuals = utils.clone(this.virtuals);
|
308 | s.$globalPluginsApplied = this.$globalPluginsApplied;
|
309 | s.$isRootDiscriminator = this.$isRootDiscriminator;
|
310 | s.$implicitlyCreated = this.$implicitlyCreated;
|
311 |
|
312 | if (this.discriminatorMapping != null) {
|
313 | s.discriminatorMapping = Object.assign({}, this.discriminatorMapping);
|
314 | }
|
315 | if (s.discriminators != null) {
|
316 | s.discriminators = Object.assign({}, this.discriminators);
|
317 | }
|
318 |
|
319 | s.aliases = Object.assign({}, this.aliases);
|
320 |
|
321 |
|
322 | s.on('init', v => this.emit('init', v));
|
323 |
|
324 | return s;
|
325 | };
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | Schema.prototype.defaultOptions = function(options) {
|
336 | if (options && options.safe === false) {
|
337 | options.safe = {w: 0};
|
338 | }
|
339 |
|
340 | if (options && options.safe && options.safe.w === 0) {
|
341 |
|
342 | options.versionKey = false;
|
343 | }
|
344 |
|
345 | this._userProvidedOptions = options == null ? {} : utils.clone(options);
|
346 |
|
347 | const baseOptions = get(this, 'base.options', {});
|
348 | options = utils.options({
|
349 | strict: 'strict' in baseOptions ? baseOptions.strict : true,
|
350 | bufferCommands: true,
|
351 | capped: false,
|
352 | versionKey: '__v',
|
353 | discriminatorKey: '__t',
|
354 | minimize: true,
|
355 | autoIndex: null,
|
356 | shardKey: null,
|
357 | read: null,
|
358 | validateBeforeSave: true,
|
359 |
|
360 | noId: false,
|
361 | _id: true,
|
362 | noVirtualId: false,
|
363 | id: true,
|
364 | typeKey: 'type'
|
365 | }, utils.clone(options));
|
366 |
|
367 | if (options.read) {
|
368 | options.read = readPref(options.read);
|
369 | }
|
370 |
|
371 | return options;
|
372 | };
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 | Schema.prototype.add = function add(obj, prefix) {
|
394 | if (obj instanceof Schema) {
|
395 | merge(this, obj);
|
396 | return;
|
397 | }
|
398 |
|
399 |
|
400 |
|
401 |
|
402 | if (obj._id === false && prefix == null) {
|
403 | delete obj._id;
|
404 | this.options._id = false;
|
405 | }
|
406 |
|
407 | prefix = prefix || '';
|
408 | const keys = Object.keys(obj);
|
409 |
|
410 | for (let i = 0; i < keys.length; ++i) {
|
411 | const key = keys[i];
|
412 |
|
413 | if (obj[key] == null) {
|
414 | throw new TypeError('Invalid value for schema path `' + prefix + key + '`');
|
415 | }
|
416 |
|
417 | if (Array.isArray(obj[key]) && obj[key].length === 1 && obj[key][0] == null) {
|
418 | throw new TypeError('Invalid value for schema Array path `' + prefix + key + '`');
|
419 | }
|
420 |
|
421 | if (utils.isPOJO(obj[key]) &&
|
422 | (!obj[key][this.options.typeKey] || (this.options.typeKey === 'type' && obj[key].type.type))) {
|
423 | if (Object.keys(obj[key]).length) {
|
424 |
|
425 | this.nested[prefix + key] = true;
|
426 | this.add(obj[key], prefix + key + '.');
|
427 | } else {
|
428 | if (prefix) {
|
429 | this.nested[prefix.substr(0, prefix.length - 1)] = true;
|
430 | }
|
431 | this.path(prefix + key, obj[key]);
|
432 | }
|
433 | } else {
|
434 | if (prefix) {
|
435 | this.nested[prefix.substr(0, prefix.length - 1)] = true;
|
436 | }
|
437 | this.path(prefix + key, obj[key]);
|
438 | }
|
439 | }
|
440 |
|
441 | const addedKeys = Object.keys(obj).
|
442 | map(key => prefix ? prefix + key : key);
|
443 | aliasFields(this, addedKeys);
|
444 | return this;
|
445 | };
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 | Schema.reserved = Object.create(null);
|
485 | Schema.prototype.reserved = Schema.reserved;
|
486 | const reserved = Schema.reserved;
|
487 |
|
488 | reserved['prototype'] =
|
489 |
|
490 | reserved.emit =
|
491 | reserved.on =
|
492 | reserved.once =
|
493 | reserved.listeners =
|
494 | reserved.removeListener =
|
495 |
|
496 | reserved.collection =
|
497 | reserved.db =
|
498 | reserved.errors =
|
499 | reserved.init =
|
500 | reserved.isModified =
|
501 | reserved.isNew =
|
502 | reserved.get =
|
503 | reserved.modelName =
|
504 | reserved.save =
|
505 | reserved.schema =
|
506 | reserved.toObject =
|
507 | reserved.validate =
|
508 | reserved.remove =
|
509 | reserved.populated =
|
510 |
|
511 | reserved._pres = reserved._posts = 1;
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 | const warnings = {};
|
518 | warnings.increment = '`increment` should not be used as a schema path name ' +
|
519 | 'unless you have disabled versioning.';
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 | Schema.prototype.path = function(path, obj) {
|
538 | if (obj === undefined) {
|
539 | if (this.paths.hasOwnProperty(path)) {
|
540 | return this.paths[path];
|
541 | }
|
542 | if (this.subpaths.hasOwnProperty(path)) {
|
543 | return this.subpaths[path];
|
544 | }
|
545 | if (this.singleNestedPaths.hasOwnProperty(path)) {
|
546 | return this.singleNestedPaths[path];
|
547 | }
|
548 |
|
549 |
|
550 | const mapPath = getMapPath(this, path);
|
551 | if (mapPath != null) {
|
552 | return mapPath;
|
553 | }
|
554 |
|
555 |
|
556 | return /\.\d+\.?.*$/.test(path)
|
557 | ? getPositionalPath(this, path)
|
558 | : undefined;
|
559 | }
|
560 |
|
561 |
|
562 | if (reserved[path]) {
|
563 | throw new Error('`' + path + '` may not be used as a schema pathname');
|
564 | }
|
565 |
|
566 | if (warnings[path]) {
|
567 | console.log('WARN: ' + warnings[path]);
|
568 | }
|
569 |
|
570 | if (typeof obj === 'object' && utils.hasUserDefinedProperty(obj, 'ref')) {
|
571 | validateRef(obj.ref, path);
|
572 | }
|
573 |
|
574 |
|
575 | const subpaths = path.split(/\./);
|
576 | const last = subpaths.pop();
|
577 | let branch = this.tree;
|
578 |
|
579 | subpaths.forEach(function(sub, i) {
|
580 | if (!branch[sub]) {
|
581 | branch[sub] = {};
|
582 | }
|
583 | if (typeof branch[sub] !== 'object') {
|
584 | const msg = 'Cannot set nested path `' + path + '`. '
|
585 | + 'Parent path `'
|
586 | + subpaths.slice(0, i).concat([sub]).join('.')
|
587 | + '` already set to type ' + branch[sub].name
|
588 | + '.';
|
589 | throw new Error(msg);
|
590 | }
|
591 | branch = branch[sub];
|
592 | });
|
593 |
|
594 | branch[last] = utils.clone(obj);
|
595 |
|
596 | this.paths[path] = this.interpretAsType(path, obj, this.options);
|
597 | const schemaType = this.paths[path];
|
598 |
|
599 | if (schemaType.$isSchemaMap) {
|
600 |
|
601 |
|
602 |
|
603 | const mapPath = path + '.$*';
|
604 | this.paths[path + '.$*'] = this.interpretAsType(mapPath,
|
605 | obj.of || { type: {} }, this.options);
|
606 | schemaType.$__schemaType = this.paths[path + '.$*'];
|
607 | }
|
608 |
|
609 | if (schemaType.$isSingleNested) {
|
610 | for (const key in schemaType.schema.paths) {
|
611 | this.singleNestedPaths[path + '.' + key] = schemaType.schema.paths[key];
|
612 | }
|
613 | for (const key in schemaType.schema.singleNestedPaths) {
|
614 | this.singleNestedPaths[path + '.' + key] =
|
615 | schemaType.schema.singleNestedPaths[key];
|
616 | }
|
617 |
|
618 | Object.defineProperty(schemaType.schema, 'base', {
|
619 | configurable: true,
|
620 | enumerable: false,
|
621 | writable: false,
|
622 | value: this.base
|
623 | });
|
624 |
|
625 | schemaType.caster.base = this.base;
|
626 | this.childSchemas.push({
|
627 | schema: schemaType.schema,
|
628 | model: schemaType.caster
|
629 | });
|
630 | } else if (schemaType.$isMongooseDocumentArray) {
|
631 | Object.defineProperty(schemaType.schema, 'base', {
|
632 | configurable: true,
|
633 | enumerable: false,
|
634 | writable: false,
|
635 | value: this.base
|
636 | });
|
637 |
|
638 | schemaType.casterConstructor.base = this.base;
|
639 | this.childSchemas.push({
|
640 | schema: schemaType.schema,
|
641 | model: schemaType.casterConstructor
|
642 | });
|
643 | }
|
644 |
|
645 | return this;
|
646 | };
|
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 | function gatherChildSchemas(schema) {
|
653 | const childSchemas = [];
|
654 |
|
655 | for (const path of Object.keys(schema.paths)) {
|
656 | const schematype = schema.paths[path];
|
657 | if (schematype.$isMongooseDocumentArray || schematype.$isSingleNested) {
|
658 | childSchemas.push({ schema: schematype.schema, model: schematype.caster });
|
659 | }
|
660 | }
|
661 |
|
662 | return childSchemas;
|
663 | }
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 | function getMapPath(schema, path) {
|
670 | for (const _path of Object.keys(schema.paths)) {
|
671 | if (!_path.includes('.$*')) {
|
672 | continue;
|
673 | }
|
674 | const re = new RegExp('^' + _path.replace(/\.\$\*/g, '\\.[^.]+') + '$');
|
675 | if (re.test(path)) {
|
676 | return schema.paths[_path];
|
677 | }
|
678 | }
|
679 |
|
680 | return null;
|
681 | }
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 | Object.defineProperty(Schema.prototype, 'base', {
|
691 | configurable: true,
|
692 | enumerable: false,
|
693 | writable: true,
|
694 | value: null
|
695 | });
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 | Schema.prototype.interpretAsType = function(path, obj, options) {
|
706 | if (obj instanceof SchemaType) {
|
707 | return obj;
|
708 | }
|
709 |
|
710 |
|
711 |
|
712 | const MongooseTypes = this.base != null ? this.base.Schema.Types : Schema.Types;
|
713 |
|
714 | if (obj.constructor) {
|
715 | const constructorName = utils.getFunctionName(obj.constructor);
|
716 | if (constructorName !== 'Object') {
|
717 | const oldObj = obj;
|
718 | obj = {};
|
719 | obj[options.typeKey] = oldObj;
|
720 | }
|
721 | }
|
722 |
|
723 |
|
724 |
|
725 |
|
726 | let type = obj[options.typeKey] && (options.typeKey !== 'type' || !obj.type.type)
|
727 | ? obj[options.typeKey]
|
728 | : {};
|
729 | let name;
|
730 |
|
731 | if (utils.isPOJO(type) || type === 'mixed') {
|
732 | return new MongooseTypes.Mixed(path, obj);
|
733 | }
|
734 |
|
735 | if (Array.isArray(type) || Array === type || type === 'array') {
|
736 |
|
737 | let cast = (Array === type || type === 'array')
|
738 | ? obj.cast
|
739 | : type[0];
|
740 |
|
741 | if (cast && cast.instanceOfSchema) {
|
742 | return new MongooseTypes.DocumentArray(path, cast, obj);
|
743 | }
|
744 | if (cast &&
|
745 | cast[options.typeKey] &&
|
746 | cast[options.typeKey].instanceOfSchema) {
|
747 | return new MongooseTypes.DocumentArray(path, cast[options.typeKey], obj, cast);
|
748 | }
|
749 |
|
750 | if (Array.isArray(cast)) {
|
751 | return new MongooseTypes.Array(path, this.interpretAsType(path, cast, options), obj);
|
752 | }
|
753 |
|
754 | if (typeof cast === 'string') {
|
755 | cast = MongooseTypes[cast.charAt(0).toUpperCase() + cast.substring(1)];
|
756 | } else if (cast && (!cast[options.typeKey] || (options.typeKey === 'type' && cast.type.type))
|
757 | && utils.isPOJO(cast)) {
|
758 | if (Object.keys(cast).length) {
|
759 |
|
760 |
|
761 |
|
762 | const childSchemaOptions = {minimize: options.minimize};
|
763 | if (options.typeKey) {
|
764 | childSchemaOptions.typeKey = options.typeKey;
|
765 | }
|
766 |
|
767 | if (options.hasOwnProperty('strict')) {
|
768 | childSchemaOptions.strict = options.strict;
|
769 | }
|
770 | const childSchema = new Schema(cast, childSchemaOptions);
|
771 | childSchema.$implicitlyCreated = true;
|
772 | return new MongooseTypes.DocumentArray(path, childSchema, obj);
|
773 | } else {
|
774 |
|
775 | return new MongooseTypes.Array(path, MongooseTypes.Mixed, obj);
|
776 | }
|
777 | }
|
778 |
|
779 | if (cast) {
|
780 | type = cast[options.typeKey] && (options.typeKey !== 'type' || !cast.type.type)
|
781 | ? cast[options.typeKey]
|
782 | : cast;
|
783 |
|
784 | name = typeof type === 'string'
|
785 | ? type
|
786 | : type.schemaName || utils.getFunctionName(type);
|
787 |
|
788 | if (!(name in MongooseTypes)) {
|
789 | throw new TypeError('Invalid schema configuration: ' +
|
790 | `\`${name}\` is not a valid type within the array \`${path}\`.` +
|
791 | 'See http://bit.ly/mongoose-schematypes for a list of valid schema types.');
|
792 | }
|
793 | }
|
794 |
|
795 | return new MongooseTypes.Array(path, cast || MongooseTypes.Mixed, obj, options);
|
796 | }
|
797 |
|
798 | if (type && type.instanceOfSchema) {
|
799 | return new MongooseTypes.Embedded(type, path, obj);
|
800 | }
|
801 |
|
802 | if (Buffer.isBuffer(type)) {
|
803 | name = 'Buffer';
|
804 | } else if (typeof type === 'function' || typeof type === 'object') {
|
805 | name = type.schemaName || utils.getFunctionName(type);
|
806 | } else {
|
807 | name = type == null ? '' + type : type.toString();
|
808 | }
|
809 |
|
810 | if (name) {
|
811 | name = name.charAt(0).toUpperCase() + name.substring(1);
|
812 | }
|
813 |
|
814 |
|
815 | if (name === 'ObjectID') {
|
816 | name = 'ObjectId';
|
817 | }
|
818 |
|
819 | if (MongooseTypes[name] == null) {
|
820 | throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
|
821 | `a valid type at path \`${path}\`. See ` +
|
822 | 'http://bit.ly/mongoose-schematypes for a list of valid schema types.');
|
823 | }
|
824 |
|
825 | return new MongooseTypes[name](path, obj);
|
826 | };
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 | Schema.prototype.eachPath = function(fn) {
|
849 | const keys = Object.keys(this.paths);
|
850 | const len = keys.length;
|
851 |
|
852 | for (let i = 0; i < len; ++i) {
|
853 | fn(keys[i], this.paths[keys[i]]);
|
854 | }
|
855 |
|
856 | return this;
|
857 | };
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 | Schema.prototype.requiredPaths = function requiredPaths(invalidate) {
|
876 | if (this._requiredpaths && !invalidate) {
|
877 | return this._requiredpaths;
|
878 | }
|
879 |
|
880 | const paths = Object.keys(this.paths);
|
881 | let i = paths.length;
|
882 | const ret = [];
|
883 |
|
884 | while (i--) {
|
885 | const path = paths[i];
|
886 | if (this.paths[path].isRequired) {
|
887 | ret.push(path);
|
888 | }
|
889 | }
|
890 | this._requiredpaths = ret;
|
891 | return this._requiredpaths;
|
892 | };
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 | Schema.prototype.indexedPaths = function indexedPaths() {
|
902 | if (this._indexedpaths) {
|
903 | return this._indexedpaths;
|
904 | }
|
905 | this._indexedpaths = this.indexes();
|
906 | return this._indexedpaths;
|
907 | };
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 | Schema.prototype.pathType = function(path) {
|
928 | if (path in this.paths) {
|
929 | return 'real';
|
930 | }
|
931 | if (path in this.virtuals) {
|
932 | return 'virtual';
|
933 | }
|
934 | if (path in this.nested) {
|
935 | return 'nested';
|
936 | }
|
937 | if (path in this.subpaths) {
|
938 | return 'real';
|
939 | }
|
940 | if (path in this.singleNestedPaths) {
|
941 | return 'real';
|
942 | }
|
943 |
|
944 |
|
945 | const mapPath = getMapPath(this, path);
|
946 | if (mapPath != null) {
|
947 | return 'real';
|
948 | }
|
949 |
|
950 | if (/\.\d+\.|\.\d+$/.test(path)) {
|
951 | return getPositionalPathType(this, path);
|
952 | }
|
953 | return 'adhocOrUndefined';
|
954 | };
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 | Schema.prototype.hasMixedParent = function(path) {
|
965 | const subpaths = path.split(/\./g);
|
966 | path = '';
|
967 | for (let i = 0; i < subpaths.length; ++i) {
|
968 | path = i > 0 ? path + '.' + subpaths[i] : subpaths[i];
|
969 | if (path in this.paths &&
|
970 | this.paths[path] instanceof MongooseTypes.Mixed) {
|
971 | return true;
|
972 | }
|
973 | }
|
974 |
|
975 | return false;
|
976 | };
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 | Schema.prototype.setupTimestamp = function(timestamps) {
|
985 | const childHasTimestamp = this.childSchemas.find(withTimestamp);
|
986 |
|
987 | function withTimestamp(s) {
|
988 | const ts = s.schema.options.timestamps;
|
989 | return !!ts;
|
990 | }
|
991 |
|
992 | if (!timestamps && !childHasTimestamp) {
|
993 | return;
|
994 | }
|
995 |
|
996 | const createdAt = handleTimestampOption(timestamps, 'createdAt');
|
997 | const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
|
998 | const schemaAdditions = {};
|
999 |
|
1000 | this.$timestamps = { createdAt: createdAt, updatedAt: updatedAt };
|
1001 |
|
1002 | if (updatedAt && !this.paths[updatedAt]) {
|
1003 | schemaAdditions[updatedAt] = Date;
|
1004 | }
|
1005 |
|
1006 | if (createdAt && !this.paths[createdAt]) {
|
1007 | schemaAdditions[createdAt] = Date;
|
1008 | }
|
1009 |
|
1010 | this.add(schemaAdditions);
|
1011 |
|
1012 | this.pre('save', function(next) {
|
1013 | if (get(this, '$__.saveOptions.timestamps') === false) {
|
1014 | return next();
|
1015 | }
|
1016 |
|
1017 | const defaultTimestamp = (this.ownerDocument ? this.ownerDocument() : this).
|
1018 | constructor.base.now();
|
1019 | const auto_id = this._id && this._id.auto;
|
1020 |
|
1021 | if (createdAt && !this.get(createdAt) && this.isSelected(createdAt)) {
|
1022 | this.set(createdAt, auto_id ? this._id.getTimestamp() : defaultTimestamp);
|
1023 | }
|
1024 |
|
1025 | if (updatedAt && (this.isNew || this.isModified())) {
|
1026 | let ts = defaultTimestamp;
|
1027 | if (this.isNew) {
|
1028 | if (createdAt != null) {
|
1029 | ts = this.getValue(createdAt);
|
1030 | } else if (auto_id) {
|
1031 | ts = this._id.getTimestamp();
|
1032 | }
|
1033 | }
|
1034 | this.set(updatedAt, ts);
|
1035 | }
|
1036 |
|
1037 | next();
|
1038 | });
|
1039 |
|
1040 | this.methods.initializeTimestamps = function() {
|
1041 | if (createdAt && !this.get(createdAt)) {
|
1042 | this.set(createdAt, new Date());
|
1043 | }
|
1044 | if (updatedAt && !this.get(updatedAt)) {
|
1045 | this.set(updatedAt, new Date());
|
1046 | }
|
1047 | return this;
|
1048 | };
|
1049 |
|
1050 | _setTimestampsOnUpdate[symbols.builtInMiddleware] = true;
|
1051 |
|
1052 | const opts = { query: true, model: false };
|
1053 | this.pre('findOneAndUpdate', opts, _setTimestampsOnUpdate);
|
1054 | this.pre('replaceOne', opts, _setTimestampsOnUpdate);
|
1055 | this.pre('update', opts, _setTimestampsOnUpdate);
|
1056 | this.pre('updateOne', opts, _setTimestampsOnUpdate);
|
1057 | this.pre('updateMany', opts, _setTimestampsOnUpdate);
|
1058 |
|
1059 | function _setTimestampsOnUpdate(next) {
|
1060 | const now = this.model.base.now();
|
1061 | applyTimestampsToUpdate(now, createdAt, updatedAt, this.getUpdate(),
|
1062 | this.options, this.schema);
|
1063 | applyTimestampsToChildren(now, this.getUpdate(), this.model.schema);
|
1064 | next();
|
1065 | }
|
1066 | };
|
1067 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 | function getPositionalPathType(self, path) {
|
1073 | const subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean);
|
1074 | if (subpaths.length < 2) {
|
1075 | return self.paths.hasOwnProperty(subpaths[0]) ? self.paths[subpaths[0]] : null;
|
1076 | }
|
1077 |
|
1078 | let val = self.path(subpaths[0]);
|
1079 | let isNested = false;
|
1080 | if (!val) {
|
1081 | return val;
|
1082 | }
|
1083 |
|
1084 | const last = subpaths.length - 1;
|
1085 |
|
1086 | for (let i = 1; i < subpaths.length; ++i) {
|
1087 | isNested = false;
|
1088 | const subpath = subpaths[i];
|
1089 |
|
1090 | if (i === last && val && !/\D/.test(subpath)) {
|
1091 | if (val.$isMongooseDocumentArray) {
|
1092 | const oldVal = val;
|
1093 | val = new SchemaType(subpath, {
|
1094 | required: get(val, 'schemaOptions.required', false)
|
1095 | });
|
1096 | val.cast = function(value, doc, init) {
|
1097 | return oldVal.cast(value, doc, init)[0];
|
1098 | };
|
1099 | val.$isMongooseDocumentArrayElement = true;
|
1100 | val.caster = oldVal.caster;
|
1101 | val.schema = oldVal.schema;
|
1102 | } else if (val instanceof MongooseTypes.Array) {
|
1103 |
|
1104 | val = val.caster;
|
1105 | } else {
|
1106 | val = undefined;
|
1107 | }
|
1108 | break;
|
1109 | }
|
1110 |
|
1111 |
|
1112 | if (!/\D/.test(subpath)) {
|
1113 | continue;
|
1114 | }
|
1115 |
|
1116 | if (!(val && val.schema)) {
|
1117 | val = undefined;
|
1118 | break;
|
1119 | }
|
1120 |
|
1121 | const type = val.schema.pathType(subpath);
|
1122 | isNested = (type === 'nested');
|
1123 | val = val.schema.path(subpath);
|
1124 | }
|
1125 |
|
1126 | self.subpaths[path] = val;
|
1127 | if (val) {
|
1128 | return 'real';
|
1129 | }
|
1130 | if (isNested) {
|
1131 | return 'nested';
|
1132 | }
|
1133 | return 'adhocOrUndefined';
|
1134 | }
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 | function getPositionalPath(self, path) {
|
1142 | getPositionalPathType(self, path);
|
1143 | return self.subpaths[path];
|
1144 | }
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 |
|
1155 |
|
1156 |
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 | Schema.prototype.queue = function(name, args) {
|
1163 | this.callQueue.push([name, args]);
|
1164 | return this;
|
1165 | };
|
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 |
|
1175 |
|
1176 |
|
1177 |
|
1178 |
|
1179 |
|
1180 |
|
1181 |
|
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 | Schema.prototype.pre = function(name) {
|
1199 | if (name instanceof RegExp) {
|
1200 | const remainingArgs = Array.prototype.slice.call(arguments, 1);
|
1201 | for (const fn of hookNames) {
|
1202 | if (name.test(fn)) {
|
1203 | this.pre.apply(this, [fn].concat(remainingArgs));
|
1204 | }
|
1205 | }
|
1206 | return this;
|
1207 | }
|
1208 | this.s.hooks.pre.apply(this.s.hooks, arguments);
|
1209 | return this;
|
1210 | };
|
1211 |
|
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 |
|
1227 |
|
1228 |
|
1229 |
|
1230 |
|
1231 |
|
1232 |
|
1233 |
|
1234 |
|
1235 |
|
1236 |
|
1237 |
|
1238 |
|
1239 |
|
1240 |
|
1241 |
|
1242 |
|
1243 |
|
1244 |
|
1245 |
|
1246 |
|
1247 |
|
1248 |
|
1249 | Schema.prototype.post = function(name) {
|
1250 | if (name instanceof RegExp) {
|
1251 | const remainingArgs = Array.prototype.slice.call(arguments, 1);
|
1252 | for (const fn of hookNames) {
|
1253 | if (name.test(fn)) {
|
1254 | this.post.apply(this, [fn].concat(remainingArgs));
|
1255 | }
|
1256 | }
|
1257 | return this;
|
1258 | }
|
1259 | this.s.hooks.post.apply(this.s.hooks, arguments);
|
1260 | return this;
|
1261 | };
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 |
|
1269 |
|
1270 |
|
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 | Schema.prototype.plugin = function(fn, opts) {
|
1279 | if (typeof fn !== 'function') {
|
1280 | throw new Error('First param to `schema.plugin()` must be a function, ' +
|
1281 | 'got "' + (typeof fn) + '"');
|
1282 | }
|
1283 |
|
1284 | if (opts &&
|
1285 | opts.deduplicate) {
|
1286 | for (let i = 0; i < this.plugins.length; ++i) {
|
1287 | if (this.plugins[i].fn === fn) {
|
1288 | return this;
|
1289 | }
|
1290 | }
|
1291 | }
|
1292 | this.plugins.push({ fn: fn, opts: opts });
|
1293 |
|
1294 | fn(this, opts);
|
1295 | return this;
|
1296 | };
|
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 |
|
1322 |
|
1323 |
|
1324 |
|
1325 |
|
1326 |
|
1327 |
|
1328 |
|
1329 |
|
1330 |
|
1331 |
|
1332 | Schema.prototype.method = function(name, fn, options) {
|
1333 | if (typeof name !== 'string') {
|
1334 | for (const i in name) {
|
1335 | this.methods[i] = name[i];
|
1336 | this.methodOptions[i] = utils.clone(options);
|
1337 | }
|
1338 | } else {
|
1339 | this.methods[name] = fn;
|
1340 | this.methodOptions[name] = utils.clone(options);
|
1341 | }
|
1342 | return this;
|
1343 | };
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 |
|
1354 |
|
1355 |
|
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 | Schema.prototype.static = function(name, fn) {
|
1368 | if (typeof name !== 'string') {
|
1369 | for (const i in name) {
|
1370 | this.statics[i] = name[i];
|
1371 | }
|
1372 | } else {
|
1373 | this.statics[name] = fn;
|
1374 | }
|
1375 | return this;
|
1376 | };
|
1377 |
|
1378 |
|
1379 |
|
1380 |
|
1381 |
|
1382 |
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 |
|
1390 |
|
1391 | Schema.prototype.index = function(fields, options) {
|
1392 | fields || (fields = {});
|
1393 | options || (options = {});
|
1394 |
|
1395 | if (options.expires) {
|
1396 | utils.expires(options);
|
1397 | }
|
1398 |
|
1399 | this._indexes.push([fields, options]);
|
1400 | return this;
|
1401 | };
|
1402 |
|
1403 |
|
1404 |
|
1405 |
|
1406 |
|
1407 |
|
1408 |
|
1409 |
|
1410 |
|
1411 |
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 | Schema.prototype.set = function(key, value, _tags) {
|
1419 | if (arguments.length === 1) {
|
1420 | return this.options[key];
|
1421 | }
|
1422 |
|
1423 | switch (key) {
|
1424 | case 'read':
|
1425 | this.options[key] = readPref(value, _tags);
|
1426 | this._userProvidedOptions[key] = this.options[key];
|
1427 | break;
|
1428 | case 'safe':
|
1429 | setSafe(this.options, value);
|
1430 | this._userProvidedOptions[key] = this.options[key];
|
1431 | break;
|
1432 | case 'timestamps':
|
1433 | this.setupTimestamp(value);
|
1434 | this.options[key] = value;
|
1435 | this._userProvidedOptions[key] = this.options[key];
|
1436 | break;
|
1437 | default:
|
1438 | this.options[key] = value;
|
1439 | this._userProvidedOptions[key] = this.options[key];
|
1440 | break;
|
1441 | }
|
1442 |
|
1443 | return this;
|
1444 | };
|
1445 |
|
1446 |
|
1447 |
|
1448 |
|
1449 |
|
1450 | const safeDeprecationWarning = 'Mongoose: The `safe` option for schemas is ' +
|
1451 | 'deprecated. Use the `writeConcern` option instead: ' +
|
1452 | 'http://bit.ly/mongoose-write-concern';
|
1453 |
|
1454 | const setSafe = util.deprecate(function setSafe(options, value) {
|
1455 | options.safe = value === false ?
|
1456 | {w: 0} :
|
1457 | value;
|
1458 | }, safeDeprecationWarning);
|
1459 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 |
|
1473 |
|
1474 | Schema.prototype.get = function(key) {
|
1475 | return this.options[key];
|
1476 | };
|
1477 |
|
1478 |
|
1479 |
|
1480 |
|
1481 |
|
1482 |
|
1483 |
|
1484 |
|
1485 |
|
1486 | const indexTypes = '2d 2dsphere hashed text'.split(' ');
|
1487 |
|
1488 | Object.defineProperty(Schema, 'indexTypes', {
|
1489 | get: function() {
|
1490 | return indexTypes;
|
1491 | },
|
1492 | set: function() {
|
1493 | throw new Error('Cannot overwrite Schema.indexTypes');
|
1494 | }
|
1495 | });
|
1496 |
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 |
|
1506 |
|
1507 |
|
1508 |
|
1509 |
|
1510 |
|
1511 |
|
1512 |
|
1513 |
|
1514 |
|
1515 |
|
1516 | Schema.prototype.indexes = function() {
|
1517 | return getIndexes(this);
|
1518 | };
|
1519 |
|
1520 |
|
1521 |
|
1522 |
|
1523 |
|
1524 |
|
1525 |
|
1526 |
|
1527 |
|
1528 |
|
1529 |
|
1530 |
|
1531 |
|
1532 |
|
1533 | Schema.prototype.virtual = function(name, options) {
|
1534 | if (options && options.ref) {
|
1535 | if (!options.localField) {
|
1536 | throw new Error('Reference virtuals require `localField` option');
|
1537 | }
|
1538 |
|
1539 | if (!options.foreignField) {
|
1540 | throw new Error('Reference virtuals require `foreignField` option');
|
1541 | }
|
1542 |
|
1543 | this.pre('init', function(obj) {
|
1544 | if (mpath.has(name, obj)) {
|
1545 | const _v = mpath.get(name, obj);
|
1546 | if (!this.$$populatedVirtuals) {
|
1547 | this.$$populatedVirtuals = {};
|
1548 | }
|
1549 |
|
1550 | if (options.justOne || options.count) {
|
1551 | this.$$populatedVirtuals[name] = Array.isArray(_v) ?
|
1552 | _v[0] :
|
1553 | _v;
|
1554 | } else {
|
1555 | this.$$populatedVirtuals[name] = Array.isArray(_v) ?
|
1556 | _v :
|
1557 | _v == null ? [] : [_v];
|
1558 | }
|
1559 |
|
1560 | mpath.unset(name, obj);
|
1561 | }
|
1562 | });
|
1563 |
|
1564 | const virtual = this.virtual(name);
|
1565 | virtual.options = options;
|
1566 | return virtual.
|
1567 | get(function() {
|
1568 | if (!this.$$populatedVirtuals) {
|
1569 | this.$$populatedVirtuals = {};
|
1570 | }
|
1571 | if (this.$$populatedVirtuals.hasOwnProperty(name)) {
|
1572 | return this.$$populatedVirtuals[name];
|
1573 | }
|
1574 | return void 0;
|
1575 | }).
|
1576 | set(function(_v) {
|
1577 | if (!this.$$populatedVirtuals) {
|
1578 | this.$$populatedVirtuals = {};
|
1579 | }
|
1580 |
|
1581 | if (options.justOne || options.count) {
|
1582 | this.$$populatedVirtuals[name] = Array.isArray(_v) ?
|
1583 | _v[0] :
|
1584 | _v;
|
1585 |
|
1586 | if (typeof this.$$populatedVirtuals[name] !== 'object') {
|
1587 | this.$$populatedVirtuals[name] = options.count ? _v : null;
|
1588 | }
|
1589 | } else {
|
1590 | this.$$populatedVirtuals[name] = Array.isArray(_v) ?
|
1591 | _v :
|
1592 | _v == null ? [] : [_v];
|
1593 |
|
1594 | this.$$populatedVirtuals[name] = this.$$populatedVirtuals[name].filter(function(doc) {
|
1595 | return doc && typeof doc === 'object';
|
1596 | });
|
1597 | }
|
1598 | });
|
1599 | }
|
1600 |
|
1601 | const virtuals = this.virtuals;
|
1602 | const parts = name.split('.');
|
1603 |
|
1604 | if (this.pathType(name) === 'real') {
|
1605 | throw new Error('Virtual path "' + name + '"' +
|
1606 | ' conflicts with a real path in the schema');
|
1607 | }
|
1608 |
|
1609 | virtuals[name] = parts.reduce(function(mem, part, i) {
|
1610 | mem[part] || (mem[part] = (i === parts.length - 1)
|
1611 | ? new VirtualType(options, name)
|
1612 | : {});
|
1613 | return mem[part];
|
1614 | }, this.tree);
|
1615 |
|
1616 | return virtuals[name];
|
1617 | };
|
1618 |
|
1619 |
|
1620 |
|
1621 |
|
1622 |
|
1623 |
|
1624 |
|
1625 |
|
1626 | Schema.prototype.virtualpath = function(name) {
|
1627 | return this.virtuals.hasOwnProperty(name) ? this.virtuals[name] : null;
|
1628 | };
|
1629 |
|
1630 |
|
1631 |
|
1632 |
|
1633 |
|
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 |
|
1639 |
|
1640 |
|
1641 |
|
1642 |
|
1643 |
|
1644 | Schema.prototype.remove = function(path) {
|
1645 | if (typeof path === 'string') {
|
1646 | path = [path];
|
1647 | }
|
1648 | if (Array.isArray(path)) {
|
1649 | path.forEach(function(name) {
|
1650 | if (this.path(name) == null && !this.nested[name]) {
|
1651 | return;
|
1652 | }
|
1653 | if (this.nested[name]) {
|
1654 | const allKeys = Object.keys(this.paths).
|
1655 | concat(Object.keys(this.nested));
|
1656 | for (const path of allKeys) {
|
1657 | if (path.startsWith(name + '.')) {
|
1658 | delete this.paths[path];
|
1659 | delete this.nested[path];
|
1660 | _deletePath(this, path);
|
1661 | }
|
1662 | }
|
1663 |
|
1664 | delete this.nested[name];
|
1665 | _deletePath(this, name);
|
1666 | return;
|
1667 | }
|
1668 |
|
1669 | delete this.paths[name];
|
1670 | _deletePath(this, name);
|
1671 | }, this);
|
1672 | }
|
1673 | return this;
|
1674 | };
|
1675 |
|
1676 |
|
1677 |
|
1678 |
|
1679 |
|
1680 | function _deletePath(schema, name) {
|
1681 | const pieces = name.split('.');
|
1682 | const last = pieces.pop();
|
1683 | let branch = schema.tree;
|
1684 | for (let i = 0; i < pieces.length; ++i) {
|
1685 | branch = branch[pieces[i]];
|
1686 | }
|
1687 | delete branch[last];
|
1688 | }
|
1689 |
|
1690 |
|
1691 |
|
1692 |
|
1693 |
|
1694 |
|
1695 |
|
1696 |
|
1697 |
|
1698 |
|
1699 |
|
1700 |
|
1701 |
|
1702 |
|
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 |
|
1708 |
|
1709 |
|
1710 |
|
1711 |
|
1712 |
|
1713 |
|
1714 |
|
1715 |
|
1716 |
|
1717 |
|
1718 |
|
1719 |
|
1720 |
|
1721 |
|
1722 |
|
1723 |
|
1724 |
|
1725 |
|
1726 |
|
1727 |
|
1728 | Schema.prototype.loadClass = function(model, virtualsOnly) {
|
1729 | if (model === Object.prototype ||
|
1730 | model === Function.prototype ||
|
1731 | model.prototype.hasOwnProperty('$isMongooseModelPrototype')) {
|
1732 | return this;
|
1733 | }
|
1734 |
|
1735 | this.loadClass(Object.getPrototypeOf(model));
|
1736 |
|
1737 |
|
1738 | if (!virtualsOnly) {
|
1739 | Object.getOwnPropertyNames(model).forEach(function(name) {
|
1740 | if (name.match(/^(length|name|prototype)$/)) {
|
1741 | return;
|
1742 | }
|
1743 | const method = Object.getOwnPropertyDescriptor(model, name);
|
1744 | if (typeof method.value === 'function') {
|
1745 | this.static(name, method.value);
|
1746 | }
|
1747 | }, this);
|
1748 | }
|
1749 |
|
1750 |
|
1751 | Object.getOwnPropertyNames(model.prototype).forEach(function(name) {
|
1752 | if (name.match(/^(constructor)$/)) {
|
1753 | return;
|
1754 | }
|
1755 | const method = Object.getOwnPropertyDescriptor(model.prototype, name);
|
1756 | if (!virtualsOnly) {
|
1757 | if (typeof method.value === 'function') {
|
1758 | this.method(name, method.value);
|
1759 | }
|
1760 | }
|
1761 | if (typeof method.get === 'function') {
|
1762 | this.virtual(name).get(method.get);
|
1763 | }
|
1764 | if (typeof method.set === 'function') {
|
1765 | this.virtual(name).set(method.set);
|
1766 | }
|
1767 | }, this);
|
1768 |
|
1769 | return this;
|
1770 | };
|
1771 |
|
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 | Schema.prototype._getSchema = function(path) {
|
1777 | const _this = this;
|
1778 | const pathschema = _this.path(path);
|
1779 | const resultPath = [];
|
1780 |
|
1781 | if (pathschema) {
|
1782 | pathschema.$fullPath = path;
|
1783 | return pathschema;
|
1784 | }
|
1785 |
|
1786 | function search(parts, schema) {
|
1787 | let p = parts.length + 1;
|
1788 | let foundschema;
|
1789 | let trypath;
|
1790 |
|
1791 | while (p--) {
|
1792 | trypath = parts.slice(0, p).join('.');
|
1793 | foundschema = schema.path(trypath);
|
1794 | if (foundschema) {
|
1795 | resultPath.push(trypath);
|
1796 |
|
1797 | if (foundschema.caster) {
|
1798 |
|
1799 | if (foundschema.caster instanceof MongooseTypes.Mixed) {
|
1800 | foundschema.caster.$fullPath = resultPath.join('.');
|
1801 | return foundschema.caster;
|
1802 | }
|
1803 |
|
1804 |
|
1805 |
|
1806 |
|
1807 |
|
1808 |
|
1809 |
|
1810 | if (p !== parts.length && foundschema.schema) {
|
1811 | let ret;
|
1812 | if (parts[p] === '$' || isArrayFilter(parts[p])) {
|
1813 | if (p + 1 === parts.length) {
|
1814 |
|
1815 | return foundschema;
|
1816 | }
|
1817 |
|
1818 | ret = search(parts.slice(p + 1), foundschema.schema);
|
1819 | if (ret) {
|
1820 | ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
|
1821 | !foundschema.schema.$isSingleNested;
|
1822 | }
|
1823 | return ret;
|
1824 | }
|
1825 |
|
1826 | ret = search(parts.slice(p), foundschema.schema);
|
1827 | if (ret) {
|
1828 | ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
|
1829 | !foundschema.schema.$isSingleNested;
|
1830 | }
|
1831 | return ret;
|
1832 | }
|
1833 | }
|
1834 |
|
1835 | foundschema.$fullPath = resultPath.join('.');
|
1836 |
|
1837 | return foundschema;
|
1838 | }
|
1839 | }
|
1840 | }
|
1841 |
|
1842 |
|
1843 | const parts = path.split('.');
|
1844 | for (let i = 0; i < parts.length; ++i) {
|
1845 | if (parts[i] === '$' || isArrayFilter(parts[i])) {
|
1846 |
|
1847 | parts[i] = '0';
|
1848 | }
|
1849 | }
|
1850 | return search(parts, _this);
|
1851 | };
|
1852 |
|
1853 |
|
1854 |
|
1855 |
|
1856 |
|
1857 | Schema.prototype._getPathType = function(path) {
|
1858 | const _this = this;
|
1859 | const pathschema = _this.path(path);
|
1860 |
|
1861 | if (pathschema) {
|
1862 | return 'real';
|
1863 | }
|
1864 |
|
1865 | function search(parts, schema) {
|
1866 | let p = parts.length + 1,
|
1867 | foundschema,
|
1868 | trypath;
|
1869 |
|
1870 | while (p--) {
|
1871 | trypath = parts.slice(0, p).join('.');
|
1872 | foundschema = schema.path(trypath);
|
1873 | if (foundschema) {
|
1874 | if (foundschema.caster) {
|
1875 |
|
1876 | if (foundschema.caster instanceof MongooseTypes.Mixed) {
|
1877 | return { schema: foundschema, pathType: 'mixed' };
|
1878 | }
|
1879 |
|
1880 |
|
1881 |
|
1882 |
|
1883 |
|
1884 |
|
1885 |
|
1886 | if (p !== parts.length && foundschema.schema) {
|
1887 | if (parts[p] === '$' || isArrayFilter(parts[p])) {
|
1888 | if (p === parts.length - 1) {
|
1889 | return { schema: foundschema, pathType: 'nested' };
|
1890 | }
|
1891 |
|
1892 | return search(parts.slice(p + 1), foundschema.schema);
|
1893 | }
|
1894 |
|
1895 | return search(parts.slice(p), foundschema.schema);
|
1896 | }
|
1897 | return {
|
1898 | schema: foundschema,
|
1899 | pathType: foundschema.$isSingleNested ? 'nested' : 'array'
|
1900 | };
|
1901 | }
|
1902 | return { schema: foundschema, pathType: 'real' };
|
1903 | } else if (p === parts.length && schema.nested[trypath]) {
|
1904 | return { schema: schema, pathType: 'nested' };
|
1905 | }
|
1906 | }
|
1907 | return { schema: foundschema || schema, pathType: 'undefined' };
|
1908 | }
|
1909 |
|
1910 |
|
1911 | return search(path.split('.'), _this);
|
1912 | };
|
1913 |
|
1914 |
|
1915 |
|
1916 |
|
1917 |
|
1918 | function isArrayFilter(piece) {
|
1919 | return piece.indexOf('$[') === 0 &&
|
1920 | piece.lastIndexOf(']') === piece.length - 1;
|
1921 | }
|
1922 |
|
1923 |
|
1924 |
|
1925 |
|
1926 |
|
1927 | module.exports = exports = Schema;
|
1928 |
|
1929 |
|
1930 |
|
1931 |
|
1932 |
|
1933 |
|
1934 |
|
1935 |
|
1936 |
|
1937 |
|
1938 |
|
1939 |
|
1940 |
|
1941 |
|
1942 |
|
1943 |
|
1944 |
|
1945 |
|
1946 |
|
1947 |
|
1948 |
|
1949 |
|
1950 |
|
1951 |
|
1952 |
|
1953 |
|
1954 |
|
1955 |
|
1956 |
|
1957 |
|
1958 | Schema.Types = MongooseTypes = require('./schema/index');
|
1959 |
|
1960 |
|
1961 |
|
1962 |
|
1963 |
|
1964 | exports.ObjectId = MongooseTypes.ObjectId;
|