0.2.5 • Published 4 years ago

sequelize-pagination-data v0.2.5

Weekly downloads
2
License
ISC
Repository
-
Last release
4 years ago

Express Query Parser for Sequelize (Beta)

Installation

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:

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

Use in controller:

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

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

    return res.status(200).json({
        data: clients
    });
}
//...
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

//...
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

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
    });
}
0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.2

4 years ago

0.1.0

4 years ago