Skip to main content

Paginate

Struct Paginate 

Source
pub struct Paginate<M> { /* private fields */ }
Expand description

Cursor-based pagination over a Query.

Paginate wraps a query with a fixed page size and provides after / before methods for forward and backward navigation using opaque cursors.

§Construction

Create a Paginate from a query via Paginate::new or by calling .into() on a query that already has limit and order_by set.

use toasty::stmt::{List, Paginate, Query};

let q = Query::<List<User>>::all()
    .order_by(User::fields().name().asc());
let page = Paginate::new(q, 20)
    .exec(&mut db)
    .await
    .unwrap();

§Requirements

The underlying query must have an order_by clause. Paginate::new sets the limit for you; From<Query<M>> requires both limit and order_by to be present already.

Implementations§

Source§

impl<M> Paginate<M>

Source

pub fn new(query: Query<List<M>>, per_page: usize) -> Paginate<M>

Create a paginator from query with the given page size.

The query must not already have a limit clause (this method sets it) and must have an order_by clause.

§Panics

Panics if query already has a limit or is missing order_by.

§Examples
use toasty::stmt::{List, Paginate, Query};

let q = Query::<List<User>>::all()
    .order_by(User::fields().name().asc());
let _paginator = Paginate::new(q, 20);
Source

pub fn after(self, key: impl Into<Expr>) -> Paginate<M>

Set the cursor for forward pagination.

Records returned will come after key in the current sort order. Obtain key from Page::next_cursor of a previous page.

§Panics

Panics if the query has no limit clause.

§Examples
use toasty::stmt::{List, Paginate, Query};

let q = Query::<List<User>>::all()
    .order_by(User::fields().id().asc());
let paginator = Paginate::new(q, 10)
    .after(toasty_core::stmt::Value::from(42_i64));
Source

pub fn before(self, key: impl Into<Expr>) -> Paginate<M>

Set the cursor for backward pagination.

Records returned will come before key in the current sort order. The result set is still returned in the original sort order (not reversed). Obtain key from Page::prev_cursor of a previous page.

§Panics

Panics if the query has no limit clause.

§Examples
use toasty::stmt::{List, Paginate, Query};

let q = Query::<List<User>>::all()
    .order_by(User::fields().id().asc());
let paginator = Paginate::new(q, 10)
    .before(toasty_core::stmt::Value::from(100_i64));
Source§

impl<M> Paginate<M>
where M: Load,

Source

pub async fn exec( self, executor: &mut dyn Executor, ) -> Result<Page<<M as Load>::Output>, Error>

Execute the paginated query and return a Page.

The returned page contains up to per_page items along with optional cursors for the next and previous pages. per_page is an upper bound, not a guarantee. The page size is applied to the database query, but Toasty may filter the returned rows further, so a page can have fewer than per_page items even when more results exist. Continue paging until next_cursor is None to consume the full result set.

§Examples
use toasty::stmt::{List, Paginate, Query};

let q = Query::<List<User>>::all()
    .order_by(User::fields().name().asc());
let page = Paginate::new(q, 20)
    .exec(&mut db)
    .await
    .unwrap();

Trait Implementations§

Source§

impl<M> Debug for Paginate<M>
where M: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<M> From<Query<List<M>>> for Paginate<M>

Source§

fn from(value: Query<List<M>>) -> Paginate<M>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<M> Freeze for Paginate<M>

§

impl<M> RefUnwindSafe for Paginate<M>
where M: RefUnwindSafe,

§

impl<M> Send for Paginate<M>
where M: Send,

§

impl<M> Sync for Paginate<M>
where M: Sync,

§

impl<M> Unpin for Paginate<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for Paginate<M>

§

impl<M> UnwindSafe for Paginate<M>
where M: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more