-
Notifications
You must be signed in to change notification settings - Fork 649
Implement async pagination #10021
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
Implement async pagination #10021
Changes from all commits
93cec6e
24da935
86c143a
afbc2dd
1302d73
1747c55
7d9347c
fb99e47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Turbo87 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,14 @@ use crate::models::helpers::with_count::*; | |
use crate::util::errors::{bad_request, AppResult}; | ||
use crate::util::{HeaderMapExt, RequestUtils}; | ||
|
||
use crate::util::diesel::prelude::*; | ||
use base64::{engine::general_purpose, Engine}; | ||
use diesel::pg::Pg; | ||
use diesel::prelude::*; | ||
use diesel::query_builder::{AstPass, Query, QueryFragment, QueryId}; | ||
use diesel::query_dsl::LoadQuery; | ||
use diesel::sql_types::BigInt; | ||
use diesel_async::AsyncPgConnection; | ||
use futures_util::future::BoxFuture; | ||
use futures_util::{FutureExt, TryStreamExt}; | ||
use http::header; | ||
use indexmap::IndexMap; | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -250,16 +252,29 @@ pub(crate) struct PaginatedQuery<T> { | |
} | ||
|
||
impl<T> PaginatedQuery<T> { | ||
pub(crate) fn load<'a, U, Conn>(self, conn: &mut Conn) -> QueryResult<Paginated<U>> | ||
pub fn load<'a, U>( | ||
self, | ||
conn: &'a mut AsyncPgConnection, | ||
) -> BoxFuture<'a, QueryResult<Paginated<U>>> | ||
where | ||
Self: LoadQuery<'a, Conn, WithCount<U>>, | ||
Self: diesel_async::methods::LoadQuery<'a, AsyncPgConnection, WithCount<U>>, | ||
T: 'a, | ||
U: Send + 'a, | ||
{ | ||
Comment on lines
+255
to
263
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I admit that I'm unsure about whether the lifetime There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no clue. I followed the suggestions of the compiler (when it wasn't crashing) and somehow made it work that way 😅 |
||
use diesel_async::methods::LoadQuery; | ||
|
||
let options = self.options.clone(); | ||
let records_and_total = self.internal_load(conn)?.collect::<QueryResult<_>>()?; | ||
Ok(Paginated { | ||
records_and_total, | ||
options, | ||
}) | ||
let future = self.internal_load(conn); | ||
|
||
async move { | ||
let records_and_total = future.await?.try_collect().await?; | ||
|
||
Ok(Paginated { | ||
records_and_total, | ||
options, | ||
}) | ||
} | ||
.boxed() | ||
} | ||
} | ||
|
||
|
@@ -272,8 +287,6 @@ impl<T: Query> Query for PaginatedQuery<T> { | |
type SqlType = (T::SqlType, BigInt); | ||
} | ||
|
||
impl<T, DB> diesel::RunQueryDsl<DB> for PaginatedQuery<T> {} | ||
|
||
impl<T> QueryFragment<Pg> for PaginatedQuery<T> | ||
where | ||
T: QueryFragment<Pg>, | ||
|
@@ -366,8 +379,6 @@ impl< | |
type SqlType = (T::SqlType, BigInt); | ||
} | ||
|
||
impl<T, C, DB> diesel::RunQueryDsl<DB> for PaginatedQueryWithCountSubq<T, C> {} | ||
|
||
impl<T, C> QueryFragment<Pg> for PaginatedQueryWithCountSubq<T, C> | ||
where | ||
T: QueryFragment<Pg>, | ||
|
@@ -390,16 +401,30 @@ where | |
} | ||
|
||
impl<T, C> PaginatedQueryWithCountSubq<T, C> { | ||
pub(crate) fn load<'a, U, Conn>(self, conn: &mut Conn) -> QueryResult<Paginated<U>> | ||
pub fn load<'a, U>( | ||
self, | ||
conn: &'a mut AsyncPgConnection, | ||
) -> BoxFuture<'a, QueryResult<Paginated<U>>> | ||
where | ||
Self: LoadQuery<'a, Conn, WithCount<U>>, | ||
Self: diesel_async::methods::LoadQuery<'a, AsyncPgConnection, WithCount<U>> + Send, | ||
C: 'a, | ||
T: 'a, | ||
U: Send + 'a, | ||
{ | ||
use diesel_async::methods::LoadQuery; | ||
|
||
let options = self.options.clone(); | ||
let records_and_total = self.internal_load(conn)?.collect::<QueryResult<_>>()?; | ||
Ok(Paginated { | ||
records_and_total, | ||
options, | ||
}) | ||
let future = self.internal_load(conn); | ||
|
||
async move { | ||
let records_and_total = future.await?.try_collect().await?; | ||
|
||
Ok(Paginated { | ||
records_and_total, | ||
options, | ||
}) | ||
} | ||
.boxed() | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.