Skip to content

Commit

Permalink
Create page with next/prev url
Browse files Browse the repository at this point in the history
  • Loading branch information
zoriya committed Jan 7, 2025
1 parent 8e466ef commit e70f224
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
34 changes: 16 additions & 18 deletions api/src/controllers/movies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
Genre,
isUuid,
keysetPaginate,
Page,
processLanguages,
type Page,
createPage,
} from "~/models/utils";

Expand Down Expand Up @@ -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,
},
],
},
Expand Down Expand Up @@ -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.",
},
],
},
},
},
);
20 changes: 17 additions & 3 deletions api/src/models/utils/keyset-paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
);

Expand All @@ -36,12 +40,22 @@ 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]));
}

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");
};
25 changes: 23 additions & 2 deletions api/src/models/utils/page.ts
Original file line number Diff line number Diff line change
@@ -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 };
};

0 comments on commit e70f224

Please sign in to comment.