GithubHelp home page GithubHelp logo

mongoose-filter-denormalize's Introduction

Mongoose-Filter-Denormalize

Simple filtering and denormalization for Mongoose. Useful for REST APIs where you might not want to send entire objects down the pipe. Allows you to store sensitive data directly on objects without worrying about it being sent to end users.

Installation

npm install mongoose-filter-denormalize

Compatibility

mongoose <= v3.4

Filter Usage

Filtering functionality is provided via a schema plugin.

Schema

var filter = require('mongoose-filter-denormalize').filter;
var ObjectId = mongoose.Schema.ObjectId;
var UserSchema = new Mongoose.schema({
  name            :   String,
  address         :   String,
  fb              :  {
      id              :   Number,
      accessToken     :   String
  },
  writeOnlyField  :   String,
  readOnlyField   :   String
});
UserSchema.plugin(filter, {
  readFilter: {
      "owner" : ['name', 'address', 'fb.id', 'fb.name', 'readOnlyField'],
      "public": ['name', 'fb.name']
  },
  writeFilter: {
      "owner" : ['name', 'address', 'fb.id', 'writeOnlyField']
  },
  // 'nofilter' is a built-in filter that does no processing, be careful with this
  defaultFilterRole: 'nofilter',
  sanitize: true, // Escape HTML in strings
  compat: true // Enable compatibility for Mongoose versions prior to 3.6 (default false)
});

Example Read

User.findOne({name: 'Foo Bar'}, User.getReadFilterKeys('public')), function(err, user){
  if(err) return next(err);
  res.send({success: true, users: [user]});
});

Example Write

User.findById(req.params.id, function(err, user){
  if(err) next(err);
  if(user.id !== req.user.id) next(403);
  user.extendWithWriteFilter(inputRecord, 'owner');  // Similar to jQuery.extend()
  user.save(function(err, user){
      if(err) return next(err);
      user.applyReadFilter('owner'); // Make sure the doc you return does not contain forbidden fields
      res.send({success: true, users: [user]});
  });
});

Options

  • readFilter (Object): Object mapping filtering profiles to string arrays of allowed fields. Used when reading a doc - useful for GET queries that must return only selected fields.
  • writeFilter (Object): As above, but used when when applied during a PUT or POST. This filters fields out of a given object so they will not be written even when specified. Useful for protected attributes like fb.accessToken.
  • defaultFilterRole (String)(default: 'nofilter'): Profile to use when one is not given, or the given profile does not exist.
  • sanitize (Boolean)(default: false): True to automatically escape HTML in strings.
  • compat (Boolean)(default: false): True to enable compatibility with Mongoose versions prior to 3.6

Statics

