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

feat: re-routing latest page have items #916

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TonyDo99
Copy link

@TonyDo99 TonyDo99 commented Jan 11, 2025

Re-routing get the latest page that has items with the option routingLatest

Description

  • I found that we do not have the option to check and get the latest page has items. Let's give one scenario like this. At the front-end team, we do have 72082 pages and ten items per page, a total of 720821 items exist on the database. Suddenly one item is deleted by someone right at the moment another clicks to the latest page it probably returns empty items and the mismatch total page was calculated.

Example UI pagination

Screenshot 2025-01-11 at 18 15 57

Result without option routingLatest

Screenshot 2025-01-11 at 18 27 57

After re-routing with option routingLatest

  • It auto return the latest page have items
Screenshot 2025-01-11 at 18 29 43

Affected areas

modified: src/tests/paginate-raw-and-entities.spec.ts
modified: src/tests/paginate-raw.spec.ts
modified: src/tests/paginate.query.builder.spec.ts
modified: src/interfaces/index.ts
modified: src/paginate.ts

Test coverage

Screenshot 2025-01-11 at 18 30 42

@TonyDo99 TonyDo99 force-pushed the feature/routing_latest_page_have_items branch from 1579546 to a3a6899 Compare January 11, 2025 11:42
@TonyDo99
Copy link
Author

TonyDo99 commented Jan 14, 2025

Hi @bashleigh. Could you take a look at this feature?

@bashleigh
Copy link
Collaborator

Hi Tony, I think I understand the requirement of the feature however I feel this would include too many changes for the existing functionality. Is there a method we could do to avoid introducing too many changes to the existing methods? The wording also confuses me a little, routing latest, I can see you're trying to recalculate the pagination query to avoid a sort of 'race condition' scenario with deletions of already paginated items. Would you mind explaining how the math works?

https://github.com/nestjsx/nestjs-typeorm-paginate/pull/916/files#diff-c58e144dadf00250381ed55e6ce83245cda76aca84131ad494fd4911932f656fR74-R77

@TonyDo99
Copy link
Author

TonyDo99 commented Mar 9, 2025

Thank you for your reply @bashleigh. Take the example of 103 records that exist on the database. We need to paginate 10 items per page, then it will have 11 pages. The API call selects the page at 15, and the mathematics is used to recalculate the latest page that will have an item.

Example:
`page` - API selected
`limit` - API selected
`total` -  count total from queries

page = total / Number(limit) < Number(page)
         |            |              |
page =  103  /       10      <       15
  
page = Math.ceil(10.3) - ( it will return the page, the latest has an item mean page at 11 ) 

-- Otherwise it returns the page from the API selected

@TonyDo99
Copy link
Author

TonyDo99 commented Mar 9, 2025

I also tried to separate the feature, but currently I have not thought of a radical solution. I have applied this custom to my project, and it still works smoothly. I believe this feature should have when we use pagination

@TonyDo99 TonyDo99 force-pushed the feature/routing_latest_page_have_items branch from a7c0352 to f2bafb5 Compare March 13, 2025 06:19
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.

2 participants