Skip to content

Commit

Permalink
insert many simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
phiresky committed Feb 18, 2025
1 parent ecaa70f commit 222d18f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
18 changes: 11 additions & 7 deletions crates/api_common/src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
14 changes: 14 additions & 0 deletions crates/db_schema/src/impls/post_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ impl Crud for PostTag {
unimplemented!()
}
}

impl PostTag {
pub async fn create_many(
pool: &mut DbPool<'_>,
forms: Vec<PostTagInsertForm>,
) -> Result<(), diesel::result::Error> {
let conn = &mut get_conn(pool).await?;
insert_into(post_tag::table)
.values(forms)
.execute(conn)
.await?;
Ok(())
}
}

0 comments on commit 222d18f

Please sign in to comment.