Skip to content

Commit

Permalink
Remove page's prev & weird reverse handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zoriya committed Jan 10, 2025
1 parent 371d914 commit 6e293ef
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
21 changes: 5 additions & 16 deletions api/src/models/utils/keyset-paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { eq, or, type Column, and, gt, lt } from "drizzle-orm";

type Table<Name extends string> = Record<Name, Column>;

type After = [boolean, ...[string | number | boolean | undefined]];
type After = (string | number | boolean | undefined)[];

// Create a filter (where) expression on the query to skip everything before/after the referenceID.
// The generalized expression for this in pseudocode is:
Expand All @@ -29,7 +29,7 @@ export const keysetPaginate = <
sort: Sort<T, Remap>;
}) => {
if (!after) return undefined;
const [reverse, ...cursor]: After = JSON.parse(
const cursor: After = JSON.parse(
Buffer.from(after, "base64").toString("utf-8"),
);

Expand All @@ -40,26 +40,15 @@ export const keysetPaginate = <
let where = undefined;
let previous = undefined;
for (const [i, by] of [...sort, pkSort].entries()) {
const cmp = by.desc !== reverse ? lt : gt;
const cmp = by.desc ? lt : gt;
where = or(where, and(previous, cmp(table[by.key], cursor[i])));
previous = and(previous, eq(table[by.key], cursor[i]));
}

return where;
};

export const generateAfter = (
cursor: any,
sort: Sort<any, any>,
reverse?: boolean,
) => {
const ret = [
reverse ?? false,
...sort.map((by) => cursor[by.key]),
cursor.pk,
];
export const generateAfter = (cursor: any, sort: Sort<any, any>) => {
const ret = [...sort.map((by) => cursor[by.key]), cursor.pk];
return Buffer.from(JSON.stringify(ret), "utf-8").toString("base64url");
};

const reverseStart = Buffer.from("[true,", "utf-8").toString("base64url");
export const isReverse = (x: string) => x.startsWith(reverseStart);
24 changes: 4 additions & 20 deletions api/src/models/utils/page.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { ObjectOptions } from "@sinclair/typebox";
import { t, type TSchema } from "elysia";
import type { Sort } from "./sort";
import { generateAfter, isReverse } from "./keyset-paginate";
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.Nullable(t.String({ format: "uri" })),
next: t.Nullable(t.String({ format: "uri" })),
},
options,
Expand All @@ -18,27 +17,12 @@ export const createPage = <T>(
items: T[],
{ url, sort, limit }: { url: string; sort: Sort<any, any>; limit: number },
) => {
let prev: string | null = null;
let next: string | null = null;

const uri = new URL(url);
const after = uri.searchParams.get("after");
const reverse = after && isReverse(after) ? 1 : 0;

const has = [
// prev
items.length > 0 && after,
// next
items.length === limit && limit > 0,
];

if (has[0 + reverse]) {
uri.searchParams.set("after", generateAfter(items[0], sort, true));
prev = uri.toString();
}
if (has[1 - reverse]) {
if (items.length === limit && limit > 0) {
const uri = new URL(url);
uri.searchParams.set("after", generateAfter(items[items.length - 1], sort));
next = uri.toString();
}
return { items, this: url, prev, next };
return { items, this: url, next };
};
38 changes: 31 additions & 7 deletions api/tests/movies/get-all-movies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,47 @@ describe("Get all movies", () => {

expectStatus(resp, body).toBe(422);
expect(body).toMatchObject({
details: expect.objectContaining( {
in: "slug eq gt bubble",
}),
details: expect.anything(),
message: "Invalid filter: slug eq gt bubble.",
status: 422,
});
});
it("Limit 1, default sort", async () => {
it("Limit 2, default sort", async () => {
const [resp, body] = await getMovies({
limit: 1,
limit: 2,
langs: "en",
});

expectStatus(resp, body).toBe(200);
expect(body).toMatchObject({
items: [bubble],
this: "",
items: [
expect.objectContaining({ slug: bubble.slug }),
expect.objectContaining({ slug: dune.slug }),
],
this: "http://localhost/movies?limit=2",
// we can't have the exact after since it contains the pk that changes with every tests.
next: expect.stringContaining(
"http://localhost/movies?limit=2&after=WyJkdW5lIiw0",
),
});
});
it("Limit 2, default sort, page 2", async () => {
let [resp, body] = await getMovies({
limit: 2,
langs: "en",
});
expectStatus(resp, body).toBe(200);

resp = await app.handle(new Request(body.next));
body = await resp.json();

expectStatus(resp, body).toBe(200);
expect(body).toMatchObject({
items: [expect.objectContaining({ slug: dune1984.slug })],
this: expect.stringContaining(
"http://localhost/movies?limit=2&after=WyJkdW5lIiw0",
),
next: null,
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions api/tests/movies/seed-movies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("Movie seeding", () => {
expect(existing).toMatchObject({ slug: dune.slug, startAir: dune.airDate });

const [resp, body] = await createMovie({ ...dune, airDate: "2158-12-13" });
expectStatus(resp, body).toBe(200);
expectStatus(resp, body).toBe(201);
expect(body.id).toBeString();
expect(body.slug).toBe("dune-2158");
});
Expand Down Expand Up @@ -224,7 +224,7 @@ describe("Movie seeding", () => {
});

const cleanup = async () => {
await db.delete(shows).where(inArray(shows.slug, [dune.slug]));
await db.delete(shows).where(inArray(shows.slug, [dune.slug, "dune-2158"]));
await db.delete(videos).where(inArray(videos.id, [duneVideo.id]));
};
// cleanup db beforehand to unsure tests are consistent
Expand Down

0 comments on commit 6e293ef

Please sign in to comment.