A simple mongoose plugin to provide private Schema paths
npm install mongoose-private-pathsmongoose-private-paths
======================







- - -
A simple mongoose plugin to provide private Schema keys
npm install mongoose-private-paths``javascript
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 "_"'
// }
`
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 "\_")
It should be noted that the toJSON method is overloaded with an optionaloptions 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)
This is a static function which holds the actual logic for the overloaded
.toJSON on documents. It's exported to support serializinglean 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.
This returns a Query` only selecting public fields.