pub struct Pagination {
pub page: usize,
pub limit: usize,
pub max_limit: usize,
}Expand description
A standard pagination structure.
Can be deserialized from query parameters (e.g., ?page=1&limit=20).
Fields§
§page: usizeThe page number (0-indexed). Default: 0.
limit: usizeThe number of items per page. Default: 10.
max_limit: usizeThe maximum allowed limit for pagination to prevent large result sets.
If the requested limit exceeds max_limit, it will be capped (default: 100).
Implementations§
Source§impl Pagination
impl Pagination
Sourcepub fn new(page: usize, limit: usize, max_limit: usize) -> Self
pub fn new(page: usize, limit: usize, max_limit: usize) -> Self
Creates a new Pagination instance with a specified max_limit.
If limit is greater than max_limit, it defaults to 10.
Sourcepub fn apply<'a, T, E>(
self,
query: QueryBuilder<'a, T, E>,
) -> QueryBuilder<'a, T, E>
pub fn apply<'a, T, E>( self, query: QueryBuilder<'a, T, E>, ) -> QueryBuilder<'a, T, E>
Sourcepub async fn paginate<'a, T, E, R>(
self,
query: QueryBuilder<'a, T, E>,
) -> Result<Paginated<R>, Error>
pub async fn paginate<'a, T, E, R>( self, query: QueryBuilder<'a, T, E>, ) -> Result<Paginated<R>, Error>
Executes the query and returns a Paginated<T> result with metadata.
This method performs two database queries:
- A
COUNT(*)query to get the total number of records matching the filters. - The actual
SELECTquery withLIMITandOFFSETapplied.
§Type Parameters
T- The Model type.E- The connection type (Database or Transaction).R- The result type (usually same as T, but can be a DTO/Projection).
§Returns
Ok(Paginated<R>)- The paginated results.Err(sqlx::Error)- Database error.
§Example
let pagination = Pagination::new(0, 10);
let result = pagination.paginate(db.model::<User>()).await?;
println!("Total users: {}", result.total);
for user in result.data {
println!("User: {}", user.username);
}Sourcepub async fn paginate_as<'a, T, E, R>(
self,
query: QueryBuilder<'a, T, E>,
) -> Result<Paginated<R>, Error>
pub async fn paginate_as<'a, T, E, R>( self, query: QueryBuilder<'a, 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
impl Default for Pagination
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