Create page with next/prev url

This commit is contained in:
Zoe Roux 2025-01-06 22:05:13 +01:00
parent 50002907e3
commit 879d2959d5
No known key found for this signature in database
3 changed files with 56 additions and 23 deletions

View File

@ -14,8 +14,8 @@ import {
Genre,
isUuid,
keysetPaginate,
Page,
processLanguages,
type Page,
createPage,
} from "~/models/utils";
@ -147,7 +147,6 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
{
status: 422,
message: "Accept-Language header could not be satisfied.",
details: undefined,
},
],
},
@ -222,21 +221,20 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
`,
}),
}),
// response: {
// 200: Page(Movie, {
// description: "Paginated list of movies that match filters.",
// }),
// 422: {
// ...KError,
// description: "Invalid query parameters.",
// examples: [
// {
// status: 422,
// message: "Accept-Language header could not be satisfied.",
// details: undefined,
// },
// ],
// },
// },
response: {
200: Page(Movie, {
description: "Paginated list of movies that match filters.",
}),
422: {
...KError,
description: "Invalid query parameters.",
examples: [
{
status: 422,
message: "Accept-Language header could not be satisfied.",
},
],
},
},
},
);

View File

@ -3,6 +3,10 @@ import { eq, or, type Column, and, gt, lt } from "drizzle-orm";
type Table<Name extends string> = Record<Name, Column>;
type After = Record<string, string | number | boolean | undefined> & {
reverse?: boolean;
};
// Create a filter (where) expression on the query to skip everything before/after the referenceID.
// The generalized expression for this in pseudocode is:
// (x > a) OR
@ -27,7 +31,7 @@ export const keysetPaginate = <
sort: Sort<T, Remap>;
}) => {
if (!after) return undefined;
const cursor: Record<string, string | number> = JSON.parse(
const { reverse, ...cursor }: After = JSON.parse(
Buffer.from(after, "base64").toString("utf-8"),
);
@ -36,7 +40,7 @@ export const keysetPaginate = <
let where = undefined;
let previous = undefined;
for (const by of [...sort, { key: "pk" as const, desc: false }]) {
const cmp = by.desc ? lt : gt;
const cmp = by.desc !== reverse ? lt : gt;
where = or(where, and(previous, cmp(table[by.key], cursor[by.key])));
previous = and(previous, eq(table[by.key], cursor[by.key]));
}
@ -44,4 +48,14 @@ export const keysetPaginate = <
return where;
};
export const generateAfter = (
cursor: any,
sort: Sort<any, any>,
reverse?: boolean,
) => {
const ret: After = { reverse };
for (const by of sort) {
ret[by.key] = cursor[by.key];
}
return Buffer.from(JSON.stringify(ret), "utf-8").toString("base64");
};

View File

@ -1,13 +1,34 @@
import type { ObjectOptions } from "@sinclair/typebox";
import { t, type TSchema } from "elysia";
import type { Sort } from "./sort";
import { generateAfter } from "./keyset-paginate";
export const Page = <T extends TSchema>(schema: T, options?: ObjectOptions) =>
t.Object(
{
items: t.Array(schema),
this: t.String({ format: "uri" }),
prev: t.String({ format: "uri" }),
next: t.String({ format: "uri" }),
prev: t.Nullable(t.String({ format: "uri" })),
next: t.Nullable(t.String({ format: "uri" })),
},
options,
);
export const createPage = <T>(
items: T[],
{ url, sort }: { url: string; sort: Sort<any, any> },
) => {
let prev: string | null = null;
let next: string | null = null;
const uri = new URL(url);
if (uri.searchParams.has("after")) {
uri.searchParams.set("after", generateAfter(items[0], sort, true));
prev = uri.toString();
}
if (items.length) {
uri.searchParams.set("after", generateAfter(items[items.length - 1], sort));
next = uri.toString();
}
return { items, this: url, prev, next };
};