egg-rest-query
v0.0.9
Published
parsing odata style query into mongoDB condition
Downloads
4
Readme
egg-rest-query
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
Install
$ npm i egg-rest-query --save
Usage
// {app_root}/config/plugin.js
exports['rest-query'] = {
enable: true,
package: 'egg-rest-query',
};
then extendQuery
with be available on ctx
Configuration
// {app_root}/config/config.default.js
exports['rest-query'] = {
};
see config/config.default.js for more detail.
Example
Questions & Suggestions
Please open an issue here.