Skip to content

Commit 1425084

Browse files
authored
Update motor sorting (#875)
1 parent fd1f0e8 commit 1425084

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

fastapi_pagination/ext/motor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__all__ = ["paginate"]
22

3-
from typing import Any, Dict, List, Optional, Sequence
3+
from typing import Any, Dict, List, Optional
44

55
from motor.core import AgnosticCollection
66

@@ -14,7 +14,7 @@ async def paginate(
1414
collection: AgnosticCollection,
1515
query_filter: Optional[Dict[Any, Any]] = None,
1616
params: Optional[AbstractParams] = None,
17-
sort: Optional[Sequence[Any]] = None,
17+
sort: Optional[Any] = None,
1818
*,
1919
transformer: Optional[AsyncItemsTransformer] = None,
2020
additional_data: Optional[AdditionalData] = None,
@@ -26,7 +26,7 @@ async def paginate(
2626
total = await collection.count_documents(query_filter)
2727
cursor = collection.find(query_filter, skip=raw_params.offset, limit=raw_params.limit, **kwargs)
2828
if sort is not None:
29-
cursor = cursor.sort(*sort)
29+
cursor = cursor.sort(*sort) if isinstance(sort, tuple) else cursor.sort(sort)
3030

3131
items = await cursor.to_list(length=raw_params.limit) # type: ignore[arg-type]
3232
t_items = await apply_items_transformer(items, transformer, async_=True)

tests/ext/test_motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def app(db_client, model_cls):
3333
@app.get("/default", response_model=Page[model_cls])
3434
@app.get("/limit-offset", response_model=LimitOffsetPage[model_cls])
3535
async def route():
36-
return await paginate(db_client.test.users, sort=["name", 1])
36+
return await paginate(db_client.test.users, sort=[("name", 1)])
3737

3838
return add_pagination(app)
3939

0 commit comments

Comments
 (0)