normalize-mongoose
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

normalize-mongoose

GH Status NPM License

Normalize Mongoose JSON output

This plugin removes the following fields _id, __v, and published virtuals id when the document is converted to JSON. Also it allows you to hide private fields like password.

Install

$ npm install normalize-mongoose

Using Github NPM Registry

$ npm install abranhe@normalize-mongoose

Usage

import mongoose from 'mongoose';
import normalize from 'normalize-mongoose';

const personSchema = mongoose.Schema({
    name: String,
    age: Number,
});

personSchema.plugin(normalize);

See how normalize-mongoose will clean the the JSON output:

From:
{
  "_id": "5dff03d3218b91425b9d6fab",
  "name": "Abraham",
  "__v": 0
}
To:
{
  "id": "5dff03d3218b91425b9d6fab",
  "name": "Abraham"
}

normalize-mongoose comes really handy on real word applications, allowing you to hide from the output any private field previously defined.

import mongoose from 'mongoose';
import normalize from 'normalize-mongoose';

const personSchema = mongoose.Schema({
    name: String,
    age: Number,
    email: String,
    password: { type: String, private: true },
});

personSchema.plugin(normalize);

const Person = mongoose.model('Person', personSchema);
const someone = new Person( {
  name: 'Abraham',
  age: 33,
  email: 'abranhe@example.com',
  password: 'my_awesome_password',
});

The above code will output:

{
  "id": "5dff03d3218b91425b9d6fab",
  "name": "Abraham",
  "age": 33,
  "email": "abranhe@example.com"
}

License

MIT © Abraham Hernandez

Package Sidebar

Install

npm i normalize-mongoose

Weekly Downloads

870

Version

1.0.0

License

MIT

Unpacked Size

4.8 kB

Total Files

5

Last publish

Collaborators

  • abranhe