Skip to content

Commit

Permalink
Make pagination optional in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h committed Apr 30, 2024
1 parent c258d6a commit e7d712c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
36 changes: 23 additions & 13 deletions src/fetch/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { expect, test } from "bun:test";
import { addMetadata } from "./utils.js";
import { Query } from "../clickhouse/makeQuery.js";

test("addMetadata pagination", () => {
const limit = 5;
const mock_query_reponse: Query<any> = {
meta: [],
data: Array(limit),
rows: limit,
rows_before_limit_at_least: 5*limit, // Simulate query with more total results than the query limit making pagination relevant
statistics: {
elapsed: 0,
rows_read: 0,
bytes_read: 0,
}
};
const limit = 5;
const mock_query_reponse: Query<any> = {
meta: [],
data: Array(limit),
rows: limit,
rows_before_limit_at_least: 5*limit, // Simulate query with more total results than the query limit making pagination relevant
statistics: {
elapsed: 0,
rows_read: 0,
bytes_read: 0,
}
};

test("addMetadata pagination", () => {
const first_page = addMetadata(mock_query_reponse, limit, 1);
expect(first_page.meta.next_page).toBe(2);
expect(first_page.meta.previous_page).toBe(1); // Previous page should be set to 1 on first page
Expand All @@ -35,11 +35,21 @@ test("addMetadata pagination", () => {
expect(even_page.meta.total_results).toBe(5*limit);

const last_page = addMetadata(mock_query_reponse, limit, 5);
// @ts-ignore
expect(last_page.meta.next_page).toBe(last_page.meta.total_pages); // Next page should be capped to total_pages on last page
expect(last_page.meta.previous_page).toBe(4);
expect(last_page.meta.total_pages).toBe(5);
expect(last_page.meta.total_results).toBe(5*limit);

// TODO: Expect error message on beyond last page
// const beyond_last_page = addMetadata(mock_query_reponse.data, mock_query_reponse.rows_before_limit_at_least, limit, 6);
});

test("addMetadata no pagination", () => {
const no_pagination = addMetadata(mock_query_reponse);

expect(no_pagination.meta.next_page).toBeUndefined();
expect(no_pagination.meta.previous_page).toBeUndefined();
expect(no_pagination.meta.total_pages).toBeUndefined();
expect(no_pagination.meta.total_results).toBeUndefined();
});
28 changes: 18 additions & 10 deletions src/fetch/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ export function toJSON(data: any, status: number = 200) {
return new Response(JSON.stringify(data), { status, headers: { "Content-Type": "application/json" } });
}

export function addMetadata(response: Query<any>, req_limit: number, req_page: number) {
export function addMetadata(response: Query<any>, req_limit?: number, req_page?: number) {
// TODO: Catch page number greater than total_pages and return error
return {
data: response.data,
meta: {
statistics: response.statistics,
"next_page": (req_page * req_limit >= response.rows_before_limit_at_least) ? req_page : req_page + 1,
"previous_page": (req_page <= 1) ? req_page : req_page - 1,
"total_pages": Math.ceil( response.rows_before_limit_at_least / req_limit),
"total_results": response.rows_before_limit_at_least
if (typeof(req_limit) !== 'undefined' && typeof(req_page) !== 'undefined')
return {
data: response.data,
meta: {
statistics: response.statistics,
"next_page": (req_page * req_limit >= response.rows_before_limit_at_least) ? req_page : req_page + 1,
"previous_page": (req_page <= 1) ? req_page : req_page - 1,
"total_pages": Math.ceil( response.rows_before_limit_at_least / req_limit),
"total_results": response.rows_before_limit_at_least
}
}
else
return {
data: response.data,
meta: {
statistics: response.statistics,
}
}
}
}

0 comments on commit e7d712c

Please sign in to comment.