0.1.11 • Published 3 years ago

sequelize-paginate-easy v0.1.11

Weekly downloads
8
License
ISC
Repository
github
Last release
3 years ago

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:

PropsTypeRequiredDefault ValueDescription
pagenumberno1Number of page
limitnumberno10Items per page
orderarraynonullSorting like in sequelize version 6.3.5 and above
filterobjectnonullThis special params for searchin and filtering. See table below

Description of filter:

PropsTypeRequiredDefault ValueDescription
fieldsarraynonullFields property need for filtering by one or several fields. More information in table field
searchobjectnonullSearch property need for flexible searching by one or several field. More information in table search

Description of field:

PropsTypeRequiredDefault ValueDescription
namestringyesnullName of column in table. If you need to filter by inner fields use . for nesting. Example userRoles.id
valueanyyesnullValue which need to find

Description of search:

PropsTypeRequiredDefault ValueDescription
byarrayyesnullArray of columns in table. If you need to filter by inner fields use . for nesting. Example userRoles.id
querystringyesnullValue 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.

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago