deptyped / prisma-extension-pagination

Prisma Client extension for pagination
https://npmjs.com/prisma-extension-pagination
MIT License
227 stars 17 forks source link

Custom default options #12

Closed deptyped closed 1 year ago

deptyped commented 1 year ago

Also going to hijack my own issue to ask if it's possible to add config globally. For example, I want to add includePageCount: true as default to all responses, and instead disable it for the few where I may not want it.

Originally posted by @jaaneh in https://github.com/deptyped/prisma-extension-pagination/issues/11#issuecomment-1648167930

deptyped commented 1 year ago

I've published the initial implementation under next tag.

npm i prisma-extension-pagination@next

Documentation is available in next branch.

This version contains a lot of internal changes (and possibly bugs) and breaks backwards compatibility of the installation process for all models (default export is now a function), so I'd like to get some feedback before release.

/cc @jaaneh

jaaneh commented 1 year ago

Oh snap! This is awesome.

I'd like to get some feedback before release.

I'll play around with it tomorrow and let you know if anything comes up!

jaaneh commented 1 year ago

From initial tests this is fantastic. Here's what I'm currently doing:

// lib/prisma.ts
const prisma = new PrismaClient().$extends(
  pagination({
    pages: {
      limit: 100,
      includePageCount: true
    }
  })
)

// jokes.service.ts
export async function getJokes() {
  const [data, meta] = await prisma.<model>.paginate().withPages()
  return { data, meta }
}

Much better especially since we have calls to Prisma where most will need to return pageCount and all will have a default page size of 100.

Will continue to play with it, but from my pov this is exactly what I needed and works fine.