]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/traits.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / db_schema / src / traits.rs
index adbbf8026b1124f21f0a2997bfb7a39567328f01..0ba0cbff642d54d7f7f3b9e782a66938dba36c47 100644 (file)
@@ -9,17 +9,21 @@ pub trait Crud {
   type InsertForm;
   type UpdateForm;
   type IdType;
-  async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error>
+  async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>
   where
     Self: Sized;
-  async fn read(pool: &DbPool, id: Self::IdType) -> Result<Self, Error>
+  async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error>
   where
     Self: Sized;
   /// 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(pool: &DbPool, id: Self::IdType, form: &Self::UpdateForm) -> Result<Self, Error>
+  async fn update(
+    pool: &mut DbPool<'_>,
+    id: Self::IdType,
+    form: &Self::UpdateForm,
+  ) -> Result<Self, Error>
   where
     Self: Sized;
-  async fn delete(_pool: &DbPool, _id: Self::IdType) -> Result<usize, Error>
+  async fn delete(_pool: &mut DbPool<'_>, _id: Self::IdType) -> Result<usize, Error>
   where
     Self: Sized,
     Self::IdType: Send,
@@ -31,17 +35,17 @@ pub trait Crud {
 #[async_trait]
 pub trait Followable {
   type Form;
-  async fn follow(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
+  async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
   async fn follow_accepted(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     community_id: CommunityId,
     person_id: PersonId,
   ) -> Result<Self, Error>
   where
     Self: Sized;
-  async fn unfollow(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
+  async fn unfollow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
@@ -49,10 +53,10 @@ pub trait Followable {
 #[async_trait]
 pub trait Joinable {
   type Form;
-  async fn join(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
+  async fn join(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  async fn leave(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
+  async fn leave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
@@ -61,11 +65,11 @@ pub trait Joinable {
 pub trait Likeable {
   type Form;
   type IdType;
-  async fn like(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
+  async fn like(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
   async fn remove(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     person_id: PersonId,
     item_id: Self::IdType,
   ) -> Result<usize, Error>
@@ -76,10 +80,10 @@ pub trait Likeable {
 #[async_trait]
 pub trait Bannable {
   type Form;
-  async fn ban(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
+  async fn ban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  async fn unban(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
+  async fn unban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
@@ -87,10 +91,10 @@ pub trait Bannable {
 #[async_trait]
 pub trait Saveable {
   type Form;
-  async fn save(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
+  async fn save(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  async fn unsave(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
+  async fn unsave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
@@ -98,10 +102,10 @@ pub trait Saveable {
 #[async_trait]
 pub trait Blockable {
   type Form;
-  async fn block(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
+  async fn block(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  async fn unblock(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
+  async fn unblock(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
@@ -109,10 +113,10 @@ pub trait Blockable {
 #[async_trait]
 pub trait Readable {
   type Form;
-  async fn mark_as_read(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
+  async fn mark_as_read(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  async fn mark_as_unread(pool: &DbPool, form: &Self::Form) -> Result<usize, Error>
+  async fn mark_as_unread(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
@@ -121,18 +125,18 @@ pub trait Readable {
 pub trait Reportable {
   type Form;
   type IdType;
-  async fn report(pool: &DbPool, form: &Self::Form) -> Result<Self, Error>
+  async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
   async fn resolve(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     report_id: Self::IdType,
     resolver_id: PersonId,
   ) -> Result<usize, Error>
   where
     Self: Sized;
   async fn unresolve(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     report_id: Self::IdType,
     resolver_id: PersonId,
   ) -> Result<usize, Error>
@@ -140,40 +144,32 @@ pub trait Reportable {
     Self: Sized;
 }
 
-pub trait ToSafe {
-  type SafeColumns;
-  fn safe_columns_tuple() -> Self::SafeColumns;
-}
-
-pub trait ToSafeSettings {
-  type SafeSettingsColumns;
-  fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns;
-}
-
-pub trait ViewToVec {
-  type DbTuple;
-  fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
+pub trait JoinView {
+  type JoinTuple;
+  fn from_tuple(tuple: Self::JoinTuple) -> Self
   where
     Self: Sized;
 }
 
 #[async_trait]
 pub trait ApubActor {
-  // TODO: this should be in a trait ApubObject (and implemented for Post, Comment, PrivateMessage as well)
-  async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error>
+  async fn read_from_apub_id(
+    pool: &mut DbPool<'_>,
+    object_id: &DbUrl,
+  ) -> Result<Option<Self>, Error>
   where
     Self: Sized;
   /// - actor_name is the name of the community or user to read.
   /// - include_deleted, if true, will return communities or users that were deleted/removed
   async fn read_from_name(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     actor_name: &str,
     include_deleted: bool,
   ) -> Result<Self, Error>
   where
     Self: Sized;
   async fn read_from_name_and_domain(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     actor_name: &str,
     protocol_domain: &str,
   ) -> Result<Self, Error>