Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jbutko/mongoose-patch-update
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.
https://github.com/jbutko/mongoose-patch-update
mongodb mongoose mongoosejs npm npm-package
Last synced: 28 days ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/jbutko/mongoose-patch-update
- Owner: jbutko
- License: wtfpl
- Created: 2017-04-02T13:35:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-08-08T19:16:15.000Z (over 7 years ago)
- Last Synced: 2024-09-28T20:43:22.810Z (about 1 month ago)
- Topics: mongodb, mongoose, mongoosejs, npm, npm-package
- Language: JavaScript
- Size: 12.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Mongoose Patch Update Plugin
=========[![NPM](https://nodei.co/npm/mongoose-patch-update.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/mongoose-patch-update/)
*mongoose-patch-update* plugin is simple [mongoose](http://www.mongoose.com) 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:
```javascript
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:
```javascript
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
```javascript
const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;
const User = require('./user.model.js'); // modellet 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
```javascript
const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;
const User = require('./user.model.js'); // modellet 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 2004Copyright (C) 2017 Jozef Butko
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 MODIFICATION0. You just DO WHAT THE FUCK YOU WANT TO.