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.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
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'

// ...

const paginate = createPaginator({ perPage: 20 })

app.get('/', async ({ query, prisma }, res) => {
  
  // Generic types can be passed to "paginate",
  // so args and result will be typed and autocompleted :)
  const result = await paginate<User, Prisma.UserFindManyArgs>(
    prisma.user,
    {
      where: {
        name: {
          contains: 'Alice'
        }
      }
      orderBy: {
        id: 'desc',
      }
    }, 
    { page: query.page }
  })

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

For Express-like frameworks, page and perPage can be initialized in the middleware:

import { createPaginator } from 'prisma-pagination'

const pagination = (req, res, next) => {
  req.paginate = createPaginator({ page: req.query.page, perPage: 20 })

  next()
}

app.use(pagination)

so there is no need to pass page value in each handler:

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

To have proper Typescript support, Request interface can be extended this way:

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 12 Dec 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