hera-mongoose-to-json
v1.0.8
Published
A plugin for Mongoose to normalize JSON output
Readme
hera-mongoose-to-json
A plugin for Mongoose to normalize JSON output
Installation
You can install this package using npm.
#npm
npm install hera-mongoose-to-json --saveUsage
Setup as a global plugin for all Mongoose schema's:
const mongoose = require('mongoose')
const hmtj = require('hera-mongoose-to-json')
//Global plugin
mongoose.plugin(hmtj.toJSON)Or for a specific (sub) schema:
const mongoose = require('mongoose')
const hmtj = require('hera-mongoose-to-json')
const MySchema = new Schema(/* ... */})
//Apply plugin
MySchema.plugin(hmtj.toJSON)This plugin will normalize JSON output for client side applications from:
{
"_id": "400e8324a71d4410b9dc3980b5f8cdea",
"__v": 2,
"name": "Item A"
}To a cleaner:
{
"id": "400e8324a71d4410b9dc3980b5f8cdea",
"name": "Item A"
}You can also remove private paths from the JSON:
const mongoose = require('mongoose')
const hmtj = require('hera-mongoose-to-json')
const schema = new Schema({
email: {type: String},
password: {type: String, private: true},
})
schema.plugin(hmtj.toJSON)
const User = mongoose.model('users', schema)
const user = new User({email: '[email protected]', password: 'test'})
console.log(user.toJSON())This will output:
{
"id": "400e8324a71d4410b9dc3980b5f8cdea",
"email": "[email protected]"
}License
(MIT License)
Copyright 2023, Burak Simsek
