Skip to content

Instantly share code, notes, and snippets.

@jwietelmann
Created November 30, 2012 00:40
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 jwietelmann/4172946 to your computer and use it in GitHub Desktop.
Save jwietelmann/4172946 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
module.exports = exports = function(schema, options) {
schema.method('massAssign', function(fields) {
for(var i in schema.tree) {
if(schema.tree[i].protect || fields[i] == null) continue;
this[i] = fields[i];
}
});
schema.static('massAssign', function(fields) {
var model = new this();
model.massAssign(fields);
return model;
});
};
mongoose.schema.plugin(exports);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment