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

Function SchemaArray

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

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

Callers

nothing calls this directly

Calls 1

cloneFunction · 0.50

Tested by

no test coverage detected