mongoose-params
is designed to give you rails-like parameter helpers to secure your models.
$ npm install --save mongoose-params
var params = require('mongoose-params');
var UserSchema = new Schema({
name: String,
email: String,
ssn: String
});
UserSchema.plugin(params, {
permitted: 'name email',
overrideMethods: false
});
UserSchema.virtual('profile')
.get(function(){
return this.only('name', 'email');
});
var bob = new User({
name: 'Bob',
email: 'bob@example.com',
ssn: '123-45-6789'
});
Return only params
properties for the document
console.log(bob.profile); // {name: 'Bob', email: 'bob@example.com'}
Return all properties except params
for the document
console.log(bob.except('ssn')); // {name: 'Bob', email: 'bob@example.com'}
Filter and apply changes to the document
bob.assign({
name: 'Bob Perry',
ssn: '000-00-0000'
});
console.log(bob); // {name: 'Bob', email: 'bob@example.com', ssn: '123-45-6789'}
Same as #assign
but returns a copy of the document that the changes were made to
Update the document using only [permitted] properties. Use override
to set properties not in [permitted].
bob.safeUpdate({
email: 'bob@newEmail.com',
ssn: '000-00-0000'
},{
role: 'admin'
},function(err, updatedBob){
console.log(updatedBob); // {name: 'Bob', email: 'bob@newEmail.com', ssn: '123-45-6789', role: 'admin'}
});
See Lodash: _.pick.
See Lodash: _.omit.
Create document(s) using only [permitted] properties. Use override
to set properties not in [permitted].
User.create({
name: 'Bob',
email: 'bob@example.com',
ssn: '123-45-6789'
},{
role: 'admin'
},function(err, bob){
console.log(bob); // {name: 'Bob', email: 'bob@example.com', role: 'admin'}
});
String
or Array<String>
that contains the permitted parameters
These both give the same result:
ThingSchema.plugin(require('mongoose-params'),{
permitted: 'title description details'
});
ThingSchema.plugin(require('mongoose-params'),{
permitted: ['title', 'description', 'details']
});
This option will set the mongoose Model.create
and Document.update
methods
to #safeCreate and #safeUpdate, respectively.
The MIT License (MIT)
Copyright (c) 2015 Bradyn Poulsen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.