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

Function SchemaArray

lib/schema/array.js:37–102  ·  view source on GitHub ↗

* Array SchemaType constructor * * @param {String} key * @param {SchemaType} cast * @param {Object} options * @inherits SchemaType * @api public

(key, cast, options, schemaOptions)

Source from the content-addressed store, hash-verified

35 */
36
37function SchemaArray(key, cast, options, schemaOptions) {
38 var typeKey = 'type';
39 if (schemaOptions && schemaOptions.typeKey) {
40 typeKey = schemaOptions.typeKey;
41 }
42
43 if (cast) {
44 var castOptions = {};
45
46 if (utils.getFunctionName(cast.constructor) === 'Object') {
47 if (cast[typeKey]) {
48 // support { type: Woot }
49 castOptions = utils.clone(cast); // do not alter user arguments
50 delete castOptions[typeKey];
51 cast = cast[typeKey];
52 } else {
53 cast = Mixed;
54 }
55 }
56
57 // support { type: 'String' }
58 var name = typeof cast === 'string'
59 ? cast
60 : utils.getFunctionName(cast);
61
62 var caster = name in Types
63 ? Types[name]
64 : cast;
65
66 this.casterConstructor = caster;
67 if (typeof caster === 'function' && !caster.$isArraySubdocument) {
68 this.caster = new caster(null, castOptions);
69 } else {
70 this.caster = caster;
71 }
72
73 if (!(this.caster instanceof EmbeddedDoc)) {
74 this.caster.path = key;
75 }
76 }
77
78 this.$isMongooseArray = true;
79
80 SchemaType.call(this, key, options, 'Array');
81
82 var defaultArr;
83 var fn;
84
85 if (this.defaultValue != null) {
86 defaultArr = this.defaultValue;
87 fn = typeof defaultArr === 'function';
88 }
89
90 if (!('defaultValue' in this) || this.defaultValue !== void 0) {
91 this.default(function() {
92 var arr = [];
93 if (fn) {
94 arr = defaultArr();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected