Last active
August 29, 2015 14:01
-
-
Save frozeman/88a3e47679dd74242cab to your computer and use it in GitHub Desktop.
Meteor minimongo: Insert and Remove in bulk using an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
These two functions provide a simple extra minimongo functionality to add and remove documents in bulk, | |
without unnecessary re-renders. | |
The process is simple. It will add the documents to the collections "_map" and only the last item will be inserted properly | |
to cause reactive dependencies to re-run. | |
*/ | |
Models = {}; | |
/* | |
Inserts documents in bulk. | |
@method insertBulk | |
@param {Object} collection the collection to insert | |
@param {Array} documents an array with documents | |
@return {Array} with the ids | |
*/ | |
Models.insertBulk = function(collection, documents){ | |
if(collection) { | |
return _.compact(_.map(documents, function(item){ | |
if(_.isObject(item)) { | |
var _id = collection._makeNewID(); | |
// insert with reactivity only on the last item | |
if(_.last(documents) === item) | |
_id = collection.insert(item); | |
// insert without reactivity | |
else { | |
item._id = _id; | |
collection._collection._docs._map[_id] = item; | |
} | |
return _id; | |
} | |
})); | |
} | |
}, | |
/* | |
Removes documents in bulk. | |
@method removeBulk | |
@param {Object} collection the collection to remove from | |
@param {Object} query the query to find items | |
@return {Undefined} | |
*/ | |
Models.removeBulk = function(collection, query){ | |
var _this = this; | |
if(collection { | |
var documents = collection.find(query).fetch(); | |
// clean items | |
_.each(documents, function(item){ | |
// insert with reactivity only on the last item | |
if(_.last(documents) === item) | |
collection.remove(item._id); | |
// insert without reactivity | |
else | |
delete _collection_collection._docs._map[item._id]; | |
}); | |
} | |
}, |
Hello Frozeman - I tried this but it didn't work. Not unexcpected only the last insert is truly saved to the database.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Frozeman,
I couldn't find a lot of other people's experience on your interesting approach. How sure are you that the collections which you bulk insert on the client are then properly saved on the server without corruption of _ids for example?