Comments (27)
Can we avoid the turbo fish?
fruit::Entity::find().paginate::<fruit::Model>
from sea-orm.
So, both Select
and SelectTwo
can be converted into Paginator
?
let's say...
Select
=>SelectPaginator
SelectTwo
=>SelectTwoPaginator
from sea-orm.
Yes, any Select* can be converted into Paginator.
But no, there should only be one Paginator, which is generic over any Select
.
Similar idea to the built in traits Iterator
and IntoIterator
.
from sea-orm.
Getting Stream
from db connection
pub trait Connection {
// new
async fn fetch(&self, stmt: Statement) -> Pin<Box<dyn Stream<Item = Result<QueryResult, QueryErr>>>>;
}
Will be impl by SelectModel
and SelectTwoModel
to generalize the conversion from QueryResult
to Item
i.e. Item
returned by one()
and all()
FromQueryResult
(FromQueryResult, FromQueryResult)
serde_json::Value
- etc...
pub trait QueryResultConverter {
type Item;
fn convert_query_result(res: &QueryResult) -> Result<Self::Item, QueryErr>;
}
Paginator that accept any QueryResultConverter
and stream corresponding Item
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
- page_num
- page_size
from sea-orm.
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!();
}
}
from sea-orm.
Go see see the POC on pagination
branch
from sea-orm.
Need to print some timestamps to assess whether the time sequence is right
from sea-orm.
What do u mean? loll
from sea-orm.
Just found a related article
https://gendignoux.com/blog/2021/04/01/rust-async-streams-futures-part1.html
from sea-orm.
@tyt2y3 check pagination
branch again
from sea-orm.
Can we avoid the turbo fish?
fruit::Entity::find().paginate::<fruit::Model>
Why this is called turbo fish? loll
from sea-orm.
https://techblog.tonsser.com/posts/what-is-rusts-turbofish
from sea-orm.
See latest commit
from sea-orm.
Just figure out how to Pin
the Stream
, so user don't have to Pin
it manually
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>>> { ... }
from sea-orm.
Trying to get rid of stream!
macro
Ref: https://gist.github.com/TimLuq/83a35453405f4c6e0f63fb2a0caa9f6e
from sea-orm.
Please open a PR when it is ready.
from sea-orm.
Can you add some test cases to Paginator?
from sea-orm.
Some how it stuck in infinite loop when running the example
from sea-orm.
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.
from sea-orm.
Can you add some test cases to Paginator?
How should we test the Paginator
?
Perform assert_eq
between each elements from...
Entity::find().all()
Stream.try_next()
Is this a reasonable test case?
from sea-orm.
Oh it was my bad. But this is the exact problem we'd like to catch in test cases.
Since paging works across multiple database calls, it could only be fully tested with a mock connection.
from sea-orm.
So unit test for paginator will be implemented once mock connection is available?
from sea-orm.
Seems so.
from sea-orm.
Will mark it down on todo list
from sea-orm.
There is now MockDatabase for testing
Line 72 in faffc51
from sea-orm.
See the draft PR for the first paginator unit test. See if that make sense.
from sea-orm.
-
Should we add some helper to avoid this?
sea-orm/src/executor/paginator.rs
Lines 113 to 123 in 2b85526
-
And should we introduce trait
IntoMockRow
to avoid the manual into.
sea-orm/src/executor/paginator.rs
Lines 144 to 158 in 2b85526
-
Also, I cannot get the transaction log from
MockDatabase
once it turns intoDatabase
.
from sea-orm.
Related Issues (20)
- Syntax error when trying to drop multiple tables in migrations with SQLite HOT 1
- does postgres int8 work? HOT 2
- [Testing] Error: `A null value was encountered while decoding "num_items"` when testing pagination and counting HOT 1
- support sqlx v0.8 HOT 1
- sea-orm-cli 1.0 doesn't support mysql type: `bit` HOT 2
- VarBinary(16) column breaks DeriveEntityModel derive
- sea-orm-cli 1.0 generate entity from mysql generates String for MediumBlob fields
- Timezone offset information lost when reading a ChronoDateTimeWithTimeZone HOT 1
- Cannot run sea-orm-cli with feature codegen without sqlite as a dependency HOT 1
- `schema::custom()` requires `col` and `name` to be of same type
- Vec<RoleEnum> in SeaORM Entity Model Causes Compilation Error HOT 1
- No reference to features list or alternate DB connectors in docs.
- Support for `HashMap`
- `sea-orm-migration` is missing the `sqlx-all` feature
- Incorrect type in entity generation
- `ColumnDef` builder functions in `sea-orm-migration` may push additional statements instead of changing them HOT 2
- Allow the use of SmartString for strings
- issue with Migrator::up, the trait bound `&sea_orm::DatabaseConnection: IntoSchemaManagerConnection<'_>` is not satisfied HOT 2
- Generated entities: "the trait bound `Option<Vec<Vec<u8>>>: TryGetable` is not satisfied"
- Double insert when using String as primary key HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sea-orm.