forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
documentarray.js
138 lines (111 loc) · 3.06 KB
/
documentarray.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
/**
* Module dependencies.
*/
var SchemaType = require('../schematype')
, ArrayType = require('./array')
, MongooseDocumentArray = require('../types/documentarray')
, Subdocument = require('../types/embedded')
, CastError = SchemaType.CastError
, Document = require('../document');
/**
* SubdocsArray SchemaType constructor
*
* @param {String} key
* @param {Schema} schema
* @param {Object} options
* @api private
*/
function DocumentArray (key, schema, options) {
// compile an embedded document for this schema
// TODO Move this into parent model compilation for performance improvement?
function EmbeddedDocument () {
Subdocument.apply(this, arguments);
};
EmbeddedDocument.prototype.__proto__ = Subdocument.prototype;
EmbeddedDocument.prototype.schema = schema;
EmbeddedDocument.schema = schema;
// apply methods
for (var i in schema.methods) {
EmbeddedDocument.prototype[i] = schema.methods[i];
}
// apply statics
for (var i in schema.statics)
EmbeddedDocument[i] = schema.statics[i];
ArrayType.call(this, key, EmbeddedDocument, options);
this.caster = EmbeddedDocument;
this.caster.options = options;
var self = this;
this.schema = schema;
this.default(function(){
return new MongooseDocumentArray([], self.path, this);
});
};
/**
* Inherits from ArrayType.
*/
DocumentArray.prototype.__proto__ = ArrayType.prototype;
/**
* Performs local validations first, then validations on each embedded doc
*
* @api private
*/
DocumentArray.prototype.doValidate = function (array, fn, scope) {
var self = this;
SchemaType.prototype.doValidate.call(this, array, function(err){
if (err) return fn(err);
var count = array.length
, error = false;
if (!count) return fn();
array.forEach(function(doc, index){
doc.validate(function(err){
if (err && !error){
// rewrite they key
err.key = self.key + '.' + index + '.' + err.key;
fn(err);
error = true;
} else {
--count || fn();
}
});
});
}, scope);
};
/**
* Casts contents
*
* @param {Object} value
* @param {Document} document that triggers the casting
* @api private
*/
DocumentArray.prototype.cast = function (value, doc, init, prev) {
var subdoc
, i
if (Array.isArray(value)) {
if (!(value instanceof MongooseDocumentArray)) {
value = new MongooseDocumentArray(value, this.path, doc);
}
i = value.length;
while (i--) {
if (!(value[i] instanceof Subdocument)) {
if (init) {
subdoc = new this.caster(null, value);
// skip _id for pre-init hooks
delete subdoc._doc._id;
value[i] = subdoc.init(value[i]);
} else {
subdoc = prev && prev.id(value[i]._id) ||
new this.caster(null, value);
value[i] = subdoc.set(value[i]);
}
}
}
return value;
} else {
return this.cast([value], doc, init, prev);
}
throw new CastError('documentarray', value);
};
/**
* Module exports.
*/
module.exports = DocumentArray;