Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination support #12

Open
jaumard opened this issue Aug 18, 2022 · 3 comments
Open

Pagination support #12

jaumard opened this issue Aug 18, 2022 · 3 comments

Comments

@jaumard
Copy link
Contributor

jaumard commented Aug 18, 2022

I'm used to do pagination with limit and offset, having something build in would be huge addition as of today the only way is to write the SQL.

@schultek
Copy link
Owner

There are limit and offset properties on the QueryParams class that you can provide to any query() method.

@jaumard
Copy link
Contributor Author

jaumard commented Aug 19, 2022

Ho I miss those, but what I had in mind was a bit more generic. But it might go with a query system.
The idea is that pagination is really common and you'll need it almost for all your get APIs. So having it manage by the ORM would be really nice.
You define the where query, the you call a toPagination(wantedPage, limit: limit) and under the hood the ORM is doing a count to get the total number and then divide by limit to get total number of pages. Then do the request with limit and offset and return something like:

{
  "totalPages": 10,
  "data": [queried model data here],
}

@jaumard
Copy link
Contributor Author

jaumard commented Aug 19, 2022

just for information that's how I did it when I tested angel3 ORM:

extension QueryExtension<T> on Query<T, QueryWhere> {
  Future<Pagination<T>> paginate(QueryExecutor executor, {int limit = kApiLimit, int page = 1}) async {
    final countItems = await count(executor);
    this
      ..limit(limit)
      ..offset((page - 1) * limit);
    final data = await get(executor);
    return Pagination(data, (countItems / limit).ceil());
  }

  Future<int> count(QueryExecutor executor) async {
    var countQuery = 'SELECT COUNT(*) FROM $tableName';
    final whereQuery = where!.compile(tableName: tableName);
    if (whereQuery.isNotEmpty) {
      countQuery += ' WHERE $whereQuery';
    }
    final result = await executor.query('not used', countQuery, {});
    return result.first.first;
  }
}

So at least I have it generic across all my API

@schultek schultek mentioned this issue Aug 20, 2022
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants