diff --git a/crates/db_schema/src/impls/comment.rs b/crates/db_schema/src/impls/comment.rs index 7538dcea6..8f3eb7563 100644 --- a/crates/db_schema/src/impls/comment.rs +++ b/crates/db_schema/src/impls/comment.rs @@ -152,23 +152,23 @@ where ca.comment_id = c.id" } #[async_trait] -impl<'a> Crud<'a> for Comment { - type InsertForm = CommentInsertForm; - type UpdateForm = CommentUpdateForm; +impl Crud for Comment { + type InsertForm<'a> = &'a CommentInsertForm; + type UpdateForm<'a> = &'a CommentUpdateForm; type IdType = CommentId; /// This is unimplemented, use [[Comment::create]] - async fn create( + async fn create<'a>( _pool: &mut DbPool<'_>, - _comment_form: &'a Self::InsertForm, + _comment_form: Self::InsertForm<'a>, ) -> Result { unimplemented!(); } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, comment_id: CommentId, - comment_form: &'a Self::UpdateForm, + comment_form: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(comment.find(comment_id)) diff --git a/crates/db_schema/src/impls/comment_reply.rs b/crates/db_schema/src/impls/comment_reply.rs index a7c5d5159..a01a75cb5 100644 --- a/crates/db_schema/src/impls/comment_reply.rs +++ b/crates/db_schema/src/impls/comment_reply.rs @@ -9,14 +9,14 @@ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; #[async_trait] -impl<'a> Crud<'a> for CommentReply { - type InsertForm = CommentReplyInsertForm; - type UpdateForm = CommentReplyUpdateForm; +impl Crud for CommentReply { + type InsertForm<'a> = &'a CommentReplyInsertForm; + type UpdateForm<'a> = &'a CommentReplyUpdateForm; type IdType = CommentReplyId; - async fn create( + async fn create<'a>( pool: &mut DbPool<'_>, - comment_reply_form: &'a Self::InsertForm, + comment_reply_form: Self::InsertForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; @@ -31,10 +31,10 @@ impl<'a> Crud<'a> for CommentReply { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, comment_reply_id: CommentReplyId, - comment_reply_form: &'a Self::UpdateForm, + comment_reply_form: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(comment_reply.find(comment_reply_id)) diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index 34a724602..24217e5a2 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -23,12 +23,12 @@ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; #[async_trait] -impl<'a> Crud<'a> for Community { - type InsertForm = CommunityInsertForm; - type UpdateForm = CommunityUpdateForm; +impl Crud for Community { + type InsertForm<'a> = &'a CommunityInsertForm; + type UpdateForm<'a> = &'a CommunityUpdateForm; type IdType = CommunityId; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { let is_new_community = match &form.actor_id { Some(id) => Community::read_from_apub_id(pool, id).await?.is_none(), None => true, @@ -52,10 +52,10 @@ impl<'a> Crud<'a> for Community { Ok(community_) } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, community_id: CommunityId, - form: &'a Self::UpdateForm, + form: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(community::table.find(community_id)) diff --git a/crates/db_schema/src/impls/local_user.rs b/crates/db_schema/src/impls/local_user.rs index a381a5f89..3f77f3868 100644 --- a/crates/db_schema/src/impls/local_user.rs +++ b/crates/db_schema/src/impls/local_user.rs @@ -65,12 +65,12 @@ impl LocalUser { } #[async_trait] -impl<'a> Crud<'a> for LocalUser { - type InsertForm = LocalUserInsertForm; - type UpdateForm = LocalUserUpdateForm; +impl Crud for LocalUser { + type InsertForm<'a> = &'a LocalUserInsertForm; + type UpdateForm<'a> = &'a LocalUserUpdateForm; type IdType = LocalUserId; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { let conn = &mut get_conn(pool).await?; let mut form_with_encrypted_password = form.clone(); let password_hash = @@ -94,10 +94,10 @@ impl<'a> Crud<'a> for LocalUser { Ok(local_user_) } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, local_user_id: LocalUserId, - form: &'a Self::UpdateForm, + form: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(local_user.find(local_user_id)) diff --git a/crates/db_schema/src/impls/moderator.rs b/crates/db_schema/src/impls/moderator.rs index 46cf97d8a..c79710ee3 100644 --- a/crates/db_schema/src/impls/moderator.rs +++ b/crates/db_schema/src/impls/moderator.rs @@ -38,12 +38,12 @@ use diesel::{dsl::insert_into, result::Error, QueryDsl}; use diesel_async::RunQueryDsl; #[async_trait] -impl<'a> Crud<'a> for ModRemovePost { - type InsertForm = ModRemovePostForm; - type UpdateForm = ModRemovePostForm; +impl Crud for ModRemovePost { + type InsertForm<'a> = &'a ModRemovePostForm; + type UpdateForm<'a> = &'a ModRemovePostForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModRemovePostForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: &'a ModRemovePostForm) -> Result { use crate::schema::mod_remove_post::dsl::mod_remove_post; let conn = &mut get_conn(pool).await?; insert_into(mod_remove_post) @@ -52,7 +52,7 @@ impl<'a> Crud<'a> for ModRemovePost { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModRemovePostForm, @@ -67,12 +67,12 @@ impl<'a> Crud<'a> for ModRemovePost { } #[async_trait] -impl<'a> Crud<'a> for ModLockPost { - type InsertForm = ModLockPostForm; - type UpdateForm = ModLockPostForm; +impl Crud for ModLockPost { + type InsertForm<'a> = &'a ModLockPostForm; + type UpdateForm<'a> = &'a ModLockPostForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModLockPostForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: &'a ModLockPostForm) -> Result { use crate::schema::mod_lock_post::dsl::mod_lock_post; let conn = &mut get_conn(pool).await?; insert_into(mod_lock_post) @@ -81,7 +81,7 @@ impl<'a> Crud<'a> for ModLockPost { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModLockPostForm, @@ -96,12 +96,12 @@ impl<'a> Crud<'a> for ModLockPost { } #[async_trait] -impl<'a> Crud<'a> for ModFeaturePost { - type InsertForm = ModFeaturePostForm; - type UpdateForm = ModFeaturePostForm; +impl Crud for ModFeaturePost { + type InsertForm<'a> = &'a ModFeaturePostForm; + type UpdateForm<'a> = &'a ModFeaturePostForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModFeaturePostForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: &'a ModFeaturePostForm) -> Result { use crate::schema::mod_feature_post::dsl::mod_feature_post; let conn = &mut get_conn(pool).await?; insert_into(mod_feature_post) @@ -110,7 +110,7 @@ impl<'a> Crud<'a> for ModFeaturePost { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModFeaturePostForm, @@ -125,12 +125,15 @@ impl<'a> Crud<'a> for ModFeaturePost { } #[async_trait] -impl<'a> Crud<'a> for ModRemoveComment { - type InsertForm = ModRemoveCommentForm; - type UpdateForm = ModRemoveCommentForm; +impl Crud for ModRemoveComment { + type InsertForm<'a> = &'a ModRemoveCommentForm; + type UpdateForm<'a> = &'a ModRemoveCommentForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModRemoveCommentForm) -> Result { + async fn create<'a>( + pool: &mut DbPool<'_>, + form: &'a ModRemoveCommentForm, + ) -> Result { use crate::schema::mod_remove_comment::dsl::mod_remove_comment; let conn = &mut get_conn(pool).await?; insert_into(mod_remove_comment) @@ -139,7 +142,7 @@ impl<'a> Crud<'a> for ModRemoveComment { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModRemoveCommentForm, @@ -154,12 +157,15 @@ impl<'a> Crud<'a> for ModRemoveComment { } #[async_trait] -impl<'a> Crud<'a> for ModRemoveCommunity { - type InsertForm = ModRemoveCommunityForm; - type UpdateForm = ModRemoveCommunityForm; +impl Crud for ModRemoveCommunity { + type InsertForm<'a> = &'a ModRemoveCommunityForm; + type UpdateForm<'a> = &'a ModRemoveCommunityForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModRemoveCommunityForm) -> Result { + async fn create<'a>( + pool: &mut DbPool<'_>, + form: &'a ModRemoveCommunityForm, + ) -> Result { use crate::schema::mod_remove_community::dsl::mod_remove_community; let conn = &mut get_conn(pool).await?; insert_into(mod_remove_community) @@ -168,7 +174,7 @@ impl<'a> Crud<'a> for ModRemoveCommunity { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModRemoveCommunityForm, @@ -183,12 +189,15 @@ impl<'a> Crud<'a> for ModRemoveCommunity { } #[async_trait] -impl<'a> Crud<'a> for ModBanFromCommunity { - type InsertForm = ModBanFromCommunityForm; - type UpdateForm = ModBanFromCommunityForm; +impl Crud for ModBanFromCommunity { + type InsertForm<'a> = &'a ModBanFromCommunityForm; + type UpdateForm<'a> = &'a ModBanFromCommunityForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModBanFromCommunityForm) -> Result { + async fn create<'a>( + pool: &mut DbPool<'_>, + form: &'a ModBanFromCommunityForm, + ) -> Result { use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community; let conn = &mut get_conn(pool).await?; insert_into(mod_ban_from_community) @@ -197,7 +206,7 @@ impl<'a> Crud<'a> for ModBanFromCommunity { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModBanFromCommunityForm, @@ -212,12 +221,12 @@ impl<'a> Crud<'a> for ModBanFromCommunity { } #[async_trait] -impl<'a> Crud<'a> for ModBan { - type InsertForm = ModBanForm; - type UpdateForm = ModBanForm; +impl Crud for ModBan { + type InsertForm<'a> = &'a ModBanForm; + type UpdateForm<'a> = &'a ModBanForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModBanForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: &'a ModBanForm) -> Result { use crate::schema::mod_ban::dsl::mod_ban; let conn = &mut get_conn(pool).await?; insert_into(mod_ban) @@ -226,7 +235,7 @@ impl<'a> Crud<'a> for ModBan { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModBanForm, @@ -241,12 +250,15 @@ impl<'a> Crud<'a> for ModBan { } #[async_trait] -impl<'a> Crud<'a> for ModHideCommunity { - type InsertForm = ModHideCommunityForm; - type UpdateForm = ModHideCommunityForm; +impl Crud for ModHideCommunity { + type InsertForm<'a> = &'a ModHideCommunityForm; + type UpdateForm<'a> = &'a ModHideCommunityForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModHideCommunityForm) -> Result { + async fn create<'a>( + pool: &mut DbPool<'_>, + form: &'a ModHideCommunityForm, + ) -> Result { use crate::schema::mod_hide_community::dsl::mod_hide_community; let conn = &mut get_conn(pool).await?; insert_into(mod_hide_community) @@ -255,7 +267,7 @@ impl<'a> Crud<'a> for ModHideCommunity { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModHideCommunityForm, @@ -270,12 +282,12 @@ impl<'a> Crud<'a> for ModHideCommunity { } #[async_trait] -impl<'a> Crud<'a> for ModAddCommunity { - type InsertForm = ModAddCommunityForm; - type UpdateForm = ModAddCommunityForm; +impl Crud for ModAddCommunity { + type InsertForm<'a> = &'a ModAddCommunityForm; + type UpdateForm<'a> = &'a ModAddCommunityForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModAddCommunityForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: &'a ModAddCommunityForm) -> Result { use crate::schema::mod_add_community::dsl::mod_add_community; let conn = &mut get_conn(pool).await?; insert_into(mod_add_community) @@ -284,7 +296,7 @@ impl<'a> Crud<'a> for ModAddCommunity { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModAddCommunityForm, @@ -299,12 +311,12 @@ impl<'a> Crud<'a> for ModAddCommunity { } #[async_trait] -impl<'a> Crud<'a> for ModTransferCommunity { - type InsertForm = ModTransferCommunityForm; - type UpdateForm = ModTransferCommunityForm; +impl Crud for ModTransferCommunity { + type InsertForm<'a> = &'a ModTransferCommunityForm; + type UpdateForm<'a> = &'a ModTransferCommunityForm; type IdType = i32; - async fn create( + async fn create<'a>( pool: &mut DbPool<'_>, form: &'a ModTransferCommunityForm, ) -> Result { @@ -316,7 +328,7 @@ impl<'a> Crud<'a> for ModTransferCommunity { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModTransferCommunityForm, @@ -331,12 +343,12 @@ impl<'a> Crud<'a> for ModTransferCommunity { } #[async_trait] -impl<'a> Crud<'a> for ModAdd { - type InsertForm = ModAddForm; - type UpdateForm = ModAddForm; +impl Crud for ModAdd { + type InsertForm<'a> = &'a ModAddForm; + type UpdateForm<'a> = &'a ModAddForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a ModAddForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: &'a ModAddForm) -> Result { use crate::schema::mod_add::dsl::mod_add; let conn = &mut get_conn(pool).await?; insert_into(mod_add) @@ -345,7 +357,7 @@ impl<'a> Crud<'a> for ModAdd { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, form: &'a ModAddForm, @@ -360,12 +372,12 @@ impl<'a> Crud<'a> for ModAdd { } #[async_trait] -impl<'a> Crud<'a> for AdminPurgePerson { - type InsertForm = AdminPurgePersonForm; - type UpdateForm = AdminPurgePersonForm; +impl Crud for AdminPurgePerson { + type InsertForm<'a> = &'a AdminPurgePersonForm; + type UpdateForm<'a> = &'a AdminPurgePersonForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { use crate::schema::admin_purge_person::dsl::admin_purge_person; let conn = &mut get_conn(pool).await?; insert_into(admin_purge_person) @@ -374,10 +386,10 @@ impl<'a> Crud<'a> for AdminPurgePerson { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, - form: &'a Self::InsertForm, + form: Self::InsertForm<'a>, ) -> Result { use crate::schema::admin_purge_person::dsl::admin_purge_person; let conn = &mut get_conn(pool).await?; @@ -389,12 +401,12 @@ impl<'a> Crud<'a> for AdminPurgePerson { } #[async_trait] -impl<'a> Crud<'a> for AdminPurgeCommunity { - type InsertForm = AdminPurgeCommunityForm; - type UpdateForm = AdminPurgeCommunityForm; +impl Crud for AdminPurgeCommunity { + type InsertForm<'a> = &'a AdminPurgeCommunityForm; + type UpdateForm<'a> = &'a AdminPurgeCommunityForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { use crate::schema::admin_purge_community::dsl::admin_purge_community; let conn = &mut get_conn(pool).await?; insert_into(admin_purge_community) @@ -403,10 +415,10 @@ impl<'a> Crud<'a> for AdminPurgeCommunity { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, - form: &'a Self::InsertForm, + form: Self::InsertForm<'a>, ) -> Result { use crate::schema::admin_purge_community::dsl::admin_purge_community; let conn = &mut get_conn(pool).await?; @@ -418,12 +430,12 @@ impl<'a> Crud<'a> for AdminPurgeCommunity { } #[async_trait] -impl<'a> Crud<'a> for AdminPurgePost { - type InsertForm = AdminPurgePostForm; - type UpdateForm = AdminPurgePostForm; +impl Crud for AdminPurgePost { + type InsertForm<'a> = &'a AdminPurgePostForm; + type UpdateForm<'a> = &'a AdminPurgePostForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { use crate::schema::admin_purge_post::dsl::admin_purge_post; let conn = &mut get_conn(pool).await?; insert_into(admin_purge_post) @@ -432,10 +444,10 @@ impl<'a> Crud<'a> for AdminPurgePost { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, - form: &'a Self::InsertForm, + form: Self::InsertForm<'a>, ) -> Result { use crate::schema::admin_purge_post::dsl::admin_purge_post; let conn = &mut get_conn(pool).await?; @@ -447,12 +459,12 @@ impl<'a> Crud<'a> for AdminPurgePost { } #[async_trait] -impl<'a> Crud<'a> for AdminPurgeComment { - type InsertForm = AdminPurgeCommentForm; - type UpdateForm = AdminPurgeCommentForm; +impl Crud for AdminPurgeComment { + type InsertForm<'a> = &'a AdminPurgeCommentForm; + type UpdateForm<'a> = &'a AdminPurgeCommentForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { use crate::schema::admin_purge_comment::dsl::admin_purge_comment; let conn = &mut get_conn(pool).await?; insert_into(admin_purge_comment) @@ -461,10 +473,10 @@ impl<'a> Crud<'a> for AdminPurgeComment { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, from_id: i32, - form: &'a Self::InsertForm, + form: Self::InsertForm<'a>, ) -> Result { use crate::schema::admin_purge_comment::dsl::admin_purge_comment; let conn = &mut get_conn(pool).await?; diff --git a/crates/db_schema/src/impls/password_reset_request.rs b/crates/db_schema/src/impls/password_reset_request.rs index 54b914b2a..7cb709056 100644 --- a/crates/db_schema/src/impls/password_reset_request.rs +++ b/crates/db_schema/src/impls/password_reset_request.rs @@ -20,12 +20,12 @@ use diesel_async::RunQueryDsl; use sha2::{Digest, Sha256}; #[async_trait] -impl<'a> Crud<'a> for PasswordResetRequest { - type InsertForm = PasswordResetRequestForm; - type UpdateForm = PasswordResetRequestForm; +impl Crud for PasswordResetRequest { + type InsertForm<'a> = &'a PasswordResetRequestForm; + type UpdateForm<'a> = &'a PasswordResetRequestForm; type IdType = i32; - async fn create( + async fn create<'a>( pool: &mut DbPool<'_>, form: &'a PasswordResetRequestForm, ) -> Result { @@ -35,7 +35,7 @@ impl<'a> Crud<'a> for PasswordResetRequest { .get_result::(conn) .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, password_reset_request_id: i32, form: &'a PasswordResetRequestForm, diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index b30d98cdf..3f4739c9c 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -15,11 +15,11 @@ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, JoinOnDsl, Quer use diesel_async::RunQueryDsl; #[async_trait] -impl<'a> Crud<'a> for Person { - type InsertForm = PersonInsertForm; - type UpdateForm = PersonUpdateForm; +impl Crud for Person { + type InsertForm<'a> = &'a PersonInsertForm; + type UpdateForm<'a> = &'a PersonUpdateForm; type IdType = PersonId; - async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { + async fn read<'a>(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; person::table .filter(person::deleted.eq(false)) @@ -28,14 +28,14 @@ impl<'a> Crud<'a> for Person { .await } - async fn create(pool: &mut DbPool<'_>, form: &'a PersonInsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: &'a PersonInsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(person::table) .values(form) .get_result::(conn) .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, person_id: PersonId, form: &'a PersonUpdateForm, diff --git a/crates/db_schema/src/impls/person_mention.rs b/crates/db_schema/src/impls/person_mention.rs index 980549a38..4e2b91a0b 100644 --- a/crates/db_schema/src/impls/person_mention.rs +++ b/crates/db_schema/src/impls/person_mention.rs @@ -9,14 +9,14 @@ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; #[async_trait] -impl<'a> Crud<'a> for PersonMention { - type InsertForm = PersonMentionInsertForm; - type UpdateForm = PersonMentionUpdateForm; +impl Crud for PersonMention { + type InsertForm<'a> = &'a PersonMentionInsertForm; + type UpdateForm<'a> = &'a PersonMentionUpdateForm; type IdType = PersonMentionId; - async fn create( + async fn create<'a>( pool: &mut DbPool<'_>, - person_mention_form: &'a Self::InsertForm, + person_mention_form: Self::InsertForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; // since the return here isnt utilized, we dont need to do an update @@ -30,10 +30,10 @@ impl<'a> Crud<'a> for PersonMention { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, person_mention_id: PersonMentionId, - person_mention_form: &'a Self::UpdateForm, + person_mention_form: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(person_mention.find(person_mention_id)) diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index 072d5e849..e5e45acda 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -34,12 +34,12 @@ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextE use diesel_async::RunQueryDsl; #[async_trait] -impl<'a> Crud<'a> for Post { - type InsertForm = PostInsertForm; - type UpdateForm = PostUpdateForm; +impl Crud for Post { + type InsertForm<'a> = &'a PostInsertForm; + type UpdateForm<'a> = &'a PostUpdateForm; type IdType = PostId; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { let conn = &mut get_conn(pool).await?; insert_into(post) .values(form) @@ -50,10 +50,10 @@ impl<'a> Crud<'a> for Post { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, post_id: PostId, - new_post: &'a Self::UpdateForm, + new_post: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(post.find(post_id)) diff --git a/crates/db_schema/src/impls/private_message.rs b/crates/db_schema/src/impls/private_message.rs index beff32729..5d8d9df77 100644 --- a/crates/db_schema/src/impls/private_message.rs +++ b/crates/db_schema/src/impls/private_message.rs @@ -11,12 +11,12 @@ use lemmy_utils::error::LemmyError; use url::Url; #[async_trait] -impl<'a> Crud<'a> for PrivateMessage { - type InsertForm = PrivateMessageInsertForm; - type UpdateForm = PrivateMessageUpdateForm; +impl Crud for PrivateMessage { + type InsertForm<'a> = &'a PrivateMessageInsertForm; + type UpdateForm<'a> = &'a PrivateMessageUpdateForm; type IdType = PrivateMessageId; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { let conn = &mut get_conn(pool).await?; insert_into(private_message) .values(form) @@ -27,10 +27,10 @@ impl<'a> Crud<'a> for PrivateMessage { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, private_message_id: PrivateMessageId, - form: &'a Self::UpdateForm, + form: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(private_message.find(private_message_id)) diff --git a/crates/db_schema/src/impls/registration_application.rs b/crates/db_schema/src/impls/registration_application.rs index 85d8d21e0..ce58d52b5 100644 --- a/crates/db_schema/src/impls/registration_application.rs +++ b/crates/db_schema/src/impls/registration_application.rs @@ -13,12 +13,12 @@ use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; #[async_trait] -impl<'a> Crud<'a> for RegistrationApplication { - type InsertForm = RegistrationApplicationInsertForm; - type UpdateForm = RegistrationApplicationUpdateForm; +impl Crud for RegistrationApplication { + type InsertForm<'a> = &'a RegistrationApplicationInsertForm; + type UpdateForm<'a> = &'a RegistrationApplicationUpdateForm; type IdType = i32; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { let conn = &mut get_conn(pool).await?; insert_into(registration_application) .values(form) @@ -26,10 +26,10 @@ impl<'a> Crud<'a> for RegistrationApplication { .await } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, id_: Self::IdType, - form: &'a Self::UpdateForm, + form: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(registration_application.find(id_)) diff --git a/crates/db_schema/src/impls/site.rs b/crates/db_schema/src/impls/site.rs index b27ca2787..2ac4f8a70 100644 --- a/crates/db_schema/src/impls/site.rs +++ b/crates/db_schema/src/impls/site.rs @@ -13,12 +13,12 @@ use diesel_async::RunQueryDsl; use url::Url; #[async_trait] -impl<'a> Crud<'a> for Site { - type InsertForm = SiteInsertForm; - type UpdateForm = SiteUpdateForm; +impl Crud for Site { + type InsertForm<'a> = &'a SiteInsertForm; + type UpdateForm<'a> = &'a SiteUpdateForm; type IdType = SiteId; - async fn create(pool: &mut DbPool<'_>, form: &'a Self::InsertForm) -> Result { + async fn create<'a>(pool: &mut DbPool<'_>, form: Self::InsertForm<'a>) -> Result { let is_new_site = match &form.actor_id { Some(id_) => Site::read_from_apub_id(pool, id_).await?.is_none(), None => true, @@ -42,10 +42,10 @@ impl<'a> Crud<'a> for Site { Ok(site_) } - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, site_id: SiteId, - new_site: &'a Self::UpdateForm, + new_site: Self::UpdateForm<'a>, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(site.find(site_id)) diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index ff2a51bb5..1e8773453 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -28,10 +28,15 @@ use std::{hash::Hash, pin::Pin}; /// Returned by `diesel::delete` pub type Delete = DeleteStatement<::Table, ::WhereClause>; -pub type Find<'a, T> = dsl::Find<::Table, >::IdType>; +pub type Find = dsl::Find<::Table, ::IdType>; -pub type InsertValues<'a, 'b, T> = - <&'a >::InsertForm as Insertable<::Table>>::Values; +pub type InsertValues = +>::Values; + +pub trait InsertFormBound<'a, T:Table,>: 'a + Send + Sync + Copy + Insertable where +InsertValues: 'a, +InsertStatement>: + LoadQuery<'a, AsyncPgConnection, Self> + 'a + Send, // When using `RunQueryDsl::execute`, directly building futures with `Box::pin` and `TryFutureExt::and_then` // instead of `async` + `await` fixes weird compile errors. @@ -39,33 +44,29 @@ pub type InsertValues<'a, 'b, T> = // When using `RunQueryDsl::first` or 'RunQueryDsl::get_result`, `async` + `await` works, and it must be used otherwise the closure for `and_then` // will both own `conn` and return a future that references it. #[async_trait] -pub trait Crud<'a> +pub trait Crud where Self: HasTable + Sized + Send, - for<'b> Self::Table: FindDsl<>::IdType> + 'static, - for<'b> Find<'b, Self>: LimitDsl + Send + IntoUpdateTarget + 'static, - for<'b> dsl::Limit>: Send + LoadQuery<'static, AsyncPgConnection, Self> + 'static, + Self::Table: FindDsl + 'static, + Find: LimitDsl + Send + IntoUpdateTarget + 'static, + dsl::Limit>: Send + LoadQuery<'static, AsyncPgConnection, Self> + 'static, ::PrimaryKey: ExpressionMethods + Send, <::PrimaryKey as Expression>::SqlType: SqlType + TypedExpressionType, - for<'b> Delete>: ExecuteDsl + Send + 'static, - for<'b> as IntoUpdateTarget>::WhereClause: 'static + Send, - for<'b> as HasTable>::Table: 'static + Send, - for<'b> &'a >::InsertForm: Insertable, - for<'b> InsertValues<'a, 'b, Self>: 'a, - for<'b> InsertStatement>: - LoadQuery<'a, AsyncPgConnection, Self> + 'a + Send, + Delete>: ExecuteDsl + Send + 'static, + as IntoUpdateTarget>::WhereClause: 'static + Send, + as HasTable>::Table: 'static + Send, { - type InsertForm: 'static + Send + Sync; - type UpdateForm: 'static + Send + Sync; + type InsertForm<'a>: 'a + Send + Sync + Copy + Insertable; + type UpdateForm<'a>: 'a + Send + Sync + Copy; type IdType: 'static + Hash + Eq + Sized + Send + AsExpression<<::PrimaryKey as Expression>::SqlType>; - fn create<'life0, 'life1, 'async_trait>( + fn create<'a, 'life0, 'life1, 'async_trait>( pool: &'life0 mut DbPool<'life1>, - form: &'a Self::InsertForm, + form: Self::InsertForm<'a>, ) -> Pin> + Send + 'async_trait>> where 'a: 'async_trait, @@ -81,22 +82,17 @@ where .and_then(move |mut conn| async move { query.get_result::(&mut *conn).await }), ) } - async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result - where - 'a: 'async_trait, - { + async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { let query = Self::table().find(id); let conn = &mut *get_conn(pool).await?; query.first::(conn).await } /// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column. - async fn update( + async fn update<'a>( pool: &mut DbPool<'_>, id: Self::IdType, - form: &'a Self::UpdateForm, - ) -> Result - where - 'a: 'async_trait; + form: Self::UpdateForm<'a>, + ) -> Result; /*{ let conn = &mut get_conn(pool).await?; diesel::update(Self::table().find(id)) @@ -109,12 +105,11 @@ where id: Self::IdType, ) -> Pin> + Send + 'async_trait>> where - 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, Self: Send + 'async_trait, { - let query: Delete> = diesel::delete(Self::table().find(id)); + let query: Delete> = diesel::delete(Self::table().find(id)); Box::pin(get_conn(pool).and_then(move |mut conn| query.execute(&mut *conn))) } }