mongoose-params
v0.0.3
Published
implement rails-like strong parameters in mongoose
Downloads
290
Maintainers
Readme
mongoose-params
mongoose-params
is designed to give you rails-like parameter helpers to secure your models.
Installation
$ npm install --save mongoose-params
Synopsis
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: '[email protected]',
ssn: '123-45-6789'
});
Methods
#only(params)
Return only params
properties for the document
console.log(bob.profile); // {name: 'Bob', email: '[email protected]'}
#except(params)
Return all properties except params
for the document
console.log(bob.except('ssn')); // {name: 'Bob', email: '[email protected]'}
#assign(data, [override])
Filter and apply changes to the document
bob.assign({
name: 'Bob Perry',
ssn: '000-00-0000'
});
console.log(bob); // {name: 'Bob', email: '[email protected]', ssn: '123-45-6789'}
#merge(data, [override])
Same as #assign
but returns a copy of the document that the changes were made to
#safeUpdate(data, [override], [done])
Update the document using only [permitted] properties. Use override
to set properties not in [permitted].
bob.safeUpdate({
email: '[email protected]',
ssn: '000-00-0000'
},{
role: 'admin'
},function(err, updatedBob){
console.log(updatedBob); // {name: 'Bob', email: '[email protected]', ssn: '123-45-6789', role: 'admin'}
});
Static Methods
#only(object, [callback], [thisArg])
See Lodash: _.pick.
#except(object, [callback], [thisArg])
See Lodash: _.omit.
#safeCreate(data, [override], [done])
Create document(s) using only [permitted] properties. Use override
to set properties not in [permitted].
User.create({
name: 'Bob',
email: '[email protected]',
ssn: '123-45-6789'
},{
role: 'admin'
},function(err, bob){
console.log(bob); // {name: 'Bob', email: '[email protected]', role: 'admin'}
});
Configuration
permitted: {String|Array<String>}
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']
});
overrideMethods: {Boolean}
This option will set the mongoose Model.create
and Document.update
methods
to #safeCreate and #safeUpdate, respectively.
License
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.