Skip to content

Commit 2c4fc61

Browse files
authored
controllers/category: Pipeline SQL queries (#10849)
1 parent 3a40109 commit 2c4fc61

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

crates/crates_io_database/src/models/category.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use diesel::dsl;
55
use diesel::prelude::*;
66
use diesel_async::scoped_futures::ScopedFutureExt;
77
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
8+
use futures_util::future::BoxFuture;
9+
use futures_util::FutureExt;
810
use std::future::Future;
911

1012
#[derive(Clone, Identifiable, Queryable, QueryableByName, Debug, Selectable)]
@@ -116,13 +118,16 @@ impl Category {
116118
.load(conn)
117119
}
118120

119-
pub async fn subcategories(&self, conn: &mut AsyncPgConnection) -> QueryResult<Vec<Category>> {
121+
pub fn subcategories(
122+
&self,
123+
conn: &mut AsyncPgConnection,
124+
) -> BoxFuture<'_, QueryResult<Vec<Category>>> {
120125
use diesel::sql_types::Text;
121126

122127
diesel::sql_query(include_str!("subcategories.sql"))
123128
.bind::<Text, _>(&self.category)
124129
.load(conn)
125-
.await
130+
.boxed()
126131
}
127132

128133
/// Gathers the parent categories from the top-level Category to the direct parent of this Category.

src/controllers/category.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use axum::Json;
88
use axum::extract::{FromRequestParts, Path, Query};
99
use diesel::QueryDsl;
1010
use diesel_async::RunQueryDsl;
11+
use futures_util::FutureExt;
1112
use http::request::Parts;
1213

1314
#[derive(Debug, Deserialize, FromRequestParts, utoipa::IntoParams)]
@@ -62,11 +63,13 @@ pub async fn list_categories(
6263

6364
let offset = options.offset().unwrap_or_default();
6465

65-
let categories = Category::toplevel(&mut conn, sort, options.per_page, offset).await?;
66-
let categories = categories.into_iter().map(Category::into).collect();
66+
let (categories, total) = tokio::try_join!(
67+
Category::toplevel(&mut conn, sort, options.per_page, offset).boxed(),
68+
// Query for the total count of categories
69+
Category::count_toplevel(&mut conn).boxed(),
70+
)?;
6771

68-
// Query for the total count of categories
69-
let total = Category::count_toplevel(&mut conn).await?;
72+
let categories = categories.into_iter().map(Category::into).collect();
7073

7174
let meta = ListMeta { total };
7275
Ok(Json(ListResponse { categories, meta }))
@@ -94,18 +97,13 @@ pub async fn find_category(
9497
let mut conn = state.db_read().await?;
9598

9699
let cat: Category = Category::by_slug(&slug).first(&mut conn).await?;
97-
let subcats = cat
98-
.subcategories(&mut conn)
99-
.await?
100-
.into_iter()
101-
.map(Category::into)
102-
.collect();
103-
let parents = cat
104-
.parent_categories(&mut conn)
105-
.await?
106-
.into_iter()
107-
.map(Category::into)
108-
.collect();
100+
let (subcats, parents) = tokio::try_join!(
101+
cat.subcategories(&mut conn),
102+
cat.parent_categories(&mut conn).boxed(),
103+
)?;
104+
105+
let subcats = subcats.into_iter().map(Category::into).collect();
106+
let parents = parents.into_iter().map(Category::into).collect();
109107

110108
let mut category = EncodableCategory::from(cat);
111109
category.subcategories = Some(subcats);

0 commit comments

Comments
 (0)