-
Notifications
You must be signed in to change notification settings - Fork 5
Find Query
The default find
block automatically implements find()
(all) and findOne()
(by id
) endpoints. However, it doesn't support querystring parameters for pagination or filtering out of the box.
To implement this functionality, you need to provide a query
function to the find
block. The function receives the express request object req
as the only argument. It should return a mongo query object.
The JSON:API documentation specifies:
The
filter
query parameter is reserved for filtering data. Servers and clients SHOULD use this key for filtering operations.
One quick way to implement static filters would be:
module.exports.autoroute = autorouteJson({
model: models.animals,
find: {
query: function(req) {
if (req.query.filter) {
return req.query.filter
}
}
}
});
Querystring:
filter[category] = humans
In this example, any attribute in the filter
querystring argument will be filtered by its value.
Here is a customized 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 },
]},
}
}
}
});
Please note that for JSON:API compliance, you should always use filter
.