Add alternative DB interface and implementation

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet
2025-04-23 13:53:10 +02:00
parent 1708e6dd06
commit 276cf3c8d8
11 changed files with 1099 additions and 17 deletions

View File

@@ -3,5 +3,5 @@ pub mod model;
pub mod comment;
// Re-export key types for convenience
pub use model::{Model, BaseModelData, IndexKey, IndexKeyBuilder};
pub use model::{Model, BaseModelData, IndexKey, IndexKeyBuilder, Index};
pub use comment::Comment;

View File

@@ -73,6 +73,15 @@ pub trait Model: Debug + Clone + Serialize + for<'de> Deserialize<'de> + Send +
}
}
/// An identifier for an index in the DB
pub trait Index {
/// The model for which this is an index in the database
type Model: Model;
/// The key of this index
fn key() -> &'static str;
}
/// Base struct that all models should include
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseModelData {

View File

@@ -3,6 +3,6 @@ pub mod core;
pub mod userexample;
// Re-export key types for convenience
pub use core::model::{Model, BaseModelData, IndexKey};
pub use core::model::{Model, BaseModelData, IndexKey, Index};
pub use core::Comment;
pub use userexample::User;

View File

@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use crate::models::core::model::{Model, BaseModelData, IndexKey};
use crate::models::core::model::{Model, BaseModelData, IndexKey, Index};
/// Represents a user in the system
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -109,3 +109,33 @@ impl Model for User {
]
}
}
// Marker structs for indexed fields
pub struct UserName;
pub struct Email;
pub struct IsActive;
impl Index for UserName {
type Model = User;
fn key() -> &'static str {
"username"
}
}
impl Index for Email {
type Model = User;
fn key() -> &'static str {
"email"
}
}
impl Index for IsActive {
type Model = User;
fn key() -> &'static str {
"is_active"
}
}