0.0.9 • Published 7 years ago

egg-rest-query v0.0.9

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

egg-rest-query

NPM version build status Test coverage David deps Known Vulnerabilities npm download

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.

License

MIT

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago