Skip to content

Instantly share code, notes, and snippets.

@fnakstad
Created April 14, 2013 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fnakstad/5383750 to your computer and use it in GitHub Desktop.
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.
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