import { PrismaClient, Prisma } from "@prisma/client"; import * as PrismaEngines from "@prisma/engines"; import { type pageData } from "./baaPageParsing"; type model = PrismaClient[Uncapitalize]; PrismaEngines.ensureBinariesExist(); function getBeforeParameters( pageData: pageData, fetchArgs: Prisma.Args, ) { const _fetchArgs = Object.assign({}, fetchArgs); return Object.assign(_fetchArgs, { take: pageData.count, orderBy: [ { id: "desc" }, ], where: { AND: [ { id: { _lt: pageData.id, }, }, fetchArgs.where, ], }, }); } function getAfterParameters( pageData: pageData, fetchArgs: Prisma.Args, ) { const _fetchArgs = Object.assign({}, fetchArgs); return Object.assign(_fetchArgs, { take: pageData.count, orderBy: [ { id: "desc" }, ], where: { AND: [ { id: { _gt: pageData.id, }, }, fetchArgs.where, ], }, }); } function getNullParameters( pageData: pageData, fetchArgs: Prisma.Args, ) { const _fetchArgs = Object.assign({}, fetchArgs); return Object.assign(_fetchArgs, { take: pageData.count, orderBy: [ { id: "desc" }, ], }); } export const database = new PrismaClient().$extends({ model: { $allModels: { findPaginated( this: T, pageData: pageData, fetchArgs: Prisma.Exact>, ): Promise> { const context = Prisma.getExtensionContext(this) as any; switch (pageData.type) { case "before": return context.findMany(getBeforeParameters(pageData, fetchArgs)); case "after": return context.findMany(getAfterParameters(pageData, fetchArgs)); case "around": return Promise.all([ context.findMany(getBeforeParameters({ type: "before", id: pageData.id, count: Math.ceil(pageData.count), }, fetchArgs)), context.findMany(getAfterParameters({ type: "after", id: pageData.id, count: Math.floor(pageData.count), }, fetchArgs)), ]).then(rv => rv.flat()) as Promise; case null: return context.findMany(getNullParameters(pageData, fetchArgs)); } }, }, }, });