mongoose-patch-update
v1.0.6
Published
Mongoose plugin for model update in patch like style
Downloads
39
Maintainers
Readme
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 = require('mongoose-patch-update');
const userSchema = new Schema({...});
// attach mongoose-patch-update plugin to userSchema
userSchema.plugin(mongoosePatchUpdate);
To use this plugin globally for all schemas:
const mongoose = require('mongoose');
const mongoosePatchUpdate = require('mongoose-patch-update');
mongoose.plugin(mongoosePatchUpdate);
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 = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;
const User = require('./user.model.js'); // model
let query = { _id: ObjectId(req.params.userId) }; // find query
let updateParams = { username: 'johndoe-123', email: '[email protected]' }; // update parameters - only `username` will be updated, `email` is protected
let protectedKeys = ['email']; // protected keys - given keys won't be allowed to be updated
let 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: '[email protected]'
};
*/
User
.patchUpdate(
query,
updateParams,
protectedKeys,
selectedKeys
)
.then((updatedUser) => {
// user after update
/*
let user = {
username: 'johndoe-123',
firstname: 'John',
surname: 'Doe',
email: '[email protected]'
};
*/
res.status(200).json(updatedUser);
})
.catch((err) => {
next(err);
});
Callback example
const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;
const User = require('./user.model.js'); // model
let query = { _id: ObjectId(req.params.userId) }; // find query
let updateParams = { username: 'johndoe-123', email: '[email protected]' }; // update parameters - only `username` will be updated, `email` is protected
let protectedKeys = ['email']; // protected keys - given keys won't be allowed to be updated
let 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: '[email protected]'
};
*/
User
.patchUpdate(
query,
updateParams,
protectedKeys,
selectedKeys,
(err, updatedUser) => {
if (err) return next(err);
// user after update
/*
let user = {
username: 'johndoe-123',
firstname: 'John',
surname: 'Doe',
email: '[email protected]'
};
*/
res.status(200).json(updatedUser);
});
License
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004
Copyright (C) 2017 Jozef Butko [email protected]
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.