Skip to content

Commit acef1a5

Browse files
committed
single page per page call
1 parent cf91eac commit acef1a5

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

aredis_om/model/model.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ def __init__(
422422
page_size: int = DEFAULT_PAGE_SIZE,
423423
sort_fields: Optional[List[str]] = None,
424424
nocontent: bool = False,
425+
single_page: bool = False,
425426
):
426427
if not has_redisearch(model.db()):
427428
raise RedisModelError(
@@ -437,6 +438,7 @@ def __init__(
437438
self.limit = limit or (self.knn.k if self.knn else DEFAULT_PAGE_SIZE)
438439
self.page_size = page_size
439440
self.nocontent = nocontent
441+
self.single_page = single_page
440442

441443
if sort_fields:
442444
self.sort_fields = self.validate_sort_fields(sort_fields)
@@ -916,6 +918,9 @@ async def execute(self, exhaust_results=True, return_raw_result=False):
916918
if count <= len(results):
917919
return self._model_cache
918920

921+
if self.single_page:
922+
return self._model_cache
923+
919924
# Transparently (to the user) make subsequent requests to paginate
920925
# through the results and finally return them all.
921926
query = self
@@ -948,7 +953,7 @@ async def all(self, batch_size=DEFAULT_PAGE_SIZE):
948953
return await self.execute()
949954

950955
async def page(self, offset=0, limit=10):
951-
return await self.copy(offset=offset, limit=limit).execute()
956+
return await self.copy(offset=offset, limit=limit, single_page=True).execute()
952957

953958
def sort_by(self, *fields: str):
954959
if not fields:

tests/test_json_model.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,3 +1069,28 @@ class Example(JsonModel):
10691069

10701070
res = await Example.find(Example.d == ex.d and Example.b == True).first()
10711071
assert res.name == ex.name
1072+
1073+
1074+
@py_test_mark_asyncio
1075+
async def test_pagination():
1076+
class Test(JsonModel):
1077+
id: str = Field(primary_key=True, index=True)
1078+
num: int = Field(sortable=True, index=True)
1079+
1080+
@classmethod
1081+
async def get_page(cls, offset, limit):
1082+
return await cls.find().sort_by("num").page(limit=limit, offset=offset)
1083+
1084+
await Migrator().run()
1085+
1086+
pipe = Test.Meta.database.pipeline()
1087+
for i in range(0, 1000):
1088+
await Test(num=i, id=str(i)).save(pipeline=pipe)
1089+
1090+
await pipe.execute()
1091+
res = await Test.get_page(100, 100)
1092+
assert len(res) == 100
1093+
assert res[0].num == 100
1094+
res = await Test.get_page(10, 30)
1095+
assert len(res) == 30
1096+
assert res[0].num == 10

0 commit comments

Comments
 (0)