# express-api-queryhandler

> Query handling middleware for Express/Mongoose APIs.

Latest version **0.4.3** (published 2016-03-08) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install express-api-queryhandler
pnpm add express-api-queryhandler
yarn add express-api-queryhandler
bun add express-api-queryhandler
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.4.3 |
| Published | 2016-03-08 |
| First published | 2015-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Richard Käll |
| Maintainers | richardkall |
| Keywords | api, express, fields, filter, json, middleware, mongoose, order, pagination, query, sort |

## Links

- npm: https://www.npmjs.com/package/express-api-queryhandler
- Repository: https://github.com/richardkall/express-api-queryhandler
- Homepage: https://github.com/richardkall/express-api-queryhandler#readme
- Issues: https://github.com/richardkall/express-api-queryhandler/issues
- npm.io page: https://npm.io/package/express-api-queryhandler

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.4.3 (latest) — 2016-03-08
- 0.4.2 — 2015-11-20
- 0.4.1 — 2015-05-15

## README

# express-api-queryhandler [![Build Status](https://travis-ci.org/richardkall/express-api-queryhandler.svg)](https://travis-ci.org/richardkall/express-api-queryhandler) [![Dependency Status](https://gemnasium.com/richardkall/express-api-queryhandler.svg)](https://gemnasium.com/richardkall/express-api-queryhandler)

> Query handling middleware for Express/Mongoose APIs.

## Install

```bash
$ npm install --save express-api-queryhandler
```

## Usage

```javascript
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 &copy; [Richard Käll](http://richardkall.se)

---
_Source: https://npm.io/package/express-api-queryhandler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
