Created
September 24, 2011 05:47
-
-
Save tblobaum/1239021 to your computer and use it in GitHub Desktop.
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 MongooseFilter = function (schema, options) { | |
options = options || {}; | |
schema.pre('save', function (next) { | |
this.filter(); | |
next(); | |
}); | |
schema.method('filter', function () { | |
var self = this; | |
var paths = schema.paths; | |
if (!'_id' in paths) paths['_id'] = ''; | |
for (var key in this._doc) { | |
if (!(key in paths)) { | |
delete this._doc[key]; | |
if (options.debug) console.log('Warn: Cannot put ' + key + ' in ' + this.constructor.modelName); | |
} | |
} | |
return this; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment