Expose mongoose query API through HTTP request with partial support of json-api:
Note: Checkout the current specification for more information.
$ npm install --save express-mquery
import expess from 'express';
import mquery from 'express-mquery';
const app = express();
app.use(mquery({ limit: 10, maxLimit: 50 }));
...
app.get('/users', (request, response, next) => {
// obtain request.mquery
console.log(request.mquery);
});
Once parse, express-mquery
will extend http request
with mquery
field
Example:
GET /invoices?fields=number,amount&filter[name]=Bob
&filter[amount][$gte]=1200&include=customer,items
&fields[customer]=name,number&fields[items]=name,price
&page[number]=1&page[size]=10&sort[number]=1&sort[amount]=-1
Will be parsed into:
{
filter: { name: "Bob", amount: { $gte: 1200 } },
paginate: { limit: 10, skip: 0, page: 1 },
populate: [
{ path: "customer", select: { name: 1, number: 1 } },
{ path: "items", select: { name: 1, price: 1 } }
],
select: { number: 1, amount: 1 },
sort: { number: 1, amount: -1 }
}
Where:
filter
: Is validmongoose
criteria and can be passed tofind()
paginate
: Contains paging details that can be passed tolimit()
,skip()
populate
: Is validmongoose
populate option and can be passed topopulate()
select
: Is validmongoose
project options and can be passed toselect()
sort
: Is validmongoose
sort options and can be passed tosort()
When passing values as objects or arrays in URLs, they must be valid JSON
GET /customers?sort=name
GET /customers?sort=-name
GET /customers?sort={"name":1}
GET /customers?sort={"name":1, "email":-1}
or
GET /customers?sort=name
GET /customers?sort=-name
GET /customers?sort[name]=1&sort[email]=-1
GET /customers?page=1
GET /customers?page=1&limit=10
GET /customers?page[number]=1&page[size]=10
GET /customers?skip=10
Only overrides maximum limit option set by the plugin
if the queried limit is lower
GET /customers?limit=10
Supports all mongodb operators ($regex, $gt, $gte, $lt, $lte, $ne, etc.)
GET /customers?query={"name":"Bob"}
GET /customers?query={"name":{"$regex":"/Bo$/"}}
GET /customers?query={"age":{"$gt":12}}
GET /customers?query={"age":{"$gte":12}}
or
GET /customers?filter[name]=Bob
GET /customers?filter[name][$regex]="/Bo$/"
GET /customers?filter[age][$gt]=12
GET /customers?filter[age][$gte]=12
Works with create, read and update operations
GET /invoices?populate=customer
GET /invoices?populate={"path":"customer"}
GET /invoices?populate=[{"path":"customer"},{"path":"products"}]
or
GET /invoices?include=customer
GET /invoices?include[customer]=name,number&includes[items]=name,price
GET /invoices?include=customer,items&fields[customer]=name,number&fields[items]=name,price
_id
is always returned unless explicitely excluded
GET /customers?select=name
GET /customers?select=-name
GET /customers?select={"name":1}
GET /customers?select={"name":0}
or
GET /customers?fields=name
GET /customers?fields=-name
GET /customers?fields=name,email
GET /invoices?include=customer&fields[customer]=name
-
Clone this repository
-
Install
grunt-cli
global
$ npm install -g grunt-cli
- Install all development dependencies
$ npm install
- Then run test
$ npm test
Fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.
Copyright (c) lykmapipo & Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.