update-props

0.2.1 • Public • Published

Build Status

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);
};

Readme

Keywords

Package Sidebar

Install

npm i update-props

Weekly Downloads

4

Version

0.2.1

License

MIT

Unpacked Size

6.73 kB

Total Files

7

Last publish

Collaborators

  • powmedia