2024-08-19 19:40:14 +02:00
|
|
|
mod error;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use diesel::{AsChangeset, Insertable};
|
2024-08-24 19:29:54 +02:00
|
|
|
use diesel_async::AsyncPgConnection;
|
|
|
|
pub use error::CrudError;
|
2024-08-19 19:40:14 +02:00
|
|
|
|
2024-08-24 19:29:54 +02:00
|
|
|
/// Combines all CRUD operations into a single trait
|
|
|
|
/// Includes:
|
|
|
|
/// - Create
|
|
|
|
/// - Read
|
|
|
|
/// - Update
|
|
|
|
/// - Delete
|
|
|
|
/// - List
|
|
|
|
pub trait DieselCrud<Table>:
|
|
|
|
DieselCrudCreate<Table> + DieselCrudRead + DieselCrudUpdate + DieselCrudDelete + DieselCrudList
|
2024-08-19 19:40:14 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert an entity into the database
|
|
|
|
/// The entity must implement `Insertable<Table>` for the given table.
|
|
|
|
///
|
|
|
|
/// Implementing the trait requires the `async_trait` macro.
|
|
|
|
/// # Associations
|
2024-08-24 19:29:54 +02:00
|
|
|
/// - `Insert` - The type to insert, must implement `Insertable<Table>`
|
2024-08-19 19:40:14 +02:00
|
|
|
/// # Parameters
|
2024-08-24 19:29:54 +02:00
|
|
|
/// - `insert` - The entity to insert
|
|
|
|
/// - `conn` - The database connection
|
2024-08-19 19:40:14 +02:00
|
|
|
/// # Returns
|
|
|
|
/// A result containing the inserted entity or a `CrudError`
|
|
|
|
#[async_trait]
|
2024-08-24 19:29:54 +02:00
|
|
|
pub trait DieselCrudCreate<Table>
|
2024-08-19 19:40:14 +02:00
|
|
|
where
|
2024-08-24 19:29:54 +02:00
|
|
|
Self: Sized,
|
2024-08-19 19:40:14 +02:00
|
|
|
{
|
2024-08-24 19:29:54 +02:00
|
|
|
type Insert: Insertable<Table>;
|
|
|
|
async fn create(insert: Self::Insert, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
|
2024-08-19 19:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets an entity from the database
|
|
|
|
///
|
|
|
|
/// Implementing the trait requires the `async_trait` macro.
|
|
|
|
/// # Associations
|
|
|
|
/// - `PK` - The primary key of the entity
|
|
|
|
/// # Parameters
|
|
|
|
/// - `pk` - The primary key of the entity
|
2024-08-24 19:29:54 +02:00
|
|
|
/// - `conn` - The database connection
|
2024-08-19 19:40:14 +02:00
|
|
|
/// # Returns
|
|
|
|
/// A result containing the entity or a `CrudError`.
|
|
|
|
/// If the entity is not found, the error should be `CrudError::NotFound`.
|
|
|
|
#[async_trait]
|
2024-08-24 19:29:54 +02:00
|
|
|
pub trait DieselCrudRead
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2024-08-19 19:40:14 +02:00
|
|
|
type PK;
|
2024-08-24 19:29:54 +02:00
|
|
|
async fn read(pk: Self::PK, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
|
2024-08-19 19:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates an entity in the database
|
|
|
|
/// The entity must implement `AsChangeset` for the given table.
|
|
|
|
///
|
|
|
|
/// Implementing the trait requires the `async_trait` macro.
|
|
|
|
/// # Associations
|
|
|
|
/// - `Update` - The type to update
|
|
|
|
/// # Parameters
|
|
|
|
/// - `update` - The update to apply
|
2024-08-24 19:29:54 +02:00
|
|
|
/// - `conn` - The database connection
|
2024-08-19 19:40:14 +02:00
|
|
|
/// # Returns
|
2024-08-24 19:29:54 +02:00
|
|
|
/// A result containing the old entry of the entity if successful or a `CrudError`.
|
2024-08-19 19:40:14 +02:00
|
|
|
/// If the entity is not found, the error should be `CrudError::NotFound`.
|
|
|
|
#[async_trait]
|
2024-08-24 19:29:54 +02:00
|
|
|
pub trait DieselCrudUpdate
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2024-08-19 19:40:14 +02:00
|
|
|
type Update: AsChangeset;
|
2024-08-24 19:29:54 +02:00
|
|
|
async fn update(update: Self::Update, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
|
2024-08-19 19:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Deletes an entity from the database
|
|
|
|
///
|
|
|
|
/// Implementing the trait requires the `async_trait` macro.
|
|
|
|
/// # Associations
|
|
|
|
/// - `PK` - The primary key of the entity
|
|
|
|
/// # Parameters
|
|
|
|
/// - `pk` - The primary key of the entity
|
2024-08-24 19:29:54 +02:00
|
|
|
/// - `conn` - The database connection
|
2024-08-19 19:40:14 +02:00
|
|
|
/// # Returns
|
2024-08-24 19:29:54 +02:00
|
|
|
/// A result containing the deleted entity or a `CrudError`.
|
2024-08-19 19:40:14 +02:00
|
|
|
/// If the entity is not found, the error should be `CrudError::NotFound`.
|
|
|
|
#[async_trait]
|
2024-08-24 19:29:54 +02:00
|
|
|
pub trait DieselCrudDelete
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2024-08-19 19:40:14 +02:00
|
|
|
type PK;
|
2024-08-24 19:29:54 +02:00
|
|
|
async fn delete(pk: Self::PK, conn: &mut AsyncPgConnection) -> Result<Self, CrudError>;
|
2024-08-19 19:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Lists all entities in the table
|
|
|
|
///
|
|
|
|
/// Implementing the trait requires the `async_trait` macro.
|
2024-08-24 19:29:54 +02:00
|
|
|
/// # Parameters
|
|
|
|
/// - `conn` - The database connection
|
2024-08-19 19:40:14 +02:00
|
|
|
/// # Returns
|
|
|
|
/// A result containing a Vec of entities or a `CrudError`.
|
|
|
|
#[async_trait]
|
2024-08-24 19:29:54 +02:00
|
|
|
pub trait DieselCrudList
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
async fn list(conn: &mut AsyncPgConnection) -> Result<Vec<Self>, CrudError>;
|
2024-08-19 19:40:14 +02:00
|
|
|
}
|