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-paginate-easy

v0.1.11

Published

sequelize paginate easy using

Downloads

25

Readme

Description

Sequelize helper for add method of pagination.

Install

npm install --save sequelize-paginate-easy

Requirements

  • Sequilize version must be 6.3.5 and above

How to use?

  1. Firstly you need connect model to easy sequelize pagination:
const ePagination = require("sequelize-paginate-easy");
const models = require("./path/to/models");

const defaultParams = {
  order: [["id", "DESC"]],
  likeOperatorSearch: "%value%",
  caseInsensitive: true,
  limit: 25,
  page: 3
};

const user = ePagination(models.user, defaultParams);
  1. Then on some method/endpoint use paginate function:
const getUsers = async query => {
  const res = await models.user.paginate(query);

  return res;
};

getUsers(shapeOfQuery);

/*
  Result object

  {
    items,
    meta: {
      pages,
      all,
      hasNext,
      hasPrevious,
      currentPage,
      limit
    }
  }
*/

How to use with scope?

  1. Firstly you need connect model to easy sequelize pagination:
const ePagination = require("sequelize-paginate-easy");
const models = require("./path/to/models");

const defaultParams = {
  order: [["id", "DESC"]],
  limit: 25,
  likeOperatorSearch: "%value%",
  caseInsensitive: true,
  page: 3
};

const user = ePagination(models.user, defaultParams);

/*
  Important!

  Use addScope method of sequeilze after connecting ePagination
*/

user.addScope("nameOfScope", {
  where: {
    attr: "value"
  }
  include: [
    {
      model: models.role,
      as: "userRoles",
      through: models.user_role,
      required: false
    },
    {
      model: models.location,
      as: "location",
      required: false
    }
  ]
});

/*
  At this moment scope works only with include and where shape
*/
  1. Then on some method/endpoint use paginate function:
const getUsers = async query => {
  const res = await models.user.paginate(query, "nameOfScope");

  return res;
};

getUsers(shapeOfQuery);

/*
  Result object

  {
    items,
    meta: {
      pages,
      all,
      hasNext,
      hasPrevious,
      currentPage,
      limit
    }
  }
*/

Additional params

user.addScope("nameOfScope", params => {
  console.log(params); // Your params

  return {
    include: [
      {
        model: models.role,
        as: "userRoles",
        through: {
          model: models.user_role,
          where: {
            role: params.user.role
          }
        },
        required: false
      },
      {
        model: models.location,
        as: "location",
        required: false
      }
    ]
  };
});

/*
  At this moment scope works only with include shape
*/

const getUsers = async query => {
  const myAdditionalParams = {
    user: {
      role: "admin"
    }
  };

  const res = await models.user.paginate(
    query,
    "nameOfScope",
    myAdditionalParams
  );

  return res;
};

getUsers(shapeOfQuery);

What is shape of query?

Example:

const query = {
  page: 1,
  limit: 10,
  order: [["userRoles", "id", "DESC"]],
  filter: {
    fields: [
      {
        name: "discountRoles.id",
        value: "3a4df7f2-7afd-4af0-9631-ba19401d3ebe"
      },
      {
        name: "location.id",
        value: "08c44256-af88-474e-885a-72245247e945"
      }
    ],
    search: {
      by: ["email"],
      query: "d"
    }
  }
};

Description:

| Props | Type | Required | Default Value | Description | | -------- | ---------- | -------- | ------------- | --------------------------------------------------------------- | | page | number | no | 1 | Number of page | | limit | number | no | 10 | Items per page | | order | array | no | null | Sorting like in sequelize version 6.3.5 and above | | filter | object | no | null | This special params for searchin and filtering. See table below |

Description of filter:

| Props | Type | Required | Default Value | Description | | -------- | ---------- | -------- | ------------- | ------------------------------------------------------------------------------------------------------- | | fields | array | no | null | Fields property need for filtering by one or several fields. More information in table field | | search | object | no | null | Search property need for flexible searching by one or several field. More information in table search |

Description of field:

| Props | Type | Required | Default Value | Description | | ------- | ---------- | -------- | ------------- | ---------------------------------------------------------------------------------------------------------- | | name | string | yes | null | Name of column in table. If you need to filter by inner fields use . for nesting. Example userRoles.id | | value | any | yes | null | Value which need to find |

Description of search:

| Props | Type | Required | Default Value | Description | | ------- | ---------- | -------- | ------------- | ------------------------------------------------------------------------------------------------------------ | | by | array | yes | null | Array of columns in table. If you need to filter by inner fields use . for nesting. Example userRoles.id | | query | string | yes | null | Value which need to find |

What is likeOperatorSearch property?

This property has by default %value% value. For customysing you can pass another like condition.

For example: value__%

Important!

This string must contain value word. Instead of this value will be substituted with the search query.