npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequelize-api-paginate

v1.3.5

Published

Sequelize paging for API. Quickly and simple to use

Downloads

49

Readme

sequelize-api-paginate

Sequelize paging for API. Quickly and simple to use

Install

npm install sequelize-api-paginate

Usage

const sequelizeApiPaginate = require("sequelize-api-paginate");

Send a request

With all the above in place, you can now send a GET request that includes a sort/filter/page query. An example:

GET /GetPosts

?sortField=      LikeCount                               // sort by likes
?sortOrder=      desc,asc                                // sort by descending or ascending
&filters=        LikeCount>10, Title@=awesome title,     // filter to posts with more than 10 likes, and a title that contains the phrase "awesome title"
&currentPage=    1                                       // get the first page...
&pageSize=       10                                      // ...which contains 10 posts

More formally:

  • sortField is a comma-delimited ordered list of property names to sort by
  • filters is a comma-delimited list of {Name}{Operator}{Value} where
    • {Name} is the name of a property with the Sieve attribute or the name of a custom filter method for TEntity
      • You can also have multiple names (for OR logic) by enclosing them in brackets and using a pipe delimiter, eg. (LikeCount|CommentCount)>10 asks if LikeCount or CommentCount is >10
    • {Operator} is one of the Operators
    • {Value} is the value to use for filtering
      • You can also have multiple values (for OR logic) by using a pipe delimiter, eg. Title@=new|hot will return posts with titles that contain the text "new" or "hot"
  • currentPage is the number of page to return
  • pageSize is the number of items returned per page

Operators

| Operator | Meaning | Example | | -------- | ------------------------------------ | ------------------------------------- | | == | Equals | name==example | | != | Not equals | name!=example | | > | Greater than | cost>2000 | | < | Less than | cost<2000 | | >= | Greater than or equal to | cost>=2000 | | <= | Less than or equal to | cost<=2000 | | @= | Contains | id@=a | | _= | Starts with | email_=c | | !@= | Does not Contains | email!@=mail.com | | !_= | Does not Starts with | email!_=a | | [] | Only datetime, date between two date | created_at[](2021/11/05-2021/11/12) |

Example

Add sequelizeApiPaginate for your API like that:

router.get(
  "/listUsers",
  sequelizeApiPaginate.middle,
  async function (req, res, next) {
    //Paging with default query in library
    let includes = [];
    let hierarchy = false;
    let raw = true;
    let nest = true;

    var listUserAfterPaging = await sequelizeApiPaginate.query(
      model.User,
      req.payload,
      includes, //not required,
      hierarchy, //not required
      raw, //not required
      nest //not required
    );

    res.json({ success: true, contents: listUserAfterPaging });
  }
);

For middleware it auto generate payload base on params pass in.

The Params like that:

const payload = {
  pageSize: req.query.pageSize || null, //Default: 10
  sortField: req.query.sortField || null, //Default: 1
  sortOrder: req.query.sortOrder || null,
  currentPage: req.query.currentPage || null, //Default: 1
  filters: req.query.filters || null,
};

Example call API with Payload:

const payload = {
  pageSize: 3, //Default: 10
  sortField: "created", //Default: 1
  sortOrder: "desc",
  currentPage: 1,
  filters: "gender==1",
};

Endpoint of API like that:

http://localhost:3000/listUsers?currentPage=1&pageSize=3&sortField=created&sortOrder=desc&filters=gender==1

The Response like that:

{
    success: true,
    contents: {
        count: 20,
        rows: {
            {
                id: 'Mash Melon',
                gender: 1,
                age: 32
            },
             {
                id: 'John Kavas',
                gender: 1,
                age: 16
            },
             {
                id: 'Matric Eric',
                gender: 1,
                age: 23
            }
        },
        totalPages: 7,
        currentPage: 1
    }
}

Noted

Please report bug to be fixed~ Many thanks all