sequelize-paginate-easy

0.1.11 • Public • Published

Sequelize Paginate Easy

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.

Readme

Keywords

none

Package Sidebar

Install

npm i sequelize-paginate-easy

Weekly Downloads

8

Version

0.1.11

License

ISC

Unpacked Size

10.2 kB

Total Files

3

Last publish

Collaborators

  • ilyarudnitsky