articles_rs/databases/
postgres_repository.rs

1use async_trait::async_trait;
2use std::collections::HashMap;
3use uuid::Uuid;
4
5/// Repository trait for PostgreSQL database operations
6///
7/// This trait defines a common interface for database repositories that handle
8/// CRUD operations and queries on PostgreSQL tables.
9#[async_trait]
10pub trait PostgresRepository {
11    /// The error type returned by repository operations
12    type Error;
13    /// The database model type this repository handles
14    type Item;
15
16    /// Finds a single item by its UUID
17    ///
18    /// # Arguments
19    /// * `id` - The UUID to search for
20    ///
21    /// # Returns
22    /// The found item wrapped in Some, or None if not found
23    async fn find_by_id(&self, id: Uuid) -> Result<Option<Self::Item>, Self::Error>;
24
25    /// Finds a single item by its name/slug
26    ///
27    /// # Arguments
28    /// * `name` - The name or slug to search for
29    ///
30    /// # Returns
31    /// The found item wrapped in Some, or None if not found
32    async fn find_by_name(&self, name: String) -> Result<Option<Self::Item>, Self::Error>;
33
34    /// Lists items matching optional filters
35    ///
36    /// # Arguments
37    /// * `filters` - Optional HashMap of column names and values to filter by
38    ///
39    /// # Returns
40    /// Vector of items matching the filters
41    async fn list(
42        &self,
43        filters: Option<HashMap<String, String>>,
44    ) -> Result<Vec<Self::Item>, Self::Error>;
45
46    /// Creates a new item in the database
47    ///
48    /// # Arguments
49    /// * `item` - The item to create
50    ///
51    /// # Returns
52    /// The UUID of the created item
53    async fn create(&self, item: &Self::Item) -> Result<Uuid, Self::Error>;
54
55    /// Deletes an item by its UUID
56    ///
57    /// # Arguments
58    /// * `id` - The UUID of the item to delete
59    async fn delete(&self, id: Uuid) -> Result<(), Self::Error>;
60
61    /// Deletes multiple items matching optional filters
62    ///
63    /// # Arguments
64    /// * `filters` - Optional HashMap of column names and values to filter deletions by
65    async fn delete_all(&self, filters: Option<HashMap<String, String>>)
66        -> Result<(), Self::Error>;
67
68    /// Updates an existing item
69    ///
70    /// # Arguments
71    /// * `item` - The item with updated values
72    async fn update(&self, item: &Self::Item) -> Result<(), Self::Error>;
73}