parsing request query into usable db condition for mongoDB etc...
request: /api/product?fields=name&sort=createdAt&filter=productName eq basement and createdAt gt datetimeoffset'2017-03-28T16:40:09.724Z' and startswith(name, 'base')
patch parsing result into ctx.extendQuery
with values:
{
mongoFilter: {
$and: [{
productName: {
$eq: 'basement',
},
}, {
$and: [{
createdAt: {
$gt: { __type : 'date', iso: '2017-03-28T16:40:09.724Z' },
},
}, {
name: /^base/i,
}],
}],
},
mongoSort: 'createdAt',
mongoFields: 'name',
}
and query in mongoDB
const { mongoFilter, mongoSort, mongoFields, mongoInclude, skip, top } = ctx.extendQuery;
this.db.class('product').find({
where: mongoFilter,
sort: mongoSort,
fields: mongoFields,
skip,
top,
include: mongoInclude,
});
more filter supported like startswith, endswith, substringof, view odata-parser
$ npm i egg-rest-query --save
// {app_root}/config/plugin.js
exports['rest-query'] = {
enable: true,
package: 'egg-rest-query',
};
then extendQuery
with be available on ctx
// {app_root}/config/config.default.js
exports['rest-query'] = {
};
see config/config.default.js for more detail.
Please open an issue here.