A plugin for Mongoose to normalize JSON output
You can install this package using yarn
or npm
.
#yarn
yarn add @meanie/mongoose-update-denormalized
#npm
npm install @meanie/mongoose-update-denormalized --save
Setup as a global plugin for all Mongoose schema’s:
const mongoose = require('mongoose');
const updateDenormalized = require('@meanie/mongoose-update-denormalized');
mongoose.plugin(updateDenormalized);
Or for a specific (sub) schema:
const mongoose = require('mongoose');
const updateDenormalized = require('@meanie/mongoose-update-denormalized');
const {Schema} = mongoose;
const MySchema = new Schema({});
MySchema.plugin(updateDenormalized);
This plugin exposes a static method to update denormalized data:
const User = mongoose.model('User');
const {_id, name, region} = location;
await User.updateDenormalized('location', _id, {name, region});
This would update the name
and region
properties on the denormalized location
property on users, for the given matching location ID.
Please report any bugs, issues, suggestions and feature requests in the @meanie/mongoose-update-denormalized issue tracker.
Pull requests are welcome! If you would like to contribute to Meanie, please check out the Meanie contributing guidelines.
This package has been kindly sponsored by Hello Club, an all in one club and membership management solution complete with booking system, automated membership renewals, online payments and integrated access and light control. Check us out if you happen to belong to any kind of club or if you know someone who helps run a club!
Author: meanie
Source Code: https://github.com/meanie/mongoose-update-denormalized
#mongoose #mongodb