0.4.3 • Published 8 years ago

express-api-queryhandler v0.4.3

Weekly downloads
24
License
MIT
Repository
github
Last release
8 years ago

express-api-queryhandler Build Status Dependency Status

Query handling middleware for Express/Mongoose APIs.

Install

$ npm install --save express-api-queryhandler

Usage

var express = require('express');
var queryHandler = require('express-api-queryhandler');

var app = express();

app.use(queryHandler.fields());
app.use(queryHandler.filter());
app.use(queryHandler.pagination({limit: 25}));
app.use(queryHandler.sort());

app.get('/items', function (req, res, next) {
  Item.find(req.where, req.fields, req.options, function (err, items) {
    if (err) return next(err);
    res.json(items);
  });
});

queryHandler.fields()

Handles a fields query parameter with a comma separated string of field names. The value will be provided as req.fields and can be used for limiting returned fields.

GET /items?fields=name,description

queryHandler.filter(options)

Handles query parameters with field names. The values will be provided as req.where and can be used for filtering database queries.

GET /items?active=true

Options

skip (optional)

Skip these query parameters and avoid empty results. By default it also skips fields, limit, offset and sort.

queryHandler.pagination(options)

Handles limit and offset query parameters. The values will be provided as req.options.limit and req.options.skip and can be used for pagination.

GET /items?limit=50

Options

limit (optional, default: 10)

Default limit if not specified with a query parameter.

queryHandler.sort()

Handles a sort query parameter with a comma separated string of field names. The sort order of each field is ascending unless the name is prefixed with -. The value will be supplied as req.options.sort and can be used for sorting records.

GET /items?sort=-name

License

MIT © Richard Käll