MCPcopy Create free account
hub / github.com/Automattic/mongoose / DocumentArrayPath

Function DocumentArrayPath

lib/schema/documentarray.js:39–91  ·  view source on GitHub ↗

* SubdocsArray SchemaType constructor * * @param {String} key * @param {Schema} schema * @param {Object} options * @param {Object} schemaOptions * @inherits SchemaArray * @api public

(key, schema, options, schemaOptions)

Source from the content-addressed store, hash-verified

37 */
38
39function DocumentArrayPath(key, schema, options, schemaOptions) {
40 if (schema.options && schema.options.timeseries) {
41 throw new InvalidSchemaOptionError(key, 'timeseries');
42 }
43 const schemaTypeIdOption = DocumentArrayPath.defaultOptions &&
44 DocumentArrayPath.defaultOptions._id;
45 if (schemaTypeIdOption != null) {
46 schemaOptions = schemaOptions || {};
47 schemaOptions._id = schemaTypeIdOption;
48 }
49
50 if (schemaOptions != null && schemaOptions._id != null) {
51 schema = handleIdOption(schema, schemaOptions);
52 } else if (options != null && options._id != null) {
53 schema = handleIdOption(schema, options);
54 }
55
56 const EmbeddedDocument = _createConstructor(schema, options);
57 EmbeddedDocument.prototype.$basePath = key;
58
59 ArrayType.call(this, key, EmbeddedDocument, options);
60
61 this.schema = schema;
62 this.schemaOptions = schemaOptions || {};
63 this.$isMongooseDocumentArray = true;
64 this.Constructor = EmbeddedDocument;
65
66 EmbeddedDocument.base = schema.base;
67
68 const fn = this.defaultValue;
69
70 if (!('defaultValue' in this) || fn !== void 0) {
71 this.default(function() {
72 let arr = fn.call(this);
73 if (arr != null && !Array.isArray(arr)) {
74 arr = [arr];
75 }
76 // Leave it up to `cast()` to convert this to a documentarray
77 return arr;
78 });
79 }
80
81 const $parentSchemaType = this;
82 this.$embeddedSchemaType = new DocumentArrayElement(key + '.$', {
83 required: this &&
84 this.schemaOptions &&
85 this.schemaOptions.required || false,
86 $parentSchemaType
87 });
88
89 this.$embeddedSchemaType.caster = this.Constructor;
90 this.$embeddedSchemaType.schema = this.schema;
91}
92
93/**
94 * This schema type's name, to defend against minifiers that mangle

Callers

nothing calls this directly

Calls 1

_createConstructorFunction · 0.70

Tested by

no test coverage detected