npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-to-json-project

v2.5.0

Published

Adds projection to toJSON with predefined levels and level selector capabilitiy

Downloads

10

Readme

mongoose toJSON project

toJSON projection plugin for mongoose.

Adds advanced field selection capabilities to toJSON with optional predefined levels and level selector capabilities.

Installation

$ npm install mongoose-to-json-project

Basic Usage

Register this plugin last as it will bootstrap any previous transform function option.

schema.plugin(require('mongoose-to-json-project'), {
  // your predefined levels:
  levels: {
    // public level
    public: 'some.deep.field.to.include -some.deep.field.to.exclude',
    // private level
    private: '-password',
    // system level
    system: ''
  },
  // (optional) default field schema level as a String
  defaultFieldLevel: 'system',
  // (optional) default toJSON() level as a String
  level: 'public',
  //  or a synchronous level selector function (preferred method).
  // All transform options are passed on to level selector functions,
  // e.g. we can get the user that requested the transform from `options.user`
  // if user is passed into `toJSON` options, like `toJSON({ user: req.user })`.
  level: (doc, ret, options) => doc._id.equals(options.user._id) ? 'private' : 'public'
});

Schema Tree Level Definitions

It is possible to define level inclusions and exclusions in the schema declaration.Mixing inclusions and exclusions are prohibited and throws an error.Inclusion levels must be predefined in plugin level options.

const schema = new Schema({
    myInvisibleField: {
      type: String,
      level: '-public -private' // visible on levels other than 'public' and 'private'
    },
    myInternalField: {
      type: String,
      level: 'internal' // only visible on 'internal' level, 'internal' must be predefined.
    }
});

schema.plugin(require('mongoose-to-json-project'), {
  levels: {
      'internal': '' // predefintion
  }
});

Plugin Options

schema.plugin(require('mongoose-to-json-project'), options);

levels: Object

Predefined levels to use with a level selector. Key is level name. Value is a Mongoose style dot-notation, space delimited projection. Mixing inclusions and exclusions is possible. Defining at least one inclusion causes all other fields to be excluded automatically like MongoDB. Child inclusions precedes Parent exclusions, e.g. '-parent parent.child1' results in all properties but parent.child1 to be excluded.

levels: {
    public: 'username rank status',
    private: '-password -secretField -secret.deep.field'
}

level: String

Basic static level selector.

level: 'public'

level: Function(doc, ret, options)

Synchronous function that must return level name to use as a string. Preferred level selector method!

  • doc - Mongoose Document
  • ret - Document as plain Object
  • options - Transform options (Same as obj - if specified)
level: (doc, ret, options) => doc._id.equals(options.user._id) ? 'private' : 'public'

Mongoose API Extensions and Modifications

Document#toJSON(obj)

Added options:

obj.projection: String - Mongoose style dot-notation, space delimited projection argument. Both inclusions and exclusions are possible but inclusions takes precedence thus excluding all other fields.

obj.level: String - Set level to use. (!)

obj.level: Function(doc, ret, options) - Synchronous function that must return level name to use as a string. (!)

  • doc - Mongoose Document
  • ret - Document as plain Object
  • options - Transform options (Same as obj - if specified)

(!) CAUTION:
The level option is passed to the toJSON method call of populated subdocuments and it may not be compatible. Therefore use with caution and if possible, avoid level option completely and depend on schema defaults instead. A Function is the preferred level selector method in plugin options.

Document#set(path, val, [type], [options])

Added options: same as toJSON
See: mongoose docs Document#set

Model#toJSONOptionsExtend(obj)

This method extends obj with the default schema toJSON options (Adds defaults to prototype chain).In mongoose the options passed to toJSON do not inherit the defaults, this method solves this.

Example:

let options = Model.toJSONOptionsExtend({
    user: req.user,
    projection: '-some.field.to.exclude some.field.to.include'
});
let plainObject = document.toJSON(options);

Model#getLevelSchemaTree(levelName)

Returns a clone of the schema tree (plain object) with level projection applied.

Compatibility

Only intended for Mongoose v4 with NodeJS later than v4. Tested with Mongoose 4.2.8.

License

LGPL-3

Forked from mongoose-toObject-project by Faleij [email protected]