Simple collection filtering and denormalization.
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.
npm install mongoose-filter-denormalize
Filtering functionality is provided via a schema plugin.
var filter = require'mongoose-filter-denormalize'filter;var ObjectId = mongooseSchemaObjectId;var UserSchema =name : Stringaddress : Stringfb :id : NumberaccessToken : StringwriteOnlyField : StringreadOnlyField : String;UserSchemapluginfilterreadFilter:"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 thisdefaultFilterRole: 'nofilter'sanitize: true // Escape HTML in strings;
UserfindOnename: 'Foo Bar' UsergetReadFilterKeys'public'iferr nexterr;ressendsuccess: true users: user;;
UserfindByIdreqparamsidiferr nexterr;ifuserid !== requserid next403;userextendWithWriteFilterinputRecord 'owner'; // Similar to jQuery.extend()usersaveiferr nexterr;userapplyReadFilter'owner'; // Make sure the doc you return does not contain forbidden fieldsressendsuccess: true users: user;;;
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.
This plugin adds the following statics to your schema:
_applyFilter(input, filterKeys) // private helper
_getFilterKeys(type, filterRole) // private helper
This plugin adds the following methods to your schema:
applyReadFilter(filterRole) // convenience method, calls statics.applyReadFilter
applyWriteFilter(filterRole) // convenience method, calls statics.applyWriteFilter
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.
var denormalize = require'mongoose-filter-denormalize'denormalize;var ObjectId = mongooseSchemaObjectId;var UserSchema =name : Stringtransactions : 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.UserSchemaplugindenormalize defaults: 'address' 'transactions' 'tickets'exclude: 'bankaccount';
// 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 defaultsfilter: "public"; // Filter requires use of filter.js and profilesconditions:address: city : $eq: "Seattle" // Only return the user if he is in Seattle;UserfindOnename: 'Foo Bar'denormalizeoptsruniferr nexterr;ressendsuccess: true users: user;;
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.
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.
Copyright (C) 2012 by Samuel T. Reed email@example.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.