strml / mongoose-filter-denormalize Goto Github PK
View Code? Open in Web Editor NEWSimple filtering and denormalization for Mongoose.
Simple filtering and denormalization for Mongoose.
The latest version on npm is 0.1.0 which does not include the patch to support mongoose 3.4.x
please make it work with latest mongoose
The newer mongoose versions want a string for the select
There is code specifically to let _id pass through the filter. What is the thinking behind this?
mongoose-filter-denormalize/lib/filter.js
Line 107 in f5cdb9a
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']
}
});
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.