import { QueryBuilder } from 'objection';

export interface PaginatedResult<T> {
  data: 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 { data: results, total };
}

export { QueryBuilder };
