25 lines
507 B
TypeScript
25 lines
507 B
TypeScript
|
|
import { QueryBuilder } from 'objection';
|
||
|
|
|
||
|
|
export interface PaginatedResult<T> {
|
||
|
|
results: T[];
|
||
|
|
total: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface PaginationOptions {
|
||
|
|
page: number;
|
||
|
|
limit: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function paginate<T>(
|
||
|
|
query: QueryBuilder<any, T[]>,
|
||
|
|
page: number,
|
||
|
|
limit: number,
|
||
|
|
): Promise<PaginatedResult<T>> {
|
||
|
|
const p = page > 0 ? page - 1 : 0;
|
||
|
|
const l = limit > 0 ? limit : 10;
|
||
|
|
|
||
|
|
const { results, total } = await query.page(p, l);
|
||
|
|
return { results, total };
|
||
|
|
}
|
||
|
|
|
||
|
|
export { QueryBuilder };
|