Files
FrozenFriendsYume/packages/backend/built/server/api/mastodon/helpers/pagination.js
T
2026-07-26 18:25:37 +09:00

57 lines
2.0 KiB
JavaScript

import { generatePaginationData } from "../middleware/pagination.js";
export class PaginationHelpers {
static makePaginationQuery(q, sinceId, maxId, minId, idField = `${q.alias}.id`) {
if (sinceId && minId) throw new Error("Can't user both sinceId and minId params");
if (sinceId && maxId) {
q.andWhere(`${idField} > :sinceId`, {
sinceId: sinceId
});
q.andWhere(`${idField} < :maxId`, {
maxId: maxId
});
q.orderBy(`${idField}`, "DESC");
}
if (minId && maxId) {
q.andWhere(`${idField} > :minId`, {
minId: minId
});
q.andWhere(`${idField} < :maxId`, {
maxId: maxId
});
q.orderBy(`${idField}`, "ASC");
} else if (sinceId) {
q.andWhere(`${idField} > :sinceId`, {
sinceId: sinceId
});
q.orderBy(`${idField}`, "DESC");
} else if (minId) {
q.andWhere(`${idField} > :minId`, {
minId: minId
});
q.orderBy(`${idField}`, "ASC");
} else if (maxId) {
q.andWhere(`${idField} < :maxId`, {
maxId: maxId
});
q.orderBy(`${idField}`, "DESC");
} else {
q.orderBy(`${idField}`, "DESC");
}
return q;
}
/**
*
* @param query
* @param limit
* @param reverse whether the result needs to be .reverse()'d. Set this to true when the parameter minId is not undefined in the original request.
*/ static async execQuery(query, limit, reverse) {
return query.take(limit).getMany().then((found)=>reverse ? found.reverse() : found);
}
static async execQueryLinkPagination(query, limit, reverse, ctx) {
return this.execQuery(query, limit, reverse).then((p)=>{
ctx.pagination = generatePaginationData(p.map((x)=>x.id), limit);
return p;
});
}
}