fraci
Version:
Fractional indexing that's robust, performant, and secure, with first-class support for Drizzle ORM and Prisma ORM.
115 lines (110 loc) • 3.33 kB
JavaScript
import {
a as x,
d as g,
e as I,
f as h,
g as A,
h as E
} from "./chunk-H7E2WV2H.js";
// src/prisma/extension.ts
import { Prisma as b } from "@prisma/client/extension.js";
// src/prisma/common.ts
var $ = "P2002";
function N(r, l, c) {
return r instanceof Error && r.name === "PrismaClientKnownRequestError" && r.code === $ && // 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 B = "fraci";
// src/prisma/extension.ts
function G(r, {
fields: l,
maxLength: c = g,
maxRetries: p = I
}) {
return b.defineExtension((F) => {
let w = h(), f = /* @__PURE__ */ new Map();
for (let [t, y] of Object.entries(l)) {
let [d, i] = t.split(".", 2), { modelName: P } = F[d]?.fields?.[i] ?? {};
if (!P)
throw new x(
"INITIALIZATION_FAILED",
`Could not get field information for ${d}.${i}`
);
let _ = y.type === "binary" ? A({
maxLength: c,
maxRetries: p
}) : E(
{
...y,
maxLength: c,
maxRetries: p
},
w
), O = async (e, n, s, o, a = F) => {
if (!n) {
let Q = await a[d].findFirst({
where: e,
// Filter by group conditions
select: { [i]: !0 },
// Only select the fractional index field
orderBy: { [i]: s }
// Order by the fractional index in appropriate direction
});
return o(null, Q?.[i] ?? null);
}
let m = await a[d].findMany({
cursor: n,
// Start from the cursor position
where: e,
// Filter by group conditions
select: { [i]: !0 },
// Only select the fractional index field
orderBy: { [i]: s },
// Order by the fractional index in appropriate direction
take: 2
// Get the cursor item and the adjacent item
});
return m.length < 1 ? (
// Return undefined if cursor not found
void 0
) : (
// Return the indices in the appropriate order based on direction
o(m[0][i], m[1]?.[i] ?? null)
);
}, u = (e, n, s) => O(e, n, "asc", (o, a) => [o, a], s), M = (e, n, s) => O(e, n, "desc", (o, a) => [a, o], s), T = {
..._,
// Include all methods from the base fraci helper
isIndexConflictError: (e) => N(e, P, i),
indicesForAfter: u,
indicesForBefore: M,
indicesForFirst: (e, n) => u(e, null, n),
indicesForLast: (e, n) => M(e, null, n)
};
f.set(`${d}\0${i}`, 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, l) {
return l;
}
export {
S as definePrismaFraci,
G as prismaFraci
};
//# sourceMappingURL=prisma.js.map