Skip to content

Instantly share code, notes, and snippets.

@alexpchin
Forked from fnakstad/gist:5383750
Created February 19, 2016 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpchin/cbb87a3bf56c4b7754fc to your computer and use it in GitHub Desktop.
Save alexpchin/cbb87a3bf56c4b7754fc 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