Use pagination by date
ci/woodpecker/tag/ociImageTag Pipeline was successful Details

This commit is contained in:
Natty 2024-01-01 20:21:22 +01:00
parent 79a7e6c232
commit a1d8e1ac97
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
1 changed files with 31 additions and 17 deletions

View File

@ -1,4 +1,4 @@
import type { SelectQueryBuilder } from "typeorm"; import type {SelectQueryBuilder} from "typeorm";
export function makePaginationQuery<T>( export function makePaginationQuery<T>(
q: SelectQueryBuilder<T>, q: SelectQueryBuilder<T>,
@ -7,36 +7,50 @@ export function makePaginationQuery<T>(
sinceDate?: number, sinceDate?: number,
untilDate?: number, untilDate?: number,
) { ) {
if (sinceId && untilId) { if (!sinceDate && !untilDate) {
q.andWhere(`${q.alias}.id > :sinceId`, { sinceId: sinceId }); if (sinceId && untilId) {
q.andWhere(`${q.alias}.id < :untilId`, { untilId: untilId }); q.andWhere(`${q.alias}.id > :sinceId`, { sinceId: sinceId });
q.orderBy(`${q.alias}.id`, "DESC"); q.andWhere(`${q.alias}.id < :untilId`, { untilId: untilId });
} else if (sinceId) { q.orderBy(`${q.alias}.id`, "DESC");
q.andWhere(`${q.alias}.id > :sinceId`, { sinceId: sinceId }); } else if (sinceId) {
q.orderBy(`${q.alias}.id`, "ASC"); q.andWhere(`${q.alias}.id > :sinceId`, { sinceId: sinceId });
} else if (untilId) { q.orderBy(`${q.alias}.id`, "ASC");
q.andWhere(`${q.alias}.id < :untilId`, { untilId: untilId }); } else if (untilId) {
q.orderBy(`${q.alias}.id`, "DESC"); q.andWhere(`${q.alias}.id < :untilId`, { untilId: untilId });
} else if (sinceDate && untilDate) { q.orderBy(`${q.alias}.id`, "DESC");
q.andWhere(`${q.alias}.createdAt > :sinceDate`, { }
return q;
}
if (sinceDate && untilDate) {
q.andWhere(`(${q.alias}.createdAt, ${q.alias}.id) > (:sinceDate, :sinceId)`, {
sinceDate: new Date(sinceDate), 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), untilDate: new Date(untilDate),
untilId: untilId,
}); });
q.orderBy(`${q.alias}.createdAt`, "DESC"); q.orderBy(`${q.alias}.createdAt`, "DESC");
q.addOrderBy(`${q.alias}.id`, "DESC");
} else if (sinceDate) { } else if (sinceDate) {
q.andWhere(`${q.alias}.createdAt > :sinceDate`, { q.andWhere(`(${q.alias}.createdAt, ${q.alias}.id) > (:sinceDate, :sinceId)`, {
sinceDate: new Date(sinceDate), sinceDate: new Date(sinceDate),
sinceId: sinceId,
}); });
q.orderBy(`${q.alias}.createdAt`, "ASC"); q.orderBy(`${q.alias}.createdAt`, "ASC");
q.addOrderBy(`${q.alias}.id`, "ASC");
} else if (untilDate) { } else if (untilDate) {
q.andWhere(`${q.alias}.createdAt < :untilDate`, { q.andWhere(`(${q.alias}.createdAt, ${q.alias}.id) < (:untilDate, :untilId)`, {
untilDate: new Date(untilDate), untilDate: new Date(untilDate),
untilId: untilId,
}); });
q.orderBy(`${q.alias}.createdAt`, "DESC"); q.orderBy(`${q.alias}.createdAt`, "DESC");
q.addOrderBy(`${q.alias}.id`, "DESC");
} else { } else {
q.orderBy(`${q.alias}.id`, "DESC"); q.orderBy(`${q.alias}.createdAt`, "DESC");
q.addOrderBy(`${q.alias}.id`, "DESC");
} }
return q; return q;
} }