Created
April 14, 2013 18:43
-
-
Save fnakstad/5383750 to your computer and use it in GitHub Desktop.
Attaches a function called "seed" to Mongoose models, which basically wraps the create function, but returns a promise. This way seeding can be done through a promise chain.
This file contains hidden or 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 mongoose = require('mongoose'); | |
mongoose.Model.seed = function(entities) { | |
var promise = new mongoose.Promise; | |
this.create(entities, function(err) { | |
if(err) { promise.reject(err); } | |
else { promise.resolve(); } | |
}); | |
return promise; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment