Created
July 27, 2012 17:30
-
-
Save timoxley/3189267 to your computer and use it in GitHub Desktop.
mongoose drop collections helper
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
var async = require('async') | |
var _ = require('underscore') | |
var Helpers = function(mongoose) { | |
this.mongoose = mongoose || require('mongoose') | |
this.dropCollections = function(callback) { | |
var collections = _.keys(mongoose.connection.collections) | |
async.forEach(collections, function(collectionName, done) { | |
var collection = mongoose.connection.collections[collectionName] | |
collection.drop(function(err) { | |
if (err && err.message != 'ns not found') done(err) | |
done(null) | |
}) | |
}, callback) | |
} | |
} | |
module.exports = Helpers |
good one, but calls done twice. You might want to do something like this:
if(err && err.message.. ) err = null;
done(err);
I think it would be better:
if (err && err.message != 'ns not found') {
done(err);
} else {
done(null);
}
Super helpful, thank you
It uses native driver - which is not good for consistence. So mongoose will not know that that collections already deleted.
mongoose in closer is assigned incorrectly. Line 5 shoould be:
this.mongoose = mongoose = mongoose || require('mongoose');
or even just like this:
mongoose = mongoose || require('mongoose');
Thanks! :)
Wouldn't it be easier to use async.forEachOf to iterate all keys in the object instead of using underscore?
async.forEachOf(mongoose.connection.collections, function(collection, collectionName, done) {
// drop-collection-code
});
That way, you wouldn't need the underscore dependency at all
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!