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

Function SchemaDocumentArray

lib/schema/documentArray.js:44–92  ·  view source on GitHub ↗

* SubdocsArray SchemaType constructor * * @param {string} key * @param {Schema} schema * @param {object} options * @param {object} schemaOptions * @param {Schema} parentSchema * @inherits SchemaArray * @api public

(key, schema, options, schemaOptions, parentSchema)

Source from the content-addressed store, hash-verified

42 */
43
44function SchemaDocumentArray(key, schema, options, schemaOptions, parentSchema) {
45 if (schema.options?.timeseries) {
46 throw new InvalidSchemaOptionError(key, 'timeseries');
47 }
48 const schemaTypeIdOption = SchemaDocumentArray.defaultOptions?._id;
49 if (schemaTypeIdOption != null) {
50 schemaOptions = schemaOptions || {};
51 schemaOptions._id = schemaTypeIdOption;
52 }
53
54 if (schemaOptions?._id != null) {
55 schema = handleIdOption(schema, schemaOptions);
56 } else if (options?._id != null) {
57 schema = handleIdOption(schema, options);
58 }
59
60 const Constructor = _createConstructor(schema, options);
61 Constructor.prototype.$basePath = key;
62 Constructor.path = key;
63
64 const $parentSchemaType = this;
65 const embeddedSchemaType = new DocumentArrayElement(key + '.$', schema, {
66 ...(schemaOptions || {}),
67 $parentSchemaType,
68 Constructor
69 });
70 SchemaArray.call(this, key, embeddedSchemaType, options, null, parentSchema);
71
72 this.schema = schema;
73 // EmbeddedDocument schematype options
74 this.schemaOptions = schemaOptions || {};
75 this.$isMongooseDocumentArray = true;
76 this.Constructor = Constructor;
77
78 Constructor.base = schema.base;
79
80 const fn = this.defaultValue;
81
82 if (!('defaultValue' in this) || fn != null) {
83 this.default(function() {
84 let arr = fn.call(this);
85 if (arr != null && !Array.isArray(arr)) {
86 arr = [arr];
87 }
88 // Leave it up to `cast()` to convert this to a documentarray
89 return arr;
90 });
91 }
92}
93
94/**
95 * 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