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-paginate-plugin

v0.0.2

Published

An external pagination plugin for prisma ORM

Downloads

7

Readme

Prisma Pagination Plugin

Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. API Reference
  5. Examples
  6. Contributing
  7. License

Introduction

The Prisma Pagination Plugin is a powerful and flexible solution for implementing pagination in your Prisma-based Node.js applications. It provides an easy-to-use interface for paginating your database queries, handling search functionality, and managing complex filtering scenarios.

Key Features:

  • Easy integration with existing Prisma models
  • Support for offset-based pagination
  • Built-in search functionality across multiple fields
  • Flexible filtering options
  • Automatic date-based sorting (can be disabled)
  • TypeScript support for enhanced type safety

Installation

To install the Prisma Pagination Plugin, run the following command in your project directory:

npm install prisma-pagination-plugin

Or if you're using Yarn:

yarn add prisma-pagination-plugin

Usage

Here's a basic example of how to use the Prisma Pagination Plugin:

import { prismaPaginate } from 'prisma-pagination-plugin';
import { PrismaClient,Prisma,User } from '@prisma/client';

const prisma = new PrismaClient();

async function getUsers() {
  const result = await prismaPaginate<User,Prisma.UserFindManyArgs>({
    model: prisma.user,
    paginationQuery: {
      limit: 10,
      offset: 0,
      search: 'John'
    },
    searchFields: ['name', 'email'],
    findManyArgs: {
      where: {
        isActive: true
      },
      orderBy: {
        createdAt: 'desc'
      }
    }
  });

  console.log(result);
}

getUsers();

API Reference

prismaPaginate<Model, ModelFindManyArgs>(params: IPaginateParams<Model, ModelFindManyArgs>)

The main function for paginating Prisma queries.

Parameters:

params: An object of type IPaginateParams<Model, ModelFindManyArgs> with the following properties:

  • model: The Prisma model to query (e.g., prisma.user)
  • paginationQuery (optional): An object containing pagination parameters:
    • limit (optional): Number of items per page (default: 10)
    • offset (optional): Number of items to skip (default: 0)
    • search (optional): Search string to filter results
  • searchFields (optional): An array of model fields to search in
  • findManyArgs (optional): Additional arguments to pass to Prisma's findMany method
  • skipDateSort (optional): If true, disables automatic date-based sorting
  • dateSortFieldName (optional): The field name to use for date sorting (default: 'created_at')

Returns:

An object with the following properties:

  • count: Total number of items matching the query
  • limit: Number of items per page
  • offset: Number of items skipped
  • docs: Array of items for the current page

Examples

Basic Pagination

const result = await prismaPaginate({
  model: prisma.user,
  paginationQuery: {
    limit: 20,
    offset: 40
  }
});

Pagination with Search

const result = await prismaPaginate({
  model: prisma.product,
  paginationQuery: {
    limit: 15,
    offset: 0,
    search: 'laptop'
  },
  searchFields: ['name', 'description', 'category']
});

Pagination with Complex Filtering

const result = await prismaPaginate({
  model: prisma.order,
  paginationQuery: {
    limit: 10,
    offset: 20
  },
  findManyArgs: {
    where: {
      status: 'COMPLETED',
      totalAmount: {
        gte: 100
      }
    },
    orderBy: {
      completedAt: 'desc'
    }
  }
});

Date Sorting

By default, the plugin applies a descending sort on the created_at field. This ensures that the most recent items appear first in the paginated results. You can modify this behavior in two ways:

  1. Changing the sort field: If your model uses a different field name for the creation date, you can specify it using the dateSortFieldName parameter:

    const result = await prismaPaginate({
      model: prisma.user,
      dateSortFieldName: 'createdAt'
    });
  2. Disabling automatic date sorting: If you want to disable the automatic date sorting entirely, you can set the skipDateSort parameter to true:

    const result = await prismaPaginate({
      model: prisma.user,
      skipDateSort: true
    });

    When skipDateSort is true, you can specify your own sorting logic in the findManyArgs.orderBy parameter:

    const result = await prismaPaginate({
      model: prisma.user,
      skipDateSort: true,
      findManyArgs: {
        orderBy: {
          lastName: 'asc'
        }
      }
    });

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.