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.32 kB
JavaScript
import {
a as x,
d as g,
e as I,
f as A,
g as h,
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 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 [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 B = m.type === "binary" ? h({
maxLength: l,
maxRetries: c
}) : E(
{
...m,
maxLength: l,
maxRetries: c
},
w
), u = async (e, n, s, o, a = F) => {
if (!n) {
let _ = 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, _?.[i] ?? null);
}
let y = 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 y.length < 1 ? (
// Return undefined if cursor not found
void 0
) : (
// Return the indices in the appropriate order based on direction
o(y[0][i], y[1]?.[i] ?? null)
);
}, O = (e, n, s) => u(e, n, "asc", (o, a) => [o, a], s), M = (e, n, s) => u(e, n, "desc", (o, a) => [a, o], s), T = {
...B,
// Include all methods from the base fraci helper
isIndexConflictError: (e) => C(e, P, i),
indicesForAfter: O,
indicesForBefore: M,
indicesForFirst: (e, n) => O(e, null, n),
indicesForLast: (e, n) => M(e, null, n)
};
p.set(`${d}\0${i}`, 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