From a1d8e1ac972839da0fd04afb01dec2f889262b4f Mon Sep 17 00:00:00 2001 From: Natty Date: Mon, 1 Jan 2024 20:21:22 +0100 Subject: [PATCH] Use pagination by date --- .../api/common/make-pagination-query.ts | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/packages/backend/src/server/api/common/make-pagination-query.ts b/packages/backend/src/server/api/common/make-pagination-query.ts index a2c3275693..73b32b9a52 100644 --- a/packages/backend/src/server/api/common/make-pagination-query.ts +++ b/packages/backend/src/server/api/common/make-pagination-query.ts @@ -1,4 +1,4 @@ -import type { SelectQueryBuilder } from "typeorm"; +import type {SelectQueryBuilder} from "typeorm"; export function makePaginationQuery( q: SelectQueryBuilder, @@ -7,36 +7,50 @@ export function makePaginationQuery( sinceDate?: number, untilDate?: number, ) { - if (sinceId && untilId) { - q.andWhere(`${q.alias}.id > :sinceId`, { sinceId: sinceId }); - q.andWhere(`${q.alias}.id < :untilId`, { untilId: untilId }); - q.orderBy(`${q.alias}.id`, "DESC"); - } else if (sinceId) { - q.andWhere(`${q.alias}.id > :sinceId`, { sinceId: sinceId }); - q.orderBy(`${q.alias}.id`, "ASC"); - } else if (untilId) { - q.andWhere(`${q.alias}.id < :untilId`, { untilId: untilId }); - q.orderBy(`${q.alias}.id`, "DESC"); - } else if (sinceDate && untilDate) { - q.andWhere(`${q.alias}.createdAt > :sinceDate`, { + if (!sinceDate && !untilDate) { + if (sinceId && untilId) { + q.andWhere(`${q.alias}.id > :sinceId`, { sinceId: sinceId }); + q.andWhere(`${q.alias}.id < :untilId`, { untilId: untilId }); + q.orderBy(`${q.alias}.id`, "DESC"); + } else if (sinceId) { + q.andWhere(`${q.alias}.id > :sinceId`, { sinceId: sinceId }); + q.orderBy(`${q.alias}.id`, "ASC"); + } else if (untilId) { + q.andWhere(`${q.alias}.id < :untilId`, { untilId: untilId }); + q.orderBy(`${q.alias}.id`, "DESC"); + } + + return q; + } + + if (sinceDate && untilDate) { + q.andWhere(`(${q.alias}.createdAt, ${q.alias}.id) > (:sinceDate, :sinceId)`, { sinceDate: new Date(sinceDate), + sinceId: sinceId, }); - q.andWhere(`${q.alias}.createdAt < :untilDate`, { + q.andWhere(`(${q.alias}.createdAt, ${q.alias}.id) < (:untilDate, :untilId)`, { untilDate: new Date(untilDate), + untilId: untilId, }); q.orderBy(`${q.alias}.createdAt`, "DESC"); + q.addOrderBy(`${q.alias}.id`, "DESC"); } else if (sinceDate) { - q.andWhere(`${q.alias}.createdAt > :sinceDate`, { + q.andWhere(`(${q.alias}.createdAt, ${q.alias}.id) > (:sinceDate, :sinceId)`, { sinceDate: new Date(sinceDate), + sinceId: sinceId, }); q.orderBy(`${q.alias}.createdAt`, "ASC"); + q.addOrderBy(`${q.alias}.id`, "ASC"); } else if (untilDate) { - q.andWhere(`${q.alias}.createdAt < :untilDate`, { + q.andWhere(`(${q.alias}.createdAt, ${q.alias}.id) < (:untilDate, :untilId)`, { untilDate: new Date(untilDate), + untilId: untilId, }); q.orderBy(`${q.alias}.createdAt`, "DESC"); + q.addOrderBy(`${q.alias}.id`, "DESC"); } else { - q.orderBy(`${q.alias}.id`, "DESC"); + q.orderBy(`${q.alias}.createdAt`, "DESC"); + q.addOrderBy(`${q.alias}.id`, "DESC"); } return q; }