/
array.js
236 lines (204 loc) · 5.45 KB
/
array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*!
* Module dependencies.
*/
var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, NumberSchema = require('./number')
, Types = {
Boolean: require('./boolean')
, Date: require('./date')
, Number: require('./number')
, String: require('./string')
, ObjectId: require('./objectid')
, Buffer: require('./buffer')
}
, MongooseArray = require('../types').Array
, Mixed = require('./mixed')
, Query = require('../query')
, isMongooseObject = require('../utils').isMongooseObject
/**
* Array SchemaType constructor
*
* @param {String} key
* @param {SchemaType} cast
* @param {Object} options
* @inherits SchemaType
* @api private
*/
function SchemaArray (key, cast, options) {
if (cast) {
var castOptions = {};
if ('Object' === cast.constructor.name) {
if (cast.type) {
// support { type: Woot }
castOptions = cast;
cast = cast.type;
delete castOptions.type;
} else {
cast = Mixed;
}
}
var caster = cast.name in Types ? Types[cast.name] : cast;
this.casterConstructor = caster;
this.caster = new caster(null, castOptions);
if ('EmbeddedDocument' !== this.caster.constructor.name) {
this.caster.path = key;
}
}
SchemaType.call(this, key, options);
var self = this
, defaultArr
, fn;
if (this.defaultValue) {
defaultArr = this.defaultValue;
fn = 'function' == typeof defaultArr;
}
this.default(function(){
var arr = fn ? defaultArr() : defaultArr || [];
return new MongooseArray(arr, self.path, this);
});
};
/*!
* Inherits from SchemaType.
*/
SchemaArray.prototype.__proto__ = SchemaType.prototype;
/**
* Check required
*
* @param {Array} value
* @api private
*/
SchemaArray.prototype.checkRequired = function (value) {
return !!(value && value.length);
};
/**
* Overrides the getters application for the population special-case
*
* @param {Object} value
* @param {Object} scope
* @api private
*/
SchemaArray.prototype.applyGetters = function (value, scope) {
if (this.caster.options && this.caster.options.ref) {
// means the object id was populated
return value;
}
return SchemaType.prototype.applyGetters.call(this, value, scope);
};
/**
* Casts contents
*
* @param {Object} value
* @param {Document} doc document that triggers the casting
* @param {Boolean} init whether this is an initialization cast
* @api private
*/
SchemaArray.prototype.cast = function (value, doc, init) {
if (Array.isArray(value)) {
if (!(value instanceof MongooseArray)) {
value = new MongooseArray(value, this.path, doc);
}
if (this.caster) {
try {
for (var i = 0, l = value.length; i < l; i++) {
value[i] = this.caster.cast(value[i], doc, init);
}
} catch (e) {
// rethrow
throw new CastError(e.type, value, this.path);
}
}
return value;
} else {
return this.cast([value], doc, init);
}
};
/**
* Casts contents for queries.
*
* @param {String} $conditional
* @param {any} [value]
* @api private
*/
SchemaArray.prototype.castForQuery = function ($conditional, value) {
var handler
, val;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
if (!handler)
throw new Error("Can't use " + $conditional + " with Array.");
val = handler.call(this, value);
} else {
val = $conditional;
var proto = this.casterConstructor.prototype;
var method = proto.castForQuery || proto.cast;
var caster = this.caster;
if (Array.isArray(val)) {
val = val.map(function (v) {
if (method) v = method.call(caster, v);
return isMongooseObject(v)
? v.toObject()
: v;
});
} else if (method) {
val = method.call(caster, val);
}
}
return val && isMongooseObject(val)
? val.toObject()
: val;
};
/*!
* @ignore
*/
function castToNumber (val) {
return Types.Number.prototype.cast.call(this, val);
}
SchemaArray.prototype.$conditionalHandlers = {
'$all': function handle$all (val) {
if (!Array.isArray(val)) {
val = [val];
}
val = val.map(function (v) {
if (v && 'Object' === v.constructor.name) {
var o = {};
o[this.path] = v;
var query = new Query(o);
query.cast(this.casterConstructor);
return query._conditions[this.path];
}
return v;
}, this);
return this.castForQuery(val);
}
, '$elemMatch': function (val) {
if (val.$in) {
val.$in = this.castForQuery('$in', val.$in);
return val;
}
var query = new Query(val);
query.cast(this.casterConstructor);
return query._conditions;
}
, '$size': castToNumber
, '$ne': SchemaArray.prototype.castForQuery
, '$in': SchemaArray.prototype.castForQuery
, '$nin': SchemaArray.prototype.castForQuery
, '$regex': SchemaArray.prototype.castForQuery
, '$near': SchemaArray.prototype.castForQuery
, '$nearSphere': SchemaArray.prototype.castForQuery
, '$gt': SchemaArray.prototype.castForQuery
, '$gte': SchemaArray.prototype.castForQuery
, '$lt': SchemaArray.prototype.castForQuery
, '$lte': SchemaArray.prototype.castForQuery
, '$within': function(val) {
var query = new Query(val);
query.cast(this.casterConstructor)
return query._conditions;
}
, '$maxDistance': castToNumber
};
/*!
* Module exports.
*/
module.exports = SchemaArray;