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

prisma-extension-paginate

v1.0.5

Published

Prisma extension used to paginate queries performed by the Prisma client

Downloads

227

Readme

Prisma-Extension-Paginate

Prisma client extension which allows you to perform pagination quickly and easily

📚 Table of Contents

🏗️ Installation

⚠️Note: This extension requires Prisma version 4.9.0 or higher.

To install the extension, run the following command:

npm install prisma-extension-paginate

Add to prisma client

import { PrismaClient } from "@prisma/client";
import paginate from "prisma-extension-paginate";

const prisma = new PrismaClient().$extends(
    paginate()
);

Adding default options

const prisma = new PrismaClient().$extends(
    paginate({
        offset: {
            perPage: 30
        },
        cursor: {
            limit: 30,
            setCursor(cursor) {
                return { id: cursor }
            },
            getCursor(target) {
                return (target as any).id
            },
        }
    })
);

🧩 Usage

🦚 Quick Start

It's possible to perform a query and receive its metadata

const prisma = new PrismaClient().$extends(
  paginate()
);

const [ data, meta ] = await prisma.user.paginate({
  offset: {
    page: 1,
    perPage: 10
  },
});

/* meta content: 
*    {
*        totalCount: 40,
*        pageCount: 10,
*        totalPages: 4,
*        currentPage: 1,
*        previousPage:  null
*        nextPage: 2,
*    }
*/

🪜 Offset Pagination

The offset pagination model uses only two parameters page and perPage

Loading the first page

const [ data, meta ] = await prisma.user.paginate({
  offset: {
    perPage: 10
  },
});

/* meta content: 
*    {
*        totalCount: 40,
*        pageCount: 10,
*        totalPages: 4,
*        currentPage: 1,
*        previousPage: null,
*        nextPage: 2 
*    }
*/

Getting an arbitrary page

const [ data, meta ] = await prisma.user.paginate({
  offset: {
    page: 3,
    perPage: 10
  },
});

/* meta content: 
*    {
*        totalCount: 40,
*        pageCount: 10,
*        totalPages: 4,
*        currentPage: 3,
*        previousPage: 2,
*        nextPage: 4 
*    }
*/

Getting all items

const [ data, meta ] = await prisma.user.paginate({
  offset: {  
    perPage: 10 // Returns all items if 'per Page' is negative
  },
});

/* meta content: 
*    {
*        totalCount: 40,
*        pageCount: 40,
*        totalPages: 1,
*        currentPage: 1,
*        previousPage: null,
*        nextPage: null
*    }
*/

Default properties can be overridden

const prisma = new PrismaClient().$extends(
  paginate({
    offset: {
      perPage: 10
    }
  })
)

const [ data, meta ] = await prisma.user.paginate({
  offset: {
    page: 1,
    perPage: 20 // overwrite
  },
});

/* meta content: 
*    {
*        totalCount: 40,
*        pageCount: 20,
*        totalPages: 1,
*        currentPage: 1,
*        previousPage: null,
*        nextPage: 2
*    }
*/

⤴️ Cursor-based Pagination

The cursor-based pagination model uses after or before to perform queries, and can also receive limit to determine the number of items per query.

By default, cursor pagination uses the 'id' property as a parameter. This can be changed by overriding the getCursor and setCursor functions.

const prisma = new PrismaClient().$extends(
    paginate({
        cursor: {
            limit: 10,
            // Using userId as a cursor
            setCursor(cursor) {
                return { userId: cursor }
            },
            getCursor(target) {
                return (target as any).userId
            },
        }
    })
)

const [ data, meta ] = await prisma.user.paginate({
    cursor: {
        after: "01914d98-79db-7dda-8976-88a8025dfd32",
        limit: 20
    }
})

Getting the first results

const [ data, meta ] = await prisma.user.paginate({
    cursor: {
        limit: 20
    }
})

/* meta content: 
* {
*    hasPreviousPage: false,
*    hasNextPage: true,
*    startCursor: 98,
*    endCursor: 118
* }
*/

Load next page

const [ data, meta ] = await prisma.user.paginate({
    cursor: {
        after: 118,
        limit: 20
    }
})

/* meta content: 
* {
*    hasPreviousPage: true,
*    hasNextPage: false,
*    startCursor: 118,
*    endCursor: 138
* }
*/

Load previous page

const [ data, meta ] = await prisma.user.paginate({
    cursor: {
        before: 118,
        limit: 20
    }
})

/* meta content: 
* {
*    hasPreviousPage: false,
*    hasNextPage: true,
*    startCursor: 98,
*    endCursor: 118
* }
*/

overriding the getCursor and setCursor functions.

// specific function
function setCursor(cursor: string | number) {
    if (typeof cursor !== "string" ) {
        cursor = cursor.toString()
    }
    const [userId, createdAt] = cursor.split(",")
    return { userId, createdAt }
}

// specific function
function getCursor(target) {
    return `${target.userId},${target.createdAt}`
}

const { data, meta } = await prisma.user.paginate({
    cursor: {
        after: "01914d98-79db-7dda-8976-88a8025dfd32,2024-08-22T00:00:00Z",
        limit: 20,
        setCursor: setCursor,
        getCursor: getCursor,
    }
})

/* meta content: 
* {
*    hasPreviousPage: false,
*    hasNextPage: true,
*    startCursor: "01914d98-79db-7dda-8976-88a8025dfd32,2024-08-22T00:00:00Z",
*    endCursor: "01914dba-e8cc-7c47-aee2-0bd80051ed4b,2024-08-24T15:00:00Z"
* }
*/

📖 docs

This library was based on the official Prisma documentation, read more about Prisma's native pagination types at: https://www.prisma.io/docs/orm/prisma-client/queries/pagination