Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prisma-pagination

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prisma-pagination

Helper library for [Prisma](https://www.prisma.io/docs/concepts/components/prisma-client/pagination) offset pagination with Typescript support.

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.6K
decreased by-15.52%
Maintainers
1
Weekly downloads
 
Created
Source

Prisma Pagination

Helper library for Prisma offset pagination with Typescript support.

Version License

Installation

npm i prisma-pagination

Usage

import { createPaginator } from 'prisma-pagination'
import { User, Prisma } from '@prisma/client'

// ...

app.get('/', async ({ prisma }, res) => {
  const paginate = createPaginator({ page: 2 })
  
  // You can pass generic types: <Model> and <Model>FindManyArgs
  // to have args and result typed
  
  const result = await paginate<User, Prisma.UserFindManyArgs>(
    prisma.user,
    {
      where: {
        name: {
          contains: 'Alice'
        }
      }
      orderBy: {
        id: 'desc',
      }
    }
  })

  /* Result structure:
  {
    data: User[]
    meta: {
      total: number
      lastPage: number
      currentPage: number
      perPage: number
      prev: number | null
      next: number | null
    }
  }
  */
})

If you use Express-like framework, initializing createPaginator could be moved to the middleware:

import { createPaginator } from 'prisma-pagination'

export const pagination = (req, res, next) => {
  const page = Number(req.query.page) || 1

  req.paginate = createPaginator({ page })
}

so then it can be used in any route handler:

app.get('/', async ({ paginate, prisma }, res) => {
  return await paginate(prisma.user)
})

To have proper Typescript support you can extend Request interface like so:

import { PaginateFunction } from 'prisma-pagination'

declare global {
  namespace Express {
    export interface Request {
      paginate: PaginateFunction
    }
  }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Keywords

FAQs

Package last updated on 24 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc