# sequelize-pagination-data

> Paginacion para sequelize desde query string

Latest version **0.2.5** (published 2020-05-02) · ISC license · 0 weekly downloads

## Install

```sh
npm install sequelize-pagination-data
pnpm add sequelize-pagination-data
yarn add sequelize-pagination-data
bun add sequelize-pagination-data
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2020-05-02 |
| First published | 2020-05-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| Author | Christian Chumbita |
| Maintainers | chumbitac |
| Keywords | sequelize, pagination, paginate |

## Links

- npm: https://www.npmjs.com/package/sequelize-pagination-data
- npm.io page: https://npm.io/package/sequelize-pagination-data

## Dependencies (1)

- [sequelize](https://npm.io/package/sequelize.md) ^5.21.7

## Recent versions

- 0.2.5 (latest) — 2020-05-02
- 0.2.4 — 2020-05-02
- 0.2.3 — 2020-05-02
- 0.2.2 — 2020-05-02
- 0.2.1 — 2020-05-02
- 0.2.0 — 2020-05-01
- 0.1.0 — 2020-05-01

## README

# Express Query Parser for Sequelize (Beta)
## Installation

```bash
npm i --save sequelize-pagination-data
```
## Features
* Choose the fields to display using pagination or not.
* Pagination middleware for express using page, per page (limit) in the query string parameters.
* Multiple sort support.
* Show paging results in response.
* Filters (coming soon).

## Usage

### Select fields
> Example URL for show some fields:
**http://localhost:3000/clients?fields=id,firstname,lastname**

> Example URL for show some fields with alias:
**http://localhost:3000/clients?fields=id,firstname:name,lastname**

Use middleware in route:
```javascript
// In route file
const { parser } = require('sequelize-pagination-data');
//...
Router.get("/", parser, controller.getAll())
```

Use in controller:
```javascript
//...
async getAll(req, res) {
    let options = {
        attributes: req.sq_parser.attributes
    };

    let clients = await Client.findAll({...options});

    return res.status(200).json({
        data: clients
    });
}
```

```javascript
//...
async getOne(req, res) {
    let options = {
        where: {
            id: req.params.id
        },
        attributes: req.sq_parser.attributes
    };

    let clients = await Client.findOne({...options});

    return res.status(200).json({
        data: clients
    });
}
```

### Sorts
> Example URL for sort:
**http://localhost:3000/clients?sort=firstname**

> Example URL for multiple sort:
**http://localhost:3000/clients?sort=-firstname,lastname**

```javascript
//...
async getAll(req, res) {
    let options = {
        order: req.sq_parser.order
    };

    let clients = await Client.findAll({...options});

    return res.status(200).json({
        data: clients
    });
}
```

### Paginate
> Example URL for paginate:
**http://localhost:3000/clients?page=3,per_page=10**

```javascript
const { paginate } = require('sequelize-pagination-data');
//...
async getAll(req, res) {

    let count = await Client.count();

    let pagination = await paginate(req.sq_parser.options.limit, count, req.sq_parser.page)

    let options = {
        ...pagination.options,
        ...req.sq_parser.options
    };

    let clients = await Client.findAll({...options});

    return res.status(200).json({
        data: clients,
        pagination: pagination.response
    });
}
```

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