This plugin adds the following statics to your schema:

  • getReadFilterKeys(filterRole)
  • getWriteFilterKeys(filterRole)
  • applyReadFilter(input, filterRole)
  • applyWriteFilter(input, filterRole
  • _applyFilter(input, filterKeys) // private helper
  • _getFilterKeys(type, filterRole) // private helper

Methods

This plugin adds the following methods to your schema:

  • extendWithWriteFilter(input, filterRole)
  • applyReadFilter(filterRole) // convenience method, calls statics.applyReadFilter
  • applyWriteFilter(filterRole) // convenience method, calls statics.applyWriteFilter

Denormalize Usage

Denormalization functionality is provided via a schema plugin. This plugin has support for, but does not require, the filter.js plugin in the same package.

Schema

var denormalize = require('mongoose-filter-denormalize').denormalize;
var ObjectId = mongoose.Schema.ObjectId;
var UserSchema = new Mongoose.schema({
  name            :   String,
  transactions    :   [{type:ObjectId, ref:'Transaction'}],
  address         :   {type:ObjectId, ref:'Address'},
  tickets         :   [{type:ObjectId, ref:'Ticket'}],
  bankaccount     :   {type:ObjectId, ref:'BankAccount'}
});

// Running .denormalize() during a query will by default denormalize the selected defaults.
// Excluded collections are never denormalized, even when asked for.
// This is useful if passing query params directly to your methods.
UserSchema.plugin(denormalize, {defaults: ['address', 'transactions', 'tickets'],
                                exclude: 'bankaccount'});

Querying

// Create a query.
// The 'conditions' object allows you to query on denormalized objects!
var opts = {
  refs: ["transactions", "address"],    // Denormalize these refs. If blank, will use defaults
  filter: "public";                     // Filter requires use of filter.js and profiles
  conditions: {
    address: {city : {$eq: "Seattle"}}  // Only return the user if he is in Seattle
  }
};
User.findOne({name: 'Foo Bar'}).denormalize(opts).run(function(err, user){
  if(err) next(err);
  res.send({success: true, users: [user]});
});

Options

  • exclude (String[] or String): References to never denormalize, even when explicitly asked Use this when generating refs programmatically, to prevent unintended leakage.
  • defaults (String[] or String): References to denormalize when called without options. Defaults to all refs (except those in 'exclude'). Useful to define this if you have hasMany references that can easily get large.
  • suffix (String): A suffix to add to all denormalized objects. This is not yet supported in Mongoose but hopefully will be soon. E.g. a suffix of '_obj' would denormalize the story.comment object to story.comment_obj, leaving the id in story.comment. This is necessary for compatibility with ExtJS.

Notes

If you are building your array of refs to denormalize programmatically, make sure it returns an empty array if you do not want it to denormalize - falsy values will cause this plugin to use defaults.

Credits

Mongoose

License

MIT

mongoose-filter-denormalize's People

Contributors

aheckmann avatar indiejoseph avatar jacobwgillespie avatar strml avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongoose-filter-denormalize's Issues

Denormalize doesn't work for me in latest build.

EDIT:

It turns out this is a Mongoose 3.8 compatibility issue. Fixes are required to support the new Mongoose query builder mquery.

Not a bug, just needs updating.

Hi,

Using similar code to the example, I still can't get denormalize to work. I can however get Filter to work on it's own.

Route:

var CameraModel = require('../../../lib/models/camera');

exports.get = function(req, res){
    CameraModel.findById( req.params.id ).denormalize( {filter: 'public'} ).run( function(err, camera) {
        if (err) return res.json(500, { status: 'An error occurred' });
        return res.json(200, {success: true, data: camera});
    });
};

Schema:

var Mongoose = require('mongoose'),
    Schema = Mongoose.Schema,
    Filter = require('Mongoose-filter-denormalize').filter,
    Denormalize = require('Mongoose-filter-denormalize').denormalize;


var CameraSchema = new Schema({
    location:       { type: Mongoose.Schema.Types.ObjectId, ref: 'Location' },
    title:          { type: String, required: true },
});


CameraSchema.plugin(Filter, {
    readFilter: {
        "public": ['title', 'location']
    },
    sanitize: true
});


CameraSchema.plugin(Denormalize, {
    defaults: ['location']
});

module.exports = Mongoose.model('Camera', CameraSchema);

Resulting error:

TypeError: Object #<Query> has no method 'denormalize'
at exports.get (/Users/usernm/Dropbox/Sites/projName/releaseName/routes/v1/camera/index.js:32:43)
at callbacks (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/lib/router/index.js:164:37)
at param (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/lib/router/index.js:138:11)
at param (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/lib/router/index.js:135:11)
at pass (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/lib/router/index.js:145:5)
at Router._dispatch (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/lib/router/index.js:173:5)
at Object.router (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/lib/router/index.js:33:10)
at next (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/node_modules/connect/lib/proto.js:193:15)
at Object.methodOverride [as handle] (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:48:5)
at next (/Users/usernm/Dropbox/Sites/projName/releaseName/node_modules/express/node_modules/connect/lib/proto.js:193:15)

package.json

{
  "name": "Project...",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "~3.4.7",
    "mongoose": "~3.8.3",
    "mongoose-filter-denormalize": "~0.2.1"
  }
}

Working filtering query:

    CameraModel.findById( req.params.id, CameraModel.getReadFilterKeys('public'), function(err, camera) {
        if (err) return res.json(500, { status: 'An error occurred' });
        return res.json(200, {success: true, data: camera});
    });

Any ideas? Is it me?

Always returns '_id' - Why?

There is code specifically to let _id pass through the filter. What is the thinking behind this?

filters = filters ? filters.concat('_id') : filters; // Always send _id property

If you wish to use slugs or other unique tags to reference your objects, internal id's are unnecessary to expose to the public.

In my case, I am exposing the id but wish to expose it as 'id' not '_id'. This is easily achieved on the schema with the following snippet (Mongoose automatically makes 'id' a virtual schema object, which is purely an ObjectId pre-casted to a hex string):

// Set virtuals to true so they are evaluated on all items in a collectio
MySchema.set('toJSON', {
    virtuals: true
});

MySchema.plugin(Filter, {
    readFilter: {
        "public": ['id', 'title', 'location', 'description', 'active']
    }
});

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.