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

Function SchemaArray

lib/schema/array.js:46–125  ·  view source on GitHub ↗

* Array SchemaType constructor * * @param {string} key * @param {SchemaType} cast * @param {object} options * @param {object} schemaOptions * @param {Schema} parentSchema * @inherits SchemaType * @api public

(key, cast, options, schemaOptions, parentSchema)

Source from the content-addressed store, hash-verified

44 */
45
46function SchemaArray(key, cast, options, schemaOptions, parentSchema) {
47 // lazy load
48 EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded);
49
50 let typeKey = 'type';
51 if (schemaOptions?.typeKey) {
52 typeKey = schemaOptions.typeKey;
53 }
54 this.schemaOptions = schemaOptions;
55
56 if (cast) {
57 let castOptions = {};
58
59 if (utils.isPOJO(cast)) {
60 if (cast[typeKey]) {
61 // support { type: Woot }
62 castOptions = clone(cast); // do not alter user arguments
63 delete castOptions[typeKey];
64 cast = cast[typeKey];
65 } else {
66 cast = Mixed;
67 }
68 }
69
70 if (options?.ref != null && castOptions.ref == null) {
71 castOptions.ref = options.ref;
72 }
73
74 if (cast === Object) {
75 cast = Mixed;
76 }
77
78 // support { type: 'String' }
79 const name = typeof cast === 'string'
80 ? cast
81 : utils.getFunctionName(cast);
82
83 const Types = require('./index.js');
84 const schemaTypeDefinition = Object.hasOwn(Types, name) ? Types[name] : cast;
85
86 if (typeof schemaTypeDefinition === 'function') {
87 if (schemaTypeDefinition === SchemaArray) {
88 this.embeddedSchemaType = new schemaTypeDefinition(key, castOptions, schemaOptions, null, parentSchema);
89 } else {
90 this.embeddedSchemaType = new schemaTypeDefinition(key, castOptions, schemaOptions, parentSchema);
91 }
92 } else if (schemaTypeDefinition instanceof SchemaType) {
93 this.embeddedSchemaType = schemaTypeDefinition;
94 if (!(this.embeddedSchemaType instanceof EmbeddedDoc)) {
95 this.embeddedSchemaType.path = key;
96 }
97 }
98
99 }
100
101 this.$isMongooseArray = true;
102
103 SchemaType.call(this, key, options, 'Array', parentSchema);

Callers

nothing calls this directly

Calls 1

cloneFunction · 0.50

Tested by

no test coverage detected