-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pagination computation is elegant with tuple_()
- Loading branch information
1 parent
d56f0de
commit e04965c
Showing
3 changed files
with
106 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
conda-store-server/conda_store_server/_internal/server/views/pagination.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import base64 | ||
from typing import Dict, List, Optional | ||
|
||
import pydantic | ||
from sqlalchemy import tuple_ | ||
from sqlalchemy.orm import Query as SqlQuery | ||
|
||
|
||
class Cursor(pydantic.BaseModel): | ||
last_id: Optional[int] = 1 | ||
|
||
# List of names of attributes to order by, and the last value of the ordered attribute | ||
# { | ||
# 'namespace': 'foo', | ||
# 'environment': 'bar', | ||
# } | ||
last_value: Optional[Dict[str, str]] = {} | ||
|
||
def dump(self): | ||
return base64.b64encode(self.model_dump_json()) | ||
|
||
@classmethod | ||
def load(cls, data: str): | ||
return cls.from_json(base64.b64decode(data)) | ||
|
||
|
||
def paginate( | ||
query: SqlQuery, | ||
cursor: Cursor, | ||
sort_by: List[str], | ||
valid_sort_by: Dict[str, object], | ||
) -> SqlQuery: | ||
"""Paginate the query using the cursor and the requested sort_bys. | ||
With cursor pagination, all keys used to order must be included in | ||
the call to query.filter(). | ||
https://medium.com/@george_16060/cursor-based-pagination-with-arbitrary-ordering-b4af6d5e22db | ||
Parameters | ||
---------- | ||
query : SqlQuery | ||
Query containing database results to paginate | ||
cursor : Cursor | ||
Cursor object containing information about the last item | ||
on the previous page | ||
sort_by : List[str] | ||
List of sort_by query parameters | ||
valid_sort_by : Dict[str, object] | ||
Mapping between query parameter names and the orm object they apply to | ||
""" | ||
objects = [] | ||
last_values = [] | ||
for obj in sort_by: | ||
objects.append(valid_sort_by[obj]) | ||
last_values.append(cursor.last_value[obj]) | ||
|
||
return query.filter( | ||
tuple_(*objects, object.id) > (*last_values, cursor.last_id) | ||
).order_by(sorts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters