Mongoose Patch Update Plugin
mongoose-patch-update plugin is simple mongoose plugin that enables model updates in PATCH like style - only what you send in body params will be updated. You can set protected keys which won't be updatable.
Installation
npm install mongoose-patch-update --save
You can use this plugin with specific schema or globally for all schemas.
To use it with specific schema:
const mongoosePatchUpdate = ; const userSchema = ...; // attach mongoose-patch-update plugin to userSchemauserSchema;
To use this plugin globally for all schemas:
const mongoose = ;const mongoosePatchUpdate = ; mongoose;
After attaching mongoose-patch-update plugin you will have Model.patchUpdate(...)
method on your model.
Usage
Mongoose patch update plugin provides support for both promise style queries as well as callback style queries.
Promise example
const mongoose = ;const ObjectId = mongooseTypesObjectId;const User = ; // model let query = _id: ; // find querylet updateParams = username: 'johndoe-123' email: 'john123@doe.com' ; // update parameters - only `username` will be updated, `email` is protectedlet protectedKeys = 'email'; // protected keys - given keys won't be allowed to be updatedlet selectedKeys = ''; // which keys will be returned after successful update - all keys will be returned // user before update/*let user = { username: 'johndoe', firstname: 'John', surname: 'Doe', email: 'john@doe.com'}; */ User ;
Callback example
const mongoose = ;const ObjectId = mongooseTypesObjectId;const User = ; // model let query = _id: ; // find querylet updateParams = username: 'johndoe-123' email: 'john123@doe.com' ; // update parameters - only `username` will be updated, `email` is protectedlet protectedKeys = 'email'; // protected keys - given keys won't be allowed to be updatedlet selectedKeys = ''; // which keys will be returned after successful update - all keys will be returned // user before update/*let user = { username: 'johndoe', firstname: 'John', surname: 'Doe', email: 'john@doe.com'}; */ User ;
License
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004
Copyright (C) 2017 Jozef Butko jbutko@gmail.com
Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- You just DO WHAT THE FUCK YOU WANT TO.