npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

express-mquery

1.1.10 • Public • Published

express-mquery

Build Status

Expose mongoose query API through HTTP request with partial support of json-api:

Note: Due to refactoring, you must install mongoose-rest-actions to have back mongoose plugin

Installation

$ npm install --save express-mquery mongoose-rest-actions

Usage

//plugin rest actions plugin
const mongoose = require('mongoose');
const actions = require('mongoose-rest-actions');
mongoose.plugin(actions);
 
//add express middleware
const expess = require('express');
const mquery = require('express-mquery');
 
const app = express();
app.use(mquery({ limit: 10, maxLimit: 50 }));
 
...
 
app.get('/users', function(request, response, next) {
  
  //obtain request.mquery
  const options = request.mquery;
 
  User
    .get(options, function(error, results) {
      ...handle error or reply
    });
 
});

Structure

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 valid mongoose criteria and can be passed to find()
  • paginate : Contains paging details that can be passed to limit(), skip()
  • populate : Is valid mongoose populate option and can be passed to populate()
  • select : Is valid mongoose project options and can be passed to select()
  • sort : Is valid mongoose sort options and can be passed to sort()

Querying

When passing values as objects or arrays in URLs, they must be valid JSON

Sort

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

Page

GET /customers?page=1
GET /customers?page=1&limit=10
GET /customers?page[number]=1&page[size]=10

Skip

GET /customers?skip=10

Limit

Only overrides maximum limit option set by the plugin if the queried limit is lower

GET /customers?limit=10

Query or Filters

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

Populate or Include

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

Select or Fields

_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

Testing

  • Clone this repository

  • Install grunt-cli global

$ npm install -g grunt-cli
  • Install all development dependencies
$ npm install
  • Then run test
$ npm test

Contribute

Fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.

Licence

Copyright (c) 2015 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.

install

npm i express-mquery

Downloadsweekly downloads

171

version

1.1.10

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability