forked from mikowals/batch-insert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch-insert-common.js
163 lines (140 loc) · 4.54 KB
/
batch-insert-common.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
Mongo.Collection.prototype._defineBatchInsert = function(){
var self = this;
// don't define a method for a null collection
if ( ! self._name || ! self._connection ) return;
var m = {};
m['/' + self._name + '/batchInsert'] = function( docs ){
check( docs, [Object]);
// 'this' refers to method context
if ( this.isSimulation){
return docs.map( function( doc ){
if (! doc._id)
doc._id = self._makeNewID();
return self.insert( doc );
});
}
//client returned so server code below
var userId = this.userId;
var generatedIds = docs.map( function( doc ){
if( ! _.has( doc, '_id') ){
return self._makeNewID();
} else
return doc._id;
});
docs.forEach( function( doc, ii ){
if ( this.connection ) {
//server method called by client so check allow / deny rules.
if (!( (doc && _type(doc) ) &&
!EJSON._isCustomType(doc))) {
throw new Error("Invalid modifier. Modifier must be an object.");
}
// call user validators.
// Any deny returns true means denied.
if (_.any(self._validators.insert.deny, function(validator) {
return validator(userId, docToValidate(validator, doc, generatedIds[ii]));
})) {
throw new Meteor.Error(403, "Access denied");
}
// Any allow returns true means proceed. Throw error if they all fail.
if (_.all(self._validators.insert.allow, function(validator) {
return !validator(userId, docToValidate(validator, doc, generatedIds[ii]));
})) {
throw new Meteor.Error(403, "Access denied");
}
}
doc._id = generatedIds[ii];
}, this ); // pass context of method into forEach
return _batchInsert(self, docs);
//end of method definition
};
self._connection.methods( m );
};
Mongo.Collection.prototype.batchInsert = function( /*args*/ ){
var self = this;
var args = _.toArray(arguments);
var cb;
if (typeof args[ args.length - 1] === 'function'){
cb = args.pop();
}
if ( ! self._name || ! self._connection) {
var res, err;
try {
res = args[0].map( function( doc ){
return self._collection.insert( doc );
});
} catch (e){
if ( ! cb )
throw e;
err = e;
};
cb && cb( err, res );
return res;
} else if (self._connection && self._connection === Meteor.server) {
docs = args[0].map( function (doc){
if (! doc._id) doc._id = self._makeNewID();
return doc;
})
return _batchInsert( self, docs, cb);
}
if ( cb )
return self._connection.apply( '/'+ self._name + '/batchInsert', args, {returnStubValue: true}, cb );
return self._connection.apply( '/'+ self._name + '/batchInsert', args, {returnStubValue: true});
};
var original = Mongo.Collection;
//_.extend ( original, Mongo.Collection );
Mongo.Collection = function( name, options ){
original.call( this, name, options );
this._defineBatchInsert();
};
Mongo.Collection.prototype = Object.create( original.prototype );
Mongo.Collection.prototype.constructor = Mongo.Collection;
_.extend( Mongo.Collection, original);
Meteor.Collection = Mongo.Collection;
//function copied from MDG Mongo.Collection._validateInsert. Needed in allow / deny checks.
function docToValidate(validator, doc, generatedId) {
var ret = doc;
if (validator.transform) {
ret = EJSON.clone(doc);
// If you set a server-side transform on your collection, then you don't get
// to tell the difference between "client specified the ID" and "server
// generated the ID", because transforms expect to get _id. If you want to
// do that check, you can do it with a specific
// `C.allow({insert: f, transform: null})` validator.
if (generatedId !== null) {
ret._id = generatedId;
}
ret = validator.transform(ret);
}
return ret;
};
function _type (v) {
if (typeof v === "number")
return 1;
if (typeof v === "string")
return 2;
if (typeof v === "boolean")
return 8;
if ( _.isArray(v) )
return 4;
if (v === null)
return 10;
if (v instanceof RegExp)
// note that typeof(/x/) === "object"
return 11;
if (typeof v === "function")
return 13;
if (v instanceof Date)
return 9;
if (EJSON.isBinary(v))
return 5;
if (v instanceof LocalCollection._ObjectID)
return 7;
return 3; // object
// XXX support some/all of these:
// 14, symbol
// 15, javascript code with scope
// 16, 18: 32-bit/64-bit integer
// 17, timestamp
// 255, minkey
// 127, maxkey
};