UNPKG

fraci

Version:

Fractional indexing that's robust, performant, and secure, with first-class support for Drizzle ORM and Prisma ORM.

119 lines (114 loc) 3.63 kB
import { a as x, d as g, e as I, f as h, g as A, h as E } from "./chunk-VEBTP4GL.js"; // src/prisma/extension.ts import { Prisma as b } from "@prisma/client/extension.js"; // src/prisma/common.ts var $ = "P2002"; function N(r, d, c) { return r instanceof Error && r.name === "PrismaClientKnownRequestError" && r.code === $ && // P2002 is the Prisma code for unique constraint violations r.meta?.modelName === d && // Check if the error is for the correct model Array.isArray(r.meta?.target) && // Check if the target field is specified r.meta.target.includes(c); } // src/prisma/constants.ts var B = "fraci"; // src/prisma/extension.ts function G(r, { fields: d, maxLength: c = g, maxRetries: m = I }) { return b.defineExtension((F) => { let w = h(), f = /* @__PURE__ */ new Map(); for (let [t, y] of Object.entries(d)) { let [s, e] = t.split(".", 2), { modelName: P } = F[s]?.fields?.[e] ?? {}; if (!P) throw console.error(`FraciError: [INITIALIZATION_FAILED] Could not get field information for ${s}.${e}. Make sure that - The model and field names are correct and exist in the Prisma schema - The Prisma client is generated with the correct schema - The Prisma version is compatible with the extension`), new x( "INITIALIZATION_FAILED", `Could not get field information for ${s}.${e}` ); let _ = y.type === "binary" ? A({ maxLength: c, maxRetries: m }) : E( { ...y, maxLength: c, maxRetries: m }, w ), O = async (i, n, o, a, l = F) => { if (!n) { let Q = await l[s].findFirst({ where: i, // Filter by group conditions select: { [e]: !0 }, // Only select the fractional index field orderBy: { [e]: o } // Order by the fractional index in appropriate direction }); return a(null, Q?.[e] ?? null); } let p = await l[s].findMany({ cursor: n, // Start from the cursor position where: i, // Filter by group conditions select: { [e]: !0 }, // Only select the fractional index field orderBy: { [e]: o }, // Order by the fractional index in appropriate direction take: 2 // Get the cursor item and the adjacent item }); return p.length < 1 ? ( // Return undefined if cursor not found void 0 ) : ( // Return the indices in the appropriate order based on direction a(p[0][e], p[1]?.[e] ?? null) ); }, u = (i, n, o) => O(i, n, "asc", (a, l) => [a, l], o), M = (i, n, o) => O(i, n, "desc", (a, l) => [l, a], o), T = { ..._, // Include all methods from the base fraci helper isIndexConflictError: (i) => N(i, P, e), indicesForAfter: u, indicesForBefore: M, indicesForFirst: (i, n) => u(i, null, n), indicesForLast: (i, n) => M(i, null, n) }; f.set(`${s}\0${e}`, T); } let C = /* @__PURE__ */ Object.create(null); for (let t of Object.keys(F)) t.startsWith("$") || t.startsWith("_") || (C[t] = { // This method retrieves the appropriate helper for the specified field fraci(y) { return f.get(`${t}\0${y}`); } }); return F.$extends({ name: B, model: C }); }); } // src/prisma/schema.ts function S(r, d) { return d; } export { S as definePrismaFraci, G as prismaFraci }; //# sourceMappingURL=prisma.js.map