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>
impl<M> Paginate<M>
Sourcepub fn new(query: Query<List<M>>, per_page: usize) -> Paginate<M>
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);Sourcepub fn after(self, key: impl Into<Expr>) -> Paginate<M>
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));Sourcepub fn before(self, key: impl Into<Expr>) -> Paginate<M>
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,
impl<M> Paginate<M>where
M: Load,
Sourcepub async fn exec(
self,
executor: &mut dyn Executor,
) -> Result<Page<<M as Load>::Output>, Error>
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();