deptyped / prisma-extension-pagination

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

Why does the cursor id have to be an integrer type? #22

Closed doob9p closed 9 months ago

doob9p commented 9 months ago
 const { limit, after, before, getCursor, parseCursor } = Object.assign(Object.assign({ 
                // @ts-expect-error actual fields of the model are not known
                getCursor({ id }) {
                    // if (typeof id !== "number") {
                    //     throw new Error("Unable to serialize cursor");
                    // }
                    return id
                },
                parseCursor(cursor) {
                    // const id = parseInt(cursor, 10);
                    // if (Number.isNaN(id)) {
                    //     throw new Error("Unable to parse cursor");
                    // }
                    return {
                        id: cursor,
                    };
                } }, globalOptions === null || globalOptions === void 0 ? void 0 : globalOptions.cursor), options);

extension.js

Because of the annotation in the "extension.js" file, the integrer type cursor id does not work.

I am using mongodb and the type of Id I am using is objectId

deptyped commented 9 months ago

Hi. The default implementation for numeric incremental identifiers, if it does not meet your requirements, you can provide your own implementation in this way:

import { PrismaClient } from "@prisma/client";
import { pagination } from "prisma-extension-pagination";

const prisma = new PrismaClient().$extends(
  pagination({
    cursor: {
      getCursor({ id }) {
        return id;
      },
      parseCursor(cursor) {
        return {
          id: cursor,
        };
      },
    },
  }),
);