fraci
Version:
Fractional indexing that's robust, performant, and secure, with first-class support for Drizzle ORM and Prisma ORM.
130 lines (126 loc) • 3.08 kB
JavaScript
// src/drizzle/runtime-sync.ts
import { and as m, sql as R } from "drizzle-orm";
// src/drizzle/common.ts
import { asc as B, desc as P, eq as O, gte as q, isNull as w, lte as L, sql as h } from "drizzle-orm";
function c(r, i) {
return i != null ? O(r, i) : i === null ? (
// Use `isNull` if value is `null`
w(r)
) : (
// SECURITY: Always return `FALSE` if value is `undefined`, meaning it's missing
h`FALSE`
);
}
var p = [
[
B,
q,
(r, i) => [r, i]
],
[
P,
L,
(r, i) => [i, r]
]
];
// src/drizzle/runtime-sync.ts
function b(r, {
group: i,
cursor: e,
column: o,
table: l
}, D, s, d) {
let [F, C, f] = p[Number(d)], a = { v: R`${o}` }, z = Object.entries(i).map(
([n, u]) => c(u, D[n])
);
if (!s) {
let n = r.select(a).from(l).where(m(...z)).limit(1).orderBy(F(o)).all();
return f(null, n[0]?.v ?? null);
}
let g = m(
...z,
...Object.entries(e).map(
([n, u]) => c(u, s[n])
// Use equity to safely handle null/undefined
)
), y = r.select(a).from(l).where(g).limit(1), t = r.select(a).from(l).where(
m(
...z,
// Stay within the same group
C(o, y)
// Use gte/lte based on direction
)
).limit(2).orderBy(F(o)).all();
return t.length < 1 ? void 0 : f(t[0].v, t[1]?.v ?? null);
}
function x(r, i, e, o) {
return b(r, i, e, o, !1);
}
function A(r, i, e, o) {
return b(r, i, e, o, !0);
}
function M(r, i) {
return {
...i.fraci,
indicesForAfter: (e, o) => x(r, i, e, o),
indicesForBefore: (e, o) => A(r, i, e, o),
indicesForFirst: (e) => x(r, i, e, null),
indicesForLast: (e) => A(r, i, e, null)
};
}
// src/drizzle/runtime.ts
import { and as I, sql as T } from "drizzle-orm";
async function j(r, {
group: i,
cursor: e,
column: o,
table: l
}, D, s, d) {
let [F, C, f] = p[Number(d)], a = { v: T`${o}` }, z = Object.entries(i).map(
([n, u]) => c(u, D[n])
);
if (!s) {
let n = await r.select(a).from(l).where(I(...z)).limit(1).orderBy(F(o));
return f(null, n[0]?.v ?? null);
}
let g = I(
...z,
...Object.entries(e).map(
([n, u]) => c(u, s[n])
// Use equity to safely handle null/undefined
)
), y = r.select(a).from(l).where(g).limit(1), t = await r.select(a).from(l).where(
I(
...z,
// Stay within the same group
C(o, y)
// Use gte/lte based on direction
)
).limit(2).orderBy(F(o));
return t.length < 1 ? void 0 : f(t[0].v, t[1]?.v ?? null);
}
function S(r, i, e, o) {
return j(r, i, e, o, !1);
}
function G(r, i, e, o) {
return j(r, i, e, o, !0);
}
function J(r, i) {
return {
...i.fraci,
indicesForAfter: (e, o) => S(r, i, e, o),
indicesForBefore: (e, o) => G(r, i, e, o),
indicesForFirst: (e) => S(r, i, e, null),
indicesForLast: (e) => G(r, i, e, null)
};
}
// src/drizzle/schema.ts
function U(r, i, e, o, l) {
return { fraci: r, table: i, column: e, group: o, cursor: l };
}
export {
U as defineDrizzleFraci,
J as drizzleFraci,
M as drizzleFraciSync
};
//# sourceMappingURL=drizzle.js.map