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

v1.0.1

Published

A simple and flexible pagination module for Prisma, designed to be used with any Node.js project, including Express, NestJS and Next.js.

Downloads

279

Readme

Prisma Paginator

prisma-paginator A simple and flexible pagination module for Prisma, designed to be used with any Node.js project, including NestJS and Next.js. It provides two methods for paginating Prisma queries: one as a method in a class that extends PrismaClientPaginated, and another as a standalone paginate function.

Installation

Install the module via npm:

$ npm install prisma-paginator

Usage

1. PrismaClientPaginated Class

The PrismaClientPaginated class extends PrismaClient and adds a paginate method. This method can be used to paginate queries on any Prisma model.

import { PrismaClient } from "@prisma/client";
import { PrismaClientPaginated } from "prisma-paginator";

//create prima service class
export class PrismaService extends PrismaClientPaginated {
  constructor() {
    super({ errorFormat: "pretty", datasourceUrl: "DATABASE_URL" });
  }
  //Other methods
}

//Can be added via the HTTP requests (body or query)
const pageOption: PageOption = {
  page: 1,
  size: 10,
  sort: ["name=asc"],
  filter: ["isVerified==true", "country==FR"],
};

async function getPaginatedUsers(pageOption) {
  const prismaService = new PrismaService();

  const prismaParams: prismaParams = {
    where: { isAdmin: false },
  };

  //use prisma service
  const paginatedUsers = await prismaService.paginate(
    "user",
    pageOption,
    prismaParams
  );
  console.log(paginatedUsers);
}

getPaginatedUsers();

2. paginate Function

The paginate function provides the same pagination functionality but can be used independently of the PrismaClientPaginated class. It requires a PrismaClient instance as the first parameter.

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

const prisma = new PrismaClient({
  errorFormat: "pretty",
  datasourceUrl: "DATABASE_URL",
});

const pageOption: PageOption = {
  page: 1,
  size: 10,
  sort: ["name=desc"],
  nestedFilter: ["address.city==Bolingo"],
  route: "/users",
};

async function getPaginatedUsers(pageOption) {
  const paginatedUsers = await paginate(prisma, "user", pageOption);
  console.log(paginatedUsers);
}

getPaginatedUsers();

API

PrismaClientPaginated.paginate

Parameters

  • model (string): The name of the Prisma model to paginate.
  • pageOption (PageOption): Options for pagination.
  • prismaParams (PrismaParams, optional): Additional Prisma query parameters.

Returns

  • Promise<Page>: A promise that resolves to a paginated result.

paginate

Parameters

  • prisma (PrismaClient): An instance of PrismaClient.
  • model (string): The name of the Prisma model to paginate.
  • pageOption (PageOption): Options for pagination.
  • prismaParams (PrismaParams, optional): Additional Prisma query parameters.

Returns

  • Promise<Page>: A promise that resolves to a paginated result.

Types

PageOption

interface PageOption {
  page?: number;
  size?: number;
  filter?: string[];
  nestedFilter?: string[];
  sort?: string[];
  route?: string;
}

PrismaParams

interface PrismaParams {
  sort?: unknown;
  where?: unknown;
  select?: unknown;
  include?: unknown;
}

Page

interface Page<T> {
  content: T[];
  metaData: {
    page: number;
    size: number;
    totalPages: number;
    sort?: unknown[];
  };
  links?: {
    first: string;
    prev: string;
    next: string;
    last: string;
  };
}

License

This project is licensed under the MIT License.