Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add find by to preprocess #170

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/books/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var express = require('express'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
mongoose = require('mongoose'),
morgan = require('morgan'),
restful = require('../../');
var app = module.exports = express();


app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({type:'application/vnd.api+json'}));
app.use(methodOverride());
app.set('view engine', 'jade');
app.use((req, res, next) => {
req.findBy= {
'author': {'$eq': 'Ken Wilber' }
};
next();
});

app.mongoose = mongoose; // used for testing

mongoose.connect("mongodb://localhost/books_test");


var books = app.books = restful.model("books", mongoose.Schema({
title: { type: 'string', required: true },
year: { type: 'number', required: true },
}));

books.methods([
{
method: 'get',
before: noop,
after: noop
},
'post',
'put',
'delete'])
.updateOptions({ new: true })

books.register(app, '/api/books');

if (!module.parent) {
app.listen(3000);
}

function noop(req, res, next) { next(); }

19 changes: 16 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ Model.filter = function(req, quer) {
function preprocess(req, res, next) {
req.body = req.body || {};
req.query = req.query || {};
req.quer = this.filter(req, this.find({}));
req.findBy = req.findBy || {};
req.quer = this.filter(req, this.find(req.findBy));
if (!('locals' in res)) {
res.locals = {};
}
Expand Down Expand Up @@ -469,11 +470,23 @@ function filterable(props, subfilters) {

return subfilters[filter_func](data, quer.where(field[0]));
},
contains: function(key, quer) {
contains: function (key, quer) {
if (key in props) return true;
var field = key.split('__');
var filter_func = field[1] || 'equals';
return field[0] in quer.model.schema.paths && filter_func in subfilters;

var prop = field[0];

if (prop.split('.').length) {
var path = prop.split('.');
var path = quer.model.schema.paths[path[0]];

if (path && path.instance === 'Array') {
prop = prop.split('.')[0];
}
}

return prop in quer.model.schema.paths && filter_func in subfilters;
}
}
}
Loading