JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 733
  • Score
    100M100P100Q107230F
  • License MIT

A plugin for Mongoose to normalize JSON output

Package Exports

  • @meanie/mongoose-to-json

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@meanie/mongoose-to-json) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@meanie/mongoose-to-json

npm version node dependencies github issues codacy

A simple plugin for Mongoose to normalize JSON output, for use with Meanie Express Seed projects

Meanie

Installation

You can install this package using yarn or npm.

#yarn
yarn add @meanie/mongoose-to-json

#npm
npm install @meanie/mongoose-to-json --save

Usage

As a global plugin for all Mongoose schema's:

const mongoose = require('mongoose');
const toJson = require('@meanie/mongoose-to-json');

mongoose.plugin(toJson);

For a specific (sub) schema:

const mongoose = require('mongoose');
const toJson = require('@meanie/mongoose-to-json');
const {Schema} = mongoose;

const MySchema = new Schema({});
MySchema.plugin(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 toJson = require('@meanie/mongoose-to-json');
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"
}

Issues & feature requests

Please report any bugs, issues, suggestions and feature requests in the @meanie/mongoose-to-json issue tracker.

Contributing

Pull requests are welcome! If you would like to contribute to Meanie, please check out the Meanie contributing guidelines.

Credits

License

(MIT License)

Copyright 2016-2017, Adam Reis