-
-
Notifications
You must be signed in to change notification settings - Fork 296
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
Comments
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. |
They are only doing one query. /// 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. |
For ref: /// 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. |
And I want to expose the total number of items not the total number of pages because thats already being exposed. loco/src/model/query/paginate/mod.rs Line 214 in 4d1a450
|
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 by calling |
Yes exactly! This would give us more Information with no extra sql queries |
Sure, that sounds good. go for it 🥇 |
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.
The text was updated successfully, but these errors were encountered: