Add alternative DB interface and implementation
Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
@@ -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;
|
@@ -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 {
|
||||
|
@@ -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;
|
@@ -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"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user