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.62 kB
import { a as x, d as g, e as I, f as A, g as h, 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 Q = "P2002"; function C(r, l, c) { return r instanceof Error && r.name === "PrismaClientKnownRequestError" && r.code === Q && // P2002 is the Prisma code for unique constraint violations r.meta?.modelName === l && // 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 N = "fraci"; // src/prisma/extension.ts function G({ fields: r, maxLength: l = g, maxRetries: c = I }) { return b.defineExtension((F) => { let w = A(), p = /* @__PURE__ */ new Map(); for (let [t, m] of Object.entries(r)) { 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 B = m.type === "binary" ? h({ maxLength: l, maxRetries: c }) : E( { ...m, maxLength: l, maxRetries: c }, w ), u = async (i, n, o, a, d = F) => { if (!n) { let _ = await d[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, _?.[e] ?? null); } let y = await d[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 y.length < 1 ? ( // Return undefined if cursor not found void 0 ) : ( // Return the indices in the appropriate order based on direction a(y[0][e], y[1]?.[e] ?? null) ); }, O = (i, n, o) => u(i, n, "asc", (a, d) => [a, d], o), M = (i, n, o) => u(i, n, "desc", (a, d) => [d, a], o), T = { ...B, // Include all methods from the base fraci helper isIndexConflictError: (i) => C(i, P, e), indicesForAfter: O, indicesForBefore: M, indicesForFirst: (i, n) => O(i, null, n), indicesForLast: (i, n) => M(i, null, n) }; p.set(`${s}\0${e}`, T); } let f = /* @__PURE__ */ Object.create(null); for (let t of Object.keys(F)) t.startsWith("$") || t.startsWith("_") || (f[t] = { // This method retrieves the appropriate helper for the specified field fraci(m) { return p.get(`${t}\0${m}`); } }); return F.$extends({ name: N, model: f }); }); } // src/prisma/schema.ts function $(r) { return r; } export { $ as definePrismaFraci, G as prismaFraci }; //# sourceMappingURL=prisma.js.map