update-props
v0.2.1
Published
Updates a whitelist of an object's properties and returns changes, if any
Downloads
4
Readme
update-props
Updates a whitelist of an object's properties and returns changes, if any
Install
npm i --save update-props
API
updateProps({Object} object, {Object} newProperties, {[String]} allowedKeys);
Usage
var update = require('update-props');
var user = {
name: 'John',
password: 'foo'
};
var changes = update(user, { name: 'Fred', password: 'bar' }, ['name']);
console.log(changes); // { before: { name: 'John' }, after: { name: 'Fred' } }
console.log(user); // { name: 'Fred', password: 'foo' }
Mongoose plugin usage
var mongoose = require('mongoose');
var updatePropsPlugin = require('update-props/mongoose-plugin');
var userSchema = new mongoose.Schema({ /*...*/ });
userSchema.plugin(updatePropsPlugin);
userSchema.methods.update = function(props) {
var allowedKeys = [];
if (this.hasPermission('update:name')) {
allowedKeys.push('firstName', 'lastName');
}
if (this.hasPermission('update:address')) {
allowedKeys.push('address1', 'address2', 'city');
}
return this.updateProps(props, allowedKeys);
};