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

Function SchemaDocumentArray

lib/schema/documentArray.js:42–91  ·  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

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