-
-
Notifications
You must be signed in to change notification settings - Fork 515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pagination API #5
Comments
So, both let's say...
|
Yes, any Select* can be converted into Paginator. |
Getting pub trait Connection {
// new
async fn fetch(&self, stmt: Statement) -> Pin<Box<dyn Stream<Item = Result<QueryResult, QueryErr>>>>;
} Will be impl by i.e.
pub trait QueryResultConverter {
type Item;
fn convert_query_result(res: &QueryResult) -> Result<Self::Item, QueryErr>;
} Paginator that accept any pub struct Paginator<C>
where
C: QueryResultConverter,
{
pub(crate) query: SelectStatement,
pub(crate) convertor: PhantomData<C>,
}
impl<C> Paginator<C>
where
C: QueryResultConverter,
{
pub async fn fetch(self) -> Pin<Box<dyn Stream<Item = Result<C::Item, QueryErr>>>> {
todo!();
let item: C::Item = C::convert_query_result(&row);
}
} Still thinking who to keep the pagination state
|
The Paginator... use crate::{Database, QueryErr, SelectorTrait};
use futures::Stream;
use sea_query::SelectStatement;
use std::{
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};
#[derive(Clone, Debug)]
pub struct Paginator<'d, S>
where
S: SelectorTrait,
{
pub(crate) query: SelectStatement,
pub(crate) page: usize,
pub(crate) page_size: usize,
pub(crate) buffer: Vec<S::Item>,
pub(crate) db: &'d Database,
}
impl<'d, S> Paginator<'d, S>
where
S: SelectorTrait,
{
pub fn new(query: SelectStatement, page_size: usize, db: &'d Database) -> Self {
Self {
query,
page: 0,
page_size,
buffer: Vec::with_capacity(page_size),
db,
}
}
}
impl<S> Stream for Paginator<'_, S>
where
S: SelectorTrait,
{
type Item = S::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
todo!();
}
} |
Go see see the POC on |
Need to print some timestamps to assess whether the time sequence is right |
What do u mean? loll |
Just found a related article |
@tyt2y3 check |
Can we avoid the turbo fish? fruit::Entity::find().paginate::<fruit::Model> |
Why this is called turbo fish? loll |
See latest commit |
Just figure out how to But the method signature be like... // paginator.rs
pub type PinBoxStream<'db, Item> = Pin<Box<dyn Stream<Item = Item> + 'db>>;
pub fn into_stream(mut self) -> PinBoxStream<'db, Result<Vec<S::Item>, QueryErr>> { ... }
pub fn into_stream_future(mut self) -> PinBoxStream<'db, impl Future<Output = Result<Vec<S::Item>, QueryErr>>> { ... } |
Trying to get rid of Ref: https://gist.github.com/TimLuq/83a35453405f4c6e0f63fb2a0caa9f6e |
Please open a PR when it is ready. |
Can you add some test cases to Paginator? |
Some how it stuck in infinite loop when running the example |
I got the same issue. Fixed on latest commit, error caused by code refactoring. |
How should we test the Perform
Is this a reasonable test case? |
Oh it was my bad. But this is the exact problem we'd like to catch in test cases. |
So unit test for paginator will be implemented once mock connection is available? |
Seems so. |
Will mark it down on todo list |
There is now MockDatabase for testing Line 72 in faffc51
|
See the draft PR for the first paginator unit test. See if that make sense. |
|
A new struct
Paginator
that wraps aSelect
, that would:COUNT
the total number of records and number of pagesLIMIT
andOFFSET
A helper method to convert a select into a paginator: (something like)
The Paginator should impl the
Stream
trait.Ref:
https://docs.rs/futures-core/0.3.15/futures_core/stream/trait.Stream.html
https://tokio.rs/tokio/tutorial/streams
https://docs.rs/async-std/1.9.0/async_std/stream/trait.Stream.html
The text was updated successfully, but these errors were encountered: