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

add total field to PagerMeta #1194

Closed
DenuxPlays opened this issue Jan 20, 2025 · 7 comments · Fixed by #1197
Closed

add total field to PagerMeta #1194

DenuxPlays opened this issue Jan 20, 2025 · 7 comments · Fixed by #1197
Labels
enhancement New feature or request

Comments

@DenuxPlays
Copy link
Contributor

Feature Request

Hey would be great if the PagerMeta object would contain a field that has the total number of entities not just the total number of pages.

Is your feature request related to a problem? Please describe.

When building an API/backend with loco you maybe want to display the total number of something which you can only estimate when you just have the total number of pages.

Describe the solution you'd like

Use the Paginator::num_items_and_pages method to get the total number of items and pages.
Ref: https://docs.rs/sea-orm/latest/sea_orm/struct.Paginator.html#method.num_items_and_pages

Describe alternatives you've considered

Currently my alternative is to just copy, pase the struct into my code and extend them.

If you approve this I am open to open a pr.

@DenuxPlays DenuxPlays added the enhancement New feature or request label Jan 20, 2025
@kaplanelad
Copy link
Contributor

To calculate the total number of pages, you would need to make an additional query to the database (see function implementaion). From a performance perspective, adding an extra database query every time you use pagination might not be ideal.

@DenuxPlays
Copy link
Contributor Author

They are only doing one query.
Implementation:

/// Get the total number of items and pages
    pub async fn num_items_and_pages(&self) -> Result<ItemsAndPagesNumber, DbErr> {
        let number_of_items = self.num_items().await?;
        let number_of_pages = self.compute_pages_number(number_of_items);

        Ok(ItemsAndPagesNumber {
            number_of_items,
            number_of_pages,
        })
    }

    /// Compute the number of pages for the current page
    fn compute_pages_number(&self, num_items: u64) -> u64 {
        (num_items / self.page_size) + (num_items % self.page_size > 0) as u64
    }

This method returns BOTH number of pages and items with just one sql query, that why there is this helper method.
There is no perfomance perspective to worry about.

@DenuxPlays
Copy link
Contributor Author

For ref:
Implementation of the num_pages which is currently used:

   /// Get the total number of pages
    pub async fn num_pages(&self) -> Result<u64, DbErr> {
        let num_items = self.num_items().await?;
        let num_pages = self.compute_pages_number(num_items);
        Ok(num_pages)
    }

It uses num_items which is also used by the other function.

@DenuxPlays
Copy link
Contributor Author

And I want to expose the total number of items not the total number of pages because thats already being exposed.
See:

let total_pages = query.num_pages().await?;

@kaplanelad
Copy link
Contributor

kaplanelad commented Jan 20, 2025

Let’s start from the end:

this is the current response

{
    "results": [...],
    "pagination": {
        "page": 1,
        "page_size": 25,
        "total_pages": 1
    }
}

Would you like to include total_items in the response?

by calling num_items_and_pages instead of num_pages? and propagate them into the JSON response?

@DenuxPlays
Copy link
Contributor Author

Yes exactly!

This would give us more Information with no extra sql queries

@kaplanelad
Copy link
Contributor

Sure, that sounds good. go for it 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants