pub struct Pagination {
pub page: usize,
pub limit: usize,
pub max_limit: usize,
}Expand description
A builder for pagination settings.
Use this struct to define how results should be paginated before executing
a query via paginate().
Fields§
§page: usizeZero-based page index
limit: usizeNumber of items per page
max_limit: usizeMaximum allowed items per page (safety limit)
Implementations§
Source§impl Pagination
impl Pagination
Sourcepub fn new_with_limit(page: usize, limit: usize, max_limit: usize) -> Self
pub fn new_with_limit(page: usize, limit: usize, max_limit: usize) -> Self
Creates a new Pagination instance with a custom safety limit.
§Arguments
page- Zero-based page numberlimit- Items per pagemax_limit- Maximum allowed items per page
Sourcepub fn new(page: usize, limit: usize) -> Self
pub fn new(page: usize, limit: usize) -> Self
Creates a new Pagination instance with a default safety limit of 100.
§Arguments
page- Zero-based page numberlimit- Items per page
Sourcepub fn apply<T, E>(self, query: QueryBuilder<T, E>) -> QueryBuilder<T, E>
pub fn apply<T, E>(self, query: QueryBuilder<T, E>) -> QueryBuilder<T, E>
Sourcepub async fn paginate<T, E, R>(
self,
query: QueryBuilder<T, E>,
) -> Result<Paginated<R>, Error>
pub async fn paginate<T, E, R>( self, query: QueryBuilder<T, E>, ) -> Result<Paginated<R>, Error>
Executes the query and returns a Paginated<R> structure.
This method performs two database operations:
- A
COUNT(*)query to determine total records. - The actual data query with
LIMITandOFFSETapplied.
§Type Parameters
T- The base Model type for the query.E- The connection type.R- The target result type (usually the same as T or a DTO).
§Returns
Ok(Paginated<R>)- The data and pagination metadata.Err(sqlx::Error)- Database error.
§Example
let p = Pagination::new(0, 20);
let res: Paginated<User> = p.paginate(db.model::<User>()).await?;
for user in res.data {
println!("User: {}", user.username);
}Sourcepub async fn paginate_as<T, E, R>(
self,
query: QueryBuilder<T, E>,
) -> Result<Paginated<R>, Error>
pub async fn paginate_as<T, E, R>( self, query: QueryBuilder<T, E>, ) -> Result<Paginated<R>, Error>
Executes the query and returns a Paginated<R> mapping to a custom DTO.
This method is similar to paginate, but it uses scan_as to map the results
to a type R that implements FromAnyRow but does not necessarily implement AnyImpl.
This is particularly useful for complex queries involving JOINs where the result
doesn’t map directly to a single Model.
§Type Parameters
T- The base Model type for the query.E- The connection type.R- The target result type (DTO/Projection).
§Returns
Ok(Paginated<R>)- The paginated results mapped to typeR.Err(sqlx::Error)- Database error.
Trait Implementations§
Source§impl Clone for Pagination
impl Clone for Pagination
Source§fn clone(&self) -> Pagination
fn clone(&self) -> Pagination
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Pagination
impl Debug for Pagination
Source§impl Default for Pagination
Default for axum headers
impl Default for Pagination
Default for axum headers
Source§impl<'de> Deserialize<'de> for Pagination
impl<'de> Deserialize<'de> for Pagination
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for Pagination
impl Serialize for Pagination
impl Copy for Pagination
Auto Trait Implementations§
impl Freeze for Pagination
impl RefUnwindSafe for Pagination
impl Send for Pagination
impl Sync for Pagination
impl Unpin for Pagination
impl UnsafeUnpin for Pagination
impl UnwindSafe for Pagination
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more