mongoose-model-update
v1.5.0
Published
Mongoose plugin providing a Model method for simple, filtered patch-style updates
Downloads
13
Maintainers
Readme
Mongoose Model Update
Mongoose plugin providing a Model method for simple, filtered patch-style updates
Setup
Install
npm install --save mongoose-model-update
Require this alongside your router-
import update from 'mongoose-model-update';
// Or, for ES5-
// var update = require('mongoose-model-update');
Usage
When registering the Mongoose plugin, you can optionally specify allowed fields
at the Model-level to be used as default params in Model.methods.update()
.
Model.plugin(update, ['name', 'bday', 'city']);
You can specify permitted fields to update in the 2nd argument. If no fields are provided and default params aren't set at the Model-level, then all fields will be available to update.
user.update(source, ['age'])
Use cases
At AutoLotto, we use this for PATCH API updates (RESTfully speaking). Ex-
export async function update(req, res) {
// ...
req.user.update(req.body);
// ...
}