2.0.4 • Published 6 years ago

@teamsight/mongoose-clean v2.0.4

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

A plugin for Mongoose to normalize JSON output, as well as Object output. Based on mongoose-to-json

Usage

Setup as a global plugin for all Mongoose schema's:

const mongoose = require('mongoose');
const clean = require('@ifit/mongoose-clean');

mongoose.plugin(clean);

Or for a specific (sub) schema:

const mongoose = require('mongoose');
const clean = require('@ifit/mongoose-clean');
const {Schema} = mongoose;

const MySchema = new Schema({});
MySchema.plugin(clean);

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 toJson = require('@ifit/mongoose-clean');
const {Schema} = mongoose;

const schema = new Schema({
  email: {type: String},
  password: {type: String, private: true},
});

schema.plugin(toJson);

const User = mongoose.model('users', schema);
const user = new User({email: 'test@test.com', password: 'test'});

console.log(user.toJSON());

This will output:

{
  "id": "400e8324a71d4410b9dc3980b5f8cdea",
  "email": "test@test.com"
}
2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.2

6 years ago