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

Fix: Pagination to consider limit value while .all() will keep returning all records #281

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

iamvishalkhare
Copy link

@iamvishalkhare iamvishalkhare commented Jun 25, 2022

Pagination fix

Problem?
Often times in production we don't want to return all matching records instead returning a paginated response based on values of limit and offset is desirable.

What is being fixed?
Calling .all() will keep returning all records matching the search criterion whereas if you set value of limit and then call .execute() with exhaust_results=False parameter, It will return number of records equal to value of limit.

Usage-
Let us consider 3 scenarios-

Scenario 1

model = Model.find(
    (condition 1) &
    (condition 2) &
    ....
    (condition n)
)
model.limit = 5
model.offset = 0
model.execute(exhaust_results=False)

This will return first 5 records matching search criterion.

Scenario 2

model = Model.find(
    (condition 1) &
    (condition 2) &
    ....
    (condition n)
)
model.execute()

This will return all records matching search criterion.

Scenario 3

model = Model.find(
    (condition 1) &
    (condition 2) &
    ....
    (condition n)
)
model.execute(exhaust_results=False)

This will return top 10 records matching search criterion because default value of limit is 10

How is it fixed?
To break away from loop executing the query a condition is added wherein if total number of fetched records is equal or greater than limit. (see file changes)

iamvishalkhare and others added 2 commits June 25, 2022 20:07
Problem?
Often time in production we don't want to return all matching records instead returning a paginated response based on values of limit and offset is desirable.

What is being fixed?
Even after setting a limit value it was not working and .all() or .execute() function were returning complete set of records matching the search criterion.

How is it fixed?
To break away from loop executing the query a condition is added wherein if total number of fetched records is equal or greater than limit.
If no limit is provided, default value i.e. 10 records will be returned.
@iamvishalkhare
Copy link
Author

@simonprickett - Any input is appreciated

@iamvishalkhare iamvishalkhare changed the title Fix: Pagination to consider limit value while .all() will return all records Fix: Pagination to consider limit value while .all() will keep returning all records Jun 25, 2022
@chayim
Copy link
Contributor

chayim commented Jul 5, 2022

@dvora-h can you please have a look?

@@ -742,8 +742,12 @@ async def execute(self, exhaust_results=True):
# current offset plus `page_size`, until we stop getting results back.
query = query.copy(offset=query.offset + query.page_size)
_results = await query.execute(exhaust_results=False)
if not _results:
break
if exhaust_results:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these two statements combine?

    break

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem important enough to block merging this. @chayim Can you approve?
Or would you like me to create a new PR?

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

Successfully merging this pull request may close these issues.

3 participants