mongoose-model-update

1.5.0 • Public • Published

Mongoose Model Update

NPM

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

Package Sidebar

Install

npm i mongoose-model-update

Weekly Downloads

9

Version

1.5.0

License

MIT

Last publish

Collaborators

  • tejasmanohar