Rework sort to handle multiples tables

This commit is contained in:
Zoe Roux
2025-03-10 00:16:35 +01:00
parent 039880d812
commit 46833ac06f
8 changed files with 165 additions and 125 deletions
+17 -31
View File
@@ -1,7 +1,5 @@
import { type Column, and, eq, gt, isNull, lt, or, sql } from "drizzle-orm";
import type { NonEmptyArray, Sort } from "./sort";
type Table<Name extends string> = Record<Name, Column>;
import { and, eq, gt, isNull, lt, or, sql } from "drizzle-orm";
import type { Sort } from "./sort";
type After = (string | number | boolean | undefined)[];
@@ -16,37 +14,37 @@ type After = (string | number | boolean | undefined)[];
// (x > a) OR
// (x = a AND y < b) OR
// (x = a AND y = b AND z > c) OR...
export const keysetPaginate = <
const T extends NonEmptyArray<string>,
const Remap extends Partial<Record<T[number], string>>,
>({
table,
export const keysetPaginate = ({
sort,
after,
}: {
table: Table<"pk" | Sort<T, Remap>["sort"][number]["key"]>;
sort: Sort | undefined;
after: string | undefined;
sort: Sort<T, Remap> | undefined;
}) => {
if (!after || !sort) return undefined;
const cursor: After = JSON.parse(
Buffer.from(after, "base64").toString("utf-8"),
);
const pkSort = { key: "pk" as const, desc: false };
const pkSort = {
sql: sort.tablePk,
isNullable: false,
accessor: (x: any) => x.pk,
desc: false,
};
if (sort.random) {
return or(
gt(
sql`md5(${sort.random.seed} || ${table[pkSort.key]})`,
sql`md5(${sort.random.seed} || ${sort.tablePk})`,
sql`md5(${sort.random.seed} || ${cursor[0]})`,
),
and(
eq(
sql`md5(${sort.random.seed} || ${table[pkSort.key]})`,
sql`md5(${sort.random.seed} || ${sort.tablePk})`,
sql`md5(${sort.random.seed} || ${cursor[0]})`,
),
gt(table[pkSort.key], cursor[0]),
gt(sort.tablePk, cursor[0]),
),
);
}
@@ -62,31 +60,19 @@ export const keysetPaginate = <
where,
and(
previous,
or(
cmp(table[by.key], cursor[i]),
!table[by.key].notNull ? isNull(table[by.key]) : undefined,
),
or(cmp(by.sql, cursor[i]), by.isNullable ? isNull(by.sql) : undefined),
),
);
previous = and(
previous,
cursor[i] === null ? isNull(table[by.key]) : eq(table[by.key], cursor[i]),
cursor[i] === null ? isNull(by.sql) : eq(by.sql, cursor[i]),
);
}
return where;
};
export const generateAfter = <
const ST extends NonEmptyArray<string>,
const Remap extends Partial<Record<ST[number], string>> = never,
>(
cursor: any,
sort: Sort<ST, Remap>,
) => {
const ret = [
...sort.sort.map((by) => cursor[by.remmapedKey ?? by.key]),
cursor.pk,
];
export const generateAfter = (cursor: any, sort: Sort) => {
const ret = [...sort.sort.map((by) => by.accessor(cursor)), cursor.pk];
return Buffer.from(JSON.stringify(ret), "utf-8").toString("base64url");
};
+3 -7
View File
@@ -1,7 +1,7 @@
import type { ObjectOptions } from "@sinclair/typebox";
import { type TSchema, t } from "elysia";
import { generateAfter } from "./keyset-paginate";
import type { NonEmptyArray, Sort } from "./sort";
import type { Sort } from "./sort";
export const Page = <T extends TSchema>(schema: T, options?: ObjectOptions) =>
t.Object(
@@ -16,13 +16,9 @@ export const Page = <T extends TSchema>(schema: T, options?: ObjectOptions) =>
},
);
export const createPage = <
T,
const ST extends NonEmptyArray<string>,
const Remap extends Partial<Record<ST[number], string>> = never,
>(
export const createPage = <T>(
items: T[],
{ url, sort, limit }: { url: string; sort: Sort<ST, Remap>; limit: number },
{ url, sort, limit }: { url: string; sort: Sort; limit: number },
) => {
let next: string | null = null;
const uri = new URL(url);
+50 -37
View File
@@ -1,34 +1,40 @@
import { sql } from "drizzle-orm";
import { type SQL, type SQLWrapper, sql } from "drizzle-orm";
import type { PgColumn } from "drizzle-orm/pg-core";
import { t } from "elysia";
export type Sort<
T extends string[],
Remap extends Partial<Record<T[number], string>>,
> = {
export type Sort = {
tablePk: SQLWrapper;
sort: {
key: Exclude<T[number], keyof Remap> | NonNullable<Remap[keyof Remap]>;
remmapedKey?: keyof Remap;
sql: SQLWrapper;
isNullable: boolean;
accessor: (cursor: any) => unknown;
desc: boolean;
}[];
random?: { seed: number };
};
export type NonEmptyArray<T> = [T, ...T[]];
export const Sort = <
const T extends NonEmptyArray<string>,
const Remap extends Partial<Record<T[number], string>> = never,
>(
values: T,
export const Sort = (
values: Record<
string,
| PgColumn
| {
sql: PgColumn;
accessor: (cursor: any) => unknown;
}
| {
sql: SQLWrapper;
isNullable: boolean;
accessor: (cursor: any) => unknown;
}
>,
{
description = "How to sort the query",
default: def,
remap,
tablePk,
}: {
default?: T[number][];
default?: (keyof typeof values)[];
tablePk: SQLWrapper;
description?: string;
remap?: Remap;
},
) =>
t
@@ -36,10 +42,10 @@ export const Sort = <
t.Array(
t.Union([
t.UnionEnum([
...values,
...values.map((x: T[number]) => `-${x}` as const),
...Object.keys(values),
...Object.keys(values).map((x) => `-${x}`),
"random",
]),
] as any),
t.TemplateLiteral("random:${number}"),
]),
{
@@ -48,21 +54,36 @@ export const Sort = <
},
),
)
.Decode((sort): Sort<T, Remap> => {
.Decode((sort: string[]): Sort => {
const random = sort.find((x) => x.startsWith("random"));
if (random) {
const seed = random.includes(":")
? Number.parseInt(random.substring("random:".length))
: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
return { random: { seed }, sort: [] };
return { tablePk, random: { seed }, sort: [] };
}
return {
tablePk,
sort: sort.map((x) => {
const desc = x[0] === "-";
const key = (desc ? x.substring(1) : x) as T[number];
if (remap && key in remap)
return { key: remap[key]!, remmapedKey: key, desc };
return { key: key as Exclude<typeof key, keyof Remap>, desc };
const key = desc ? x.substring(1) : x;
if ("getSQL" in values[key]) {
return {
sql: values[key],
isNullable: !values[key].notNull,
accessor: (x) => x[key],
desc,
};
}
return {
sql: values[key].sql,
isNullable:
"isNullable" in values[key]
? values[key].isNullable
: !values[key].sql.notNull,
accessor: values[key].accessor,
desc,
};
}),
};
})
@@ -70,20 +91,12 @@ export const Sort = <
throw new Error("Encode not supported for sort");
});
type Table<Name extends string> = Record<Name, PgColumn>;
export const sortToSql = <
T extends string[],
Remap extends Partial<Record<T[number], string>>,
>(
sort: Sort<T, Remap> | undefined,
table: Table<Sort<T, Remap>["sort"][number]["key"] | "pk">,
) => {
export const sortToSql = (sort: Sort | undefined) => {
if (!sort) return [];
if (sort.random) {
return [sql`md5(${sort.random.seed} || ${table.pk})`];
return [sql`md5(${sort.random.seed} || ${sort.tablePk})`];
}
return sort.sort.map((x) =>
x.desc ? sql`${table[x.key]} desc nulls last` : table[x.key],
x.desc ? sql`${x.sql} desc nulls last` : (x.sql as SQL),
);
};