-
Notifications
You must be signed in to change notification settings - Fork 5
Find Query
Chris Manson edited this page Nov 24, 2016
·
2 revisions
The default find block automatically implements "Find All" and "Find by Id" endpoints but out of the box it doesn't support query params or filtering the results.
To allow for filtering you need to provide a query function to the find block. The query receives the express request object as the only parameter and needs to return an object that follows the mongo query syntax
module.exports.autoroute = autorouteJson({
model: models.account,
find: {
query: function(req) {
if (req.query.projectId) {
return {
project: req.query.projectId,
}
}
}
}
});
In most cases the query function will be translating the query parameters (req.query) into a mongo query document. In this example, if you provide a projectId on the query parameters we will return accounts who's project is set as the provided projectId
Here is a slightly larger example:
module.exports.autoroute = autorouteJson({
model: models.account,
find: {
query: function(req) {
return {
project: req.query.projectId,
date: { $gt: req.query.earliest },
cost: { $and: [
{ $gt: req.query.minCost },
{ $lt: req.query.maxCost },
]},
}
}
}
});