1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX) ┃
// ┃ SPDX-License-Identifier: MPL-2.0 ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ ┃
// ┃ This Source Code Form is subject to the terms of the Mozilla Public ┃
// ┃ License, v. 2.0. If a copy of the MPL was not distributed with this ┃
// ┃ file, You can obtain one at https://mozilla.org/MPL/2.0/. ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
#[cfg(feature = "database-postgres")]
mod postgres;
#[cfg(feature = "database-postgres")]
pub use self::postgres::ModelPostgresInterface;
use super::error::Error;
// --------- //
// Interface //
// --------- //
#[async_trait::async_trait]
pub trait ModelInterface {
/// Base de donnée.
type Database: lexa_database::SGBD;
/// Entité du modèle.
type Entity: Unpin
+ Send
+ Sync
+ serde::ser::Serialize
+ serde::de::DeserializeOwned;
/// Crée une instance du [model](Self).
fn new(database: &Self::Database) -> Self
where
Self: Sized;
fn database(&self) -> &Self::Database;
/// Nom de la table du model, correspondant à un nom de table d'une base de
/// données.
fn table() -> &'static str;
/// Tous les champs d'une entité.
fn fields() -> &'static [&'static str];
/// Récupère tous les résultats de la table [Self::table()].
async fn fetch_all<'c>(&self) -> Result<Vec<Self::Entity>, Error>;
/// Récupère tous les résultats de la table [Self::table()] en fonction d'un
/// ID.
async fn fetch_all_by_id(
&self,
id: impl ToString + Send + Sync,
) -> Result<Vec<Self::Entity>, Error>;
/// Cherche dans la table de la base de donnée s'il existe l'ID passé en
/// argument. En supposant qu'il existe un champ `id` dans la table.
async fn find_by_id(
&self,
id: impl ToString + Send + Sync,
) -> Result<Self::Entity, Error>;
/// Ajoute une entité en base de données.
async fn create(
&self,
data: impl serde::ser::Serialize + Send + Sync,
) -> Result<Self::Entity, Error>;
/// Supprime une entité en base de données en fonction d'un ID.
async fn delete_by_id(
&self,
id: impl ToString + Send + Sync,
) -> Result<(), Error>;
/// Modifie les champs d'une table en base de données.
async fn update_by_id(
&self,
id: impl ToString + Send + Sync,
data: impl serde::ser::Serialize + Send + Sync,
) -> Result<Self::Entity, Error>;
}