From 222d18f960de4697dd308009554272b437e75a9b Mon Sep 17 00:00:00 2001 From: phiresky Date: Tue, 18 Feb 2025 21:49:23 +0100 Subject: [PATCH] insert many simultaneously --- crates/api_common/src/tags.rs | 18 +++++++++++------- crates/db_schema/src/impls/post_tag.rs | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/crates/api_common/src/tags.rs b/crates/api_common/src/tags.rs index 4d59d334e2..0c42ac82d2 100644 --- a/crates/api_common/src/tags.rs +++ b/crates/api_common/src/tags.rs @@ -36,12 +36,16 @@ pub async fn update_post_tags( // Delete existing post tags PostTag::delete_for_post(&mut context.pool(), post.id).await?; // Create new post tags - for tag_id in tags { - let form = PostTagInsertForm { - post_id: post.id, - tag_id: *tag_id, - }; - PostTag::create(&mut context.pool(), &form).await?; - } + PostTag::create_many( + &mut context.pool(), + tags + .iter() + .map(|tag_id| PostTagInsertForm { + post_id: post.id, + tag_id: *tag_id, + }) + .collect(), + ) + .await?; Ok(()) } diff --git a/crates/db_schema/src/impls/post_tag.rs b/crates/db_schema/src/impls/post_tag.rs index 7e17a47b88..e3b903b914 100644 --- a/crates/db_schema/src/impls/post_tag.rs +++ b/crates/db_schema/src/impls/post_tag.rs @@ -49,3 +49,17 @@ impl Crud for PostTag { unimplemented!() } } + +impl PostTag { + pub async fn create_many( + pool: &mut DbPool<'_>, + forms: Vec, + ) -> Result<(), diesel::result::Error> { + let conn = &mut get_conn(pool).await?; + insert_into(post_tag::table) + .values(forms) + .execute(conn) + .await?; + Ok(()) + } +}