mirror of
https://git.joinfirefish.org/firefish/firefish.git
synced 2024-05-19 02:01:12 +02:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import type { ObjectLiteral, SelectQueryBuilder } from "typeorm";
|
|
|
|
export function makePaginationQuery<T extends ObjectLiteral>(
|
|
q: SelectQueryBuilder<T>,
|
|
sinceId?: string,
|
|
untilId?: string,
|
|
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`, {
|
|
sinceDate: new Date(sinceDate),
|
|
});
|
|
q.andWhere(`${q.alias}.createdAt < :untilDate`, {
|
|
untilDate: new Date(untilDate),
|
|
});
|
|
q.orderBy(`${q.alias}.createdAt`, "DESC");
|
|
} else if (sinceDate) {
|
|
q.andWhere(`${q.alias}.createdAt > :sinceDate`, {
|
|
sinceDate: new Date(sinceDate),
|
|
});
|
|
q.orderBy(`${q.alias}.createdAt`, "ASC");
|
|
} else if (untilDate) {
|
|
q.andWhere(`${q.alias}.createdAt < :untilDate`, {
|
|
untilDate: new Date(untilDate),
|
|
});
|
|
q.orderBy(`${q.alias}.createdAt`, "DESC");
|
|
} else {
|
|
q.orderBy(`${q.alias}.id`, "DESC");
|
|
}
|
|
return q;
|
|
}
|