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-paginator

v1.0.4

Published

Sequlize paginator is very easy package to paginate any model | both rendering : SSR / CSR

Downloads

8

Readme

Sequelize Paginator

Sequelize Paginator is a utility package for handling pagination with Sequelize models in Node.js applications. It provides easy-to-use functions for paginating query results and generating pagination links with Bootstrap and Tailwind CSS styles.

Installation

You can install the package using npm or yarn:

npm install sequelize-paginator

Setup

In Your Models

To set up Sequelize Paginator in your Sequelize models, follow these steps:

Import the package:

const { findAndPaginate } = require("sequelize-paginator");

1 - define

// Define the model
const MyModel = sequelize.define('MyModel', {
  // Your model attributes here
  name: DataTypes.STRING,
  age: DataTypes.INTEGER,
});

// Add a findAndPaginate to the model
MyModel.findAndPaginate = findAndPaginate ;

2 - init

  Cours.init(
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },

    },
    {
      sequelize,
      modelName: "Cours",
    }
  );
   Cours.findAndPaginate = findAndPaginate;

3 - For each Sequelize model you want to paginate, add the findAndPaginate function to it. For example:

fs.readdirSync(__dirname)
  .filter((file) => {
    return file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js" && file.indexOf(".test.js") === -1;
  })
  .forEach((file) => {
    const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
    db[model.name] = model;
    model.findAndPaginate = findAndPaginate;
  });

In Your Application

To use Sequelize Paginator in your application, you can follow these examples:

Basic Api Usage | CSR


  result = await YourModel.findAndPaginate(5, req);
  res.json(result);

result

Capture

BootstrapLinks

const { bootstrapLinks  } = require("sequelize-paginator");

// Inside your route or controller:
const result = await YourModel.findAndPaginate(6, req, {});
const paginationLinks = bootstrapLinks(result.links);

res.render("your_template", { result.data, paginationLinks });
   <!-- OR USE
  res.send(` <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3 azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"></link>
${paginationLinks} `); -->

In this example, YourModel should be replaced with your Sequelize model, and "your_template" should be replaced with the name of your template or view.

TailwindLinks

const { tailwindLinks } = require("sequelize-paginator");

// Inside your route or controller:
const result = await YourModel.findAndPaginate(10, req, {});
const paginationLinks = tailwindLinks(result.links);

res.render("your_template", { result.data, paginationLinks });

Again, replace YourModel and "your_template" with your model and template name, respectively.

Usage with addQueryString

The addQueryString function is used to append query parameters to pagination links. It ensures that the pagination links retain any existing query parameters while adding the necessary pagination-related parameters.

const { addQueryString , bootstrapLinks } = require("sequelize-paginator");

const result = await YourModel.findAndPaginate(10, req, {});
const paginationLinks = bootstrapLinks(addQueryString(result.links, req.query, "page"));

Usage with where Operators

const { Op } = require("sequelize");
  data = await Cours.findAndPaginate(5, req, { where: { id: { [Op.gt]: 100 } } });
  data2 = await User.findAndPaginate(10, req, { where: { id: { [Op.lt]: 100 } } });

functions explanation

findAndPaginate: This is an asynchronous function that accepts parameters for pagination. It retrieves data based on these parameters and returns a paginated response. It also generates pagination links.

generatePaginationLinks: A nested function within findAndPaginate, it generates pagination links based on the current page, total pages, and other parameters. It handles different cases for pagination link generation, including ellipsis and disabled links.

bootstrapLinks: This function generates Bootstrap-styled pagination links based on the data provided to it. It creates HTML for pagination using Bootstrap classes.

tailwindLinks: This function generates Tailwind CSS-styled pagination links based on the data provided to it. It creates HTML for pagination using Tailwind CSS classes.

addQueryString: This function appends query parameters to pagination links. It ensures that the pagination links retain any existing query parameters while adding the necessary pagination-related parameters.

buildQueryString: A helper function that converts a query object into a query string format.

At the end of the code, you export these functions for use in other parts of your application.

This code provides a comprehensive solution for handling pagination and generating pagination links in both Bootstrap and Tailwind CSS styles, making it versatile and adaptable for various web development projects.

Server-Side Rendering (SSR) and Client-Side Rendering (CSR)

Sequelize Paginator can be used in both server-side rendering (SSR) and client-side rendering (CSR) applications. Simply import the necessary functions and use them in your routes, controllers, or components as shown in the examples above.

For SSR, you might use frameworks like Express.js, while for CSR, you can integrate Sequelize Paginator with your frontend JavaScript framework of choice.

Feel free to customize the pagination links' styles to match your project's design using Bootstrap or Tailwind CSS classes.