mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-22 18:07:31 -07:00
[mastodon-client] Improve query performance
This commit is contained in:
parent
f87712040d
commit
c90161189f
1 changed files with 1 additions and 21 deletions
|
@ -43,27 +43,7 @@ export class PaginationHelpers {
|
||||||
* @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.
|
* @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.
|
||||||
*/
|
*/
|
||||||
public static async execQuery<T extends ObjectLiteral>(query: SelectQueryBuilder<T>, limit: number, reverse: boolean): Promise<T[]> {
|
public static async execQuery<T extends ObjectLiteral>(query: SelectQueryBuilder<T>, limit: number, reverse: boolean): Promise<T[]> {
|
||||||
// We fetch more than requested because some may be filtered out, and if there's less than
|
return query.take(limit).getMany().then(found => reverse ? found.reverse() : found);
|
||||||
// requested, the pagination stops.
|
|
||||||
const found = [];
|
|
||||||
const take = Math.floor(limit * 1.5);
|
|
||||||
let skip = 0;
|
|
||||||
try {
|
|
||||||
while (found.length < limit) {
|
|
||||||
const notes = await query.take(take).skip(skip).getMany();
|
|
||||||
found.push(...notes);
|
|
||||||
skip += take;
|
|
||||||
if (notes.length < take) break;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (found.length > limit) {
|
|
||||||
found.length = limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
return reverse ? found.reverse() : found;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static appendLinkPaginationHeader(args: any, ctx: any, res: any, defaultLimit: number): void {
|
public static appendLinkPaginationHeader(args: any, ctx: any, res: any, defaultLimit: number): void {
|
||||||
|
|
Loading…
Reference in a new issue