paginate method

Future<DbPage> paginate({
  1. int page = 1,
  2. int limit = 20,
})

Fetches one page of results and the total match count in a single call — pagination happens in SQL (LIMIT/OFFSET + COUNT(*)).

Add an orderBy to the builder for stable page boundaries.

final page = await db.query('users')
    .where('active', equals: true)
    .orderBy('id')
    .paginate(page: 2, limit: 20);
// page.rows, page.total, page.totalPages, page.hasNext, page.hasPrev

Implementation

Future<DbPage> paginate({int page = 1, int limit = 20}) async {
  final safePage = page < 1 ? 1 : page;
  final safeLimit = limit < 1 ? 1 : limit;
  final total = await count();
  _limit = safeLimit;
  _offset = (safePage - 1) * safeLimit;
  final result = await get();
  return DbPage(
    rows: result.rows,
    total: total,
    page: safePage,
    limit: safeLimit,
  );
}