@@ -7,12 +7,14 @@ use crate::models::helpers::with_count::*;
7
7
use crate :: util:: errors:: { bad_request, AppResult } ;
8
8
use crate :: util:: { HeaderMapExt , RequestUtils } ;
9
9
10
- use crate :: util:: diesel:: prelude:: * ;
11
10
use base64:: { engine:: general_purpose, Engine } ;
12
11
use diesel:: pg:: Pg ;
12
+ use diesel:: prelude:: * ;
13
13
use diesel:: query_builder:: { AstPass , Query , QueryFragment , QueryId } ;
14
- use diesel:: query_dsl:: LoadQuery ;
15
14
use diesel:: sql_types:: BigInt ;
15
+ use diesel_async:: AsyncPgConnection ;
16
+ use futures_util:: future:: BoxFuture ;
17
+ use futures_util:: { FutureExt , TryStreamExt } ;
16
18
use http:: header;
17
19
use indexmap:: IndexMap ;
18
20
use serde:: { Deserialize , Serialize } ;
@@ -250,16 +252,29 @@ pub(crate) struct PaginatedQuery<T> {
250
252
}
251
253
252
254
impl < T > PaginatedQuery < T > {
253
- pub ( crate ) fn load < ' a , U , Conn > ( self , conn : & mut Conn ) -> QueryResult < Paginated < U > >
255
+ pub fn load < ' a , U > (
256
+ self ,
257
+ conn : & ' a mut AsyncPgConnection ,
258
+ ) -> BoxFuture < ' a , QueryResult < Paginated < U > > >
254
259
where
255
- Self : LoadQuery < ' a , Conn , WithCount < U > > ,
260
+ Self : diesel_async:: methods:: LoadQuery < ' a , AsyncPgConnection , WithCount < U > > ,
261
+ T : ' a ,
262
+ U : Send + ' a ,
256
263
{
264
+ use diesel_async:: methods:: LoadQuery ;
265
+
257
266
let options = self . options . clone ( ) ;
258
- let records_and_total = self . internal_load ( conn) ?. collect :: < QueryResult < _ > > ( ) ?;
259
- Ok ( Paginated {
260
- records_and_total,
261
- options,
262
- } )
267
+ let future = self . internal_load ( conn) ;
268
+
269
+ async move {
270
+ let records_and_total = future. await ?. try_collect ( ) . await ?;
271
+
272
+ Ok ( Paginated {
273
+ records_and_total,
274
+ options,
275
+ } )
276
+ }
277
+ . boxed ( )
263
278
}
264
279
}
265
280
@@ -272,8 +287,6 @@ impl<T: Query> Query for PaginatedQuery<T> {
272
287
type SqlType = ( T :: SqlType , BigInt ) ;
273
288
}
274
289
275
- impl < T , DB > diesel:: RunQueryDsl < DB > for PaginatedQuery < T > { }
276
-
277
290
impl < T > QueryFragment < Pg > for PaginatedQuery < T >
278
291
where
279
292
T : QueryFragment < Pg > ,
@@ -366,8 +379,6 @@ impl<
366
379
type SqlType = ( T :: SqlType , BigInt ) ;
367
380
}
368
381
369
- impl < T , C , DB > diesel:: RunQueryDsl < DB > for PaginatedQueryWithCountSubq < T , C > { }
370
-
371
382
impl < T , C > QueryFragment < Pg > for PaginatedQueryWithCountSubq < T , C >
372
383
where
373
384
T : QueryFragment < Pg > ,
@@ -390,16 +401,30 @@ where
390
401
}
391
402
392
403
impl < T , C > PaginatedQueryWithCountSubq < T , C > {
393
- pub ( crate ) fn load < ' a , U , Conn > ( self , conn : & mut Conn ) -> QueryResult < Paginated < U > >
404
+ pub fn load < ' a , U > (
405
+ self ,
406
+ conn : & ' a mut AsyncPgConnection ,
407
+ ) -> BoxFuture < ' a , QueryResult < Paginated < U > > >
394
408
where
395
- Self : LoadQuery < ' a , Conn , WithCount < U > > ,
409
+ Self : diesel_async:: methods:: LoadQuery < ' a , AsyncPgConnection , WithCount < U > > + Send ,
410
+ C : ' a ,
411
+ T : ' a ,
412
+ U : Send + ' a ,
396
413
{
414
+ use diesel_async:: methods:: LoadQuery ;
415
+
397
416
let options = self . options . clone ( ) ;
398
- let records_and_total = self . internal_load ( conn) ?. collect :: < QueryResult < _ > > ( ) ?;
399
- Ok ( Paginated {
400
- records_and_total,
401
- options,
402
- } )
417
+ let future = self . internal_load ( conn) ;
418
+
419
+ async move {
420
+ let records_and_total = future. await ?. try_collect ( ) . await ?;
421
+
422
+ Ok ( Paginated {
423
+ records_and_total,
424
+ options,
425
+ } )
426
+ }
427
+ . boxed ( )
403
428
}
404
429
}
405
430
0 commit comments