Adds support for special query string params used for filtering data in FeatherJS
npm install feathers-query-filters --save
This is used internally in service adapters like Feathers MongoDB and Feathers NeDB.
Usage is like so:
var Proto = require('uberproto');
var filter = require('feathers-query-filters');
var CustomService = Proto.extend({
init: function(name, options){
// your custom initialization code
},
find: function(params, callback) {
// Start with finding all, and limit when necessary.
var query = this.db.find({});
// Prepare the special query params.
if (params.query) {
var filters = filter(params.query);
// $select uses a specific find syntax, so it has to come first.
if (filters.$select) {
query = this.db.find(params.query, filters.$select);
} else {
query = this.db.find(params.query);
}
// Handle $sort
if (filters.$sort){
query.sort(filters.$sort);
}
// Handle $limit
if (filters.$limit){
query.limit(filters.$limit);
}
// Handle $skip
if (filters.$skip){
query.skip(filters.$skip);
}
}
// Execute the query
query.exec(function(err, docs) {
if (err) {
return callback(err);
}
return callback(err, docs);
});
},
setup: function(app) {
// Called by feathers.configure()
this.app = app;
this.service = app.service.bind(app);
}
});
module.exports = CustomService;
The following keywords are supported. They are pulled from the query object's conditions and returned so they can be mapped by each adapter in their own way.
- Adding usage docs
- Initial release.