GithubHelp home page GithubHelp logo

abranhe / normalize-mongoose Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 0.0 31 KB

Normalize Mongoose JSON output

Home Page: https://p.abranhe.com/normalize-mongoose

License: MIT License

JavaScript 100.00%
mongoose mongoose-plugin mongodb js

normalize-mongoose's Introduction

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: '[email protected]',
  password: 'my_awesome_password',
});

The above code will output:

{
  "id": "5dff03d3218b91425b9d6fab",
  "name": "Abraham",
  "age": 33,
  "email": "[email protected]"
}

License

MIT © Abraham Hernandez

normalize-mongoose's People

Contributors

abranhe avatar dependabot[bot] avatar yubintw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

yubintw

normalize-mongoose's Issues

transpile exported code

would be easier to consume this package if you transpile it with babel before shipping.

right now I copy pasted it locally because jest is throwing syntax errors

Does not remove all `_ids`

Assume adding this (noted in bold) to your example:

const personSchema = mongoose.Schema({
  name: String,
  age: Number,
  email: String,
  password: { type: String, private: true },
  tracklist: {
    type: [
      {
        track: String,
          title: String,
      }
    ],
    validate: v => Array.isArray(v) && v.length > 0,
  }
});

personSchema.plugin(normalize);

const Person = mongoose.model('Person', personSchema);

const someone = new Person({
  name: 'Abraham',
  age: 33,
  email: '[email protected]',
  password: 'my_awesome_password',
  tracklist: [
    {
      track: "1",
      title: "1"
    },
    {
      track:"2",
      title:"2"
    }]
});

Then the output will still contain _ids:

{
  "name": "Abraham",
  "age": 33,
  "email": "[email protected]",
  "tracklist": [
    {
      "track": "1",
      "title": "1",
      "_id": "6361521b2b1edf8bd136a3f0"
    },
    {
      "track": "2",
      "title": "2",
      "_id": "6361521b2b1edf8bd136a3f1"
    }
  ],
  "id": "6361521b2b1edf8bd136a3ef"
}

Typescript definitions

Any chance of getting typescript definitions so we can use the import without errors. If they exist I can't locate them.
Thanks

Not work with CommonJS module system

const normalize = require("normalize-mongoose");
^

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Code\FullStack Todo\todo-backend\node_modules\normalize-mongoose\index.js from D:\Code\FullStack Todo\todo-backend\modal.js not supported.
Instead change the require of index.js in D:\Code\FullStack Todo\todo-backend\modal.js to a dynamic import() which is available
in all CommonJS modules.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.