pub struct Paginator<'db, C, S>where
C: ConnectionTrait,
S: SelectorTrait + 'db,{ /* private fields */ }Expand description
Defined a structure to handle pagination of a result from a query operation on a Model
Implementations§
source§impl<'db, C, S> Paginator<'db, C, S>where
C: ConnectionTrait,
S: SelectorTrait + 'db,
impl<'db, C, S> Paginator<'db, C, S>where
C: ConnectionTrait,
S: SelectorTrait + 'db,
sourcepub async fn fetch_page(&self, page: u64) -> Result<Vec<S::Item>, DbErr>
pub async fn fetch_page(&self, page: u64) -> Result<Vec<S::Item>, DbErr>
Fetch a specific page; page index starts from zero
sourcepub async fn num_items(&self) -> Result<u64, DbErr>
pub async fn num_items(&self) -> Result<u64, DbErr>
Get the total number of items
Examples found in repository?
src/executor/paginator.rs (line 91)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
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)
}
/// 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
}
/// Increment the page counter
pub fn next(&mut self) {
self.page += 1;
}
/// Get current page number
pub fn cur_page(&self) -> u64 {
self.page
}
/// Fetch one page and increment the page counter
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let owned_db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![
/// # vec![cake::Model {
/// # id: 1,
/// # name: "Cake".to_owned(),
/// # }],
/// # vec![],
/// # ])
/// # .into_connection();
/// # let db = &owned_db;
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
/// let mut cake_pages = cake::Entity::find()
/// .order_by_asc(cake::Column::Id)
/// .paginate(db, 50);
///
/// while let Some(cakes) = cake_pages.fetch_and_next().await? {
/// // Do something on cakes: Vec<cake::Model>
/// }
/// #
/// # Ok(())
/// # }
/// ```
pub async fn fetch_and_next(&mut self) -> Result<Option<Vec<S::Item>>, DbErr> {
let vec = self.fetch().await?;
self.next();
let opt = if !vec.is_empty() { Some(vec) } else { None };
Ok(opt)
}
/// Convert self into an async stream
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let owned_db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![
/// # vec![cake::Model {
/// # id: 1,
/// # name: "Cake".to_owned(),
/// # }],
/// # vec![],
/// # ])
/// # .into_connection();
/// # let db = &owned_db;
/// #
/// use futures::TryStreamExt;
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
/// let mut cake_stream = cake::Entity::find()
/// .order_by_asc(cake::Column::Id)
/// .paginate(db, 50)
/// .into_stream();
///
/// while let Some(cakes) = cake_stream.try_next().await? {
/// // Do something on cakes: Vec<cake::Model>
/// }
/// #
/// # Ok(())
/// # }
/// ```
pub fn into_stream(mut self) -> PinBoxStream<'db, Result<Vec<S::Item>, DbErr>> {
Box::pin(stream! {
loop {
if let Some(vec) = self.fetch_and_next().await? {
yield Ok(vec);
} else {
break
}
}
})
}
}
#[async_trait::async_trait]
/// A Trait for any type that can paginate results
pub trait PaginatorTrait<'db, C>
where
C: ConnectionTrait,
{
/// Select operation
type Selector: SelectorTrait + Send + Sync + 'db;
/// Paginate the result of a select operation.
fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, Self::Selector>;
/// Perform a count on the paginated results
async fn count(self, db: &'db C) -> Result<u64, DbErr>
where
Self: Send + Sized,
{
self.paginate(db, 1).num_items().await
}sourcepub async fn num_items_and_pages(&self) -> Result<ItemsAndPagesNumber, DbErr>
pub async fn num_items_and_pages(&self) -> Result<ItemsAndPagesNumber, DbErr>
Get the total number of items and pages
sourcepub async fn fetch_and_next(&mut self) -> Result<Option<Vec<S::Item>>, DbErr>
pub async fn fetch_and_next(&mut self) -> Result<Option<Vec<S::Item>>, DbErr>
Fetch one page and increment the page counter
use sea_orm::{entity::*, query::*, tests_cfg::cake};
let mut cake_pages = cake::Entity::find()
.order_by_asc(cake::Column::Id)
.paginate(db, 50);
while let Some(cakes) = cake_pages.fetch_and_next().await? {
// Do something on cakes: Vec<cake::Model>
}sourcepub fn into_stream(self) -> PinBoxStream<'db, Result<Vec<S::Item>, DbErr>>
pub fn into_stream(self) -> PinBoxStream<'db, Result<Vec<S::Item>, DbErr>>
Convert self into an async stream
use futures::TryStreamExt;
use sea_orm::{entity::*, query::*, tests_cfg::cake};
let mut cake_stream = cake::Entity::find()
.order_by_asc(cake::Column::Id)
.paginate(db, 50)
.into_stream();
while let Some(cakes) = cake_stream.try_next().await? {
// Do something on cakes: Vec<cake::Model>
}