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-private-paths

v0.7.0

Published

A simple mongoose plugin to provide private Schema paths

Downloads

5,882

Readme

mongoose-private-paths

Build Status Code Climate Coverage Status Analytics Dependency Status devDependency Status peerDependency Status


A simple mongoose plugin to provide private Schema keys

Getting Started

Install the module with: npm install mongoose-private-paths

var mongoose = require('mongoose'),
    privatePaths = require('mongoose-private-paths');

var TestSchema = new mongoose.Schema({
  public:       { type: String },
  _private:     { type: String },
  also_private: { type: String, private: true },
  not_private:  { type: String, private: false },
  _even:        { type: String, private: false },
  works_too:   [{ type: String, private: false }]
});

TestSchema.plugin(privatePaths);

var Test = mongoose.model('Test', Testschema);

var test = new Test({
  public:       'all keys are public by default!',
  _private:     'keys prefixed with an "_" will be private - except "_id"',
  also_private: 'stuff with a "private" field set to true will be... private!',
  not_private:  'stuff with a "private" field set to false will be... NOT private!',
  _even:        'if they are prefixed with an "_"',
  works_too:    'array-like fields should work too'
});

test.toJSON(); // =>
// {
//   _id:          '1234',
//   public:       'all keys are public by default!',
//   not_private: 'stuff with a "private" field set to false will be... NOT private!',
//   _even:        'if they are prefixed with an "_"'
// }

Documentation

As of right now there're only two options available, to be passed when calling Schema.plugin(privatePaths, options):

  • ignore => an Array of keys to ignore (they'll all be public)
  • prefix => a different private key prefix (to be used instead of "_")

document.toJSON([options])

It should be noted that the toJSON method is overloaded with an optional options parameter, with the following keys:

  • keep => Either an array of keys or a single key to ignore (not omit)
  • remove => Either an array of keys or a single key to remove (the default behaviour is overridden, to only remove the specified keys)

Model.omitPrivatePaths(doc, [options])

This is a static function which holds the actual logic for the overloaded .toJSON on documents. It's exported to support serializing lean mongoose objects, avoiding the mongoose overhead for lists and things like that. It takes a document or lean object as its first argument and the options node as its second.

Model.findPrivate(query)

This returns a Query only selecting public fields.

License

Copyright (c) 2014. Licensed under the MIT license.

Donations

Would you like to buy me a beer? Send bitcoin to 3JjxJydvoJjTrhLL86LGMc8cNB16pTAF3y