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

Function SchemaArray

lib/schema/array.js:44–123  ·  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

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

Callers

nothing calls this directly

Calls 1

cloneFunction · 0.50

Tested by

no test coverage detected