Update macro to use #[index] attributes

- Also use proper types for index.
 - Update DB interface to be more flexible for index params

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet
2025-04-25 13:26:15 +02:00
parent dc93518a35
commit 96a1ecd974
10 changed files with 368 additions and 167 deletions

View File

@@ -1,22 +1,27 @@
use crate::models::core::model::{BaseModelData, Index, IndexKey, Model};
use crate::models::core::model::BaseModelData;
use heromodels_derive::model;
use serde::{Deserialize, Serialize};
/// Represents a user in the system
#[derive(Debug, Clone, Serialize, Deserialize)]
#[model]
pub struct User {
/// Base model data
pub base_data: BaseModelData,
/// User's username
#[index]
pub username: String,
/// User's email address
#[index]
pub email: String,
/// User's full name
pub full_name: String,
/// Whether the user is active
#[index(name = "ac")]
pub is_active: bool,
}
@@ -78,70 +83,70 @@ impl User {
}
// Implement the Model trait for User
impl Model for User {
fn db_prefix() -> &'static str {
"user"
}
fn get_id(&self) -> u32 {
self.base_data.id
}
//WHY?
fn base_data_mut(&mut self) -> &mut BaseModelData {
&mut self.base_data
}
fn db_keys(&self) -> Vec<IndexKey> {
vec![
IndexKey {
name: "username",
value: self.username.clone(),
},
IndexKey {
name: "email",
value: self.email.clone(),
},
IndexKey {
name: "is_active",
value: self.is_active.to_string(),
},
]
}
}
// Marker structs for indexed fields
pub struct UserName;
pub struct Email;
pub struct IsActive;
impl Index for UserName {
type Model = User;
type Key = str;
fn key() -> &'static str {
"username"
}
}
impl Index for Email {
type Model = User;
type Key = str;
fn key() -> &'static str {
"email"
}
}
impl Index for IsActive {
type Model = User;
type Key = bool;
fn key() -> &'static str {
"is_active"
}
}
// impl Model for User {
// fn db_prefix() -> &'static str {
// "user"
// }
//
// fn get_id(&self) -> u32 {
// self.base_data.id
// }
//
// //WHY?
// fn base_data_mut(&mut self) -> &mut BaseModelData {
// &mut self.base_data
// }
//
// fn db_keys(&self) -> Vec<IndexKey> {
// vec![
// IndexKey {
// name: "username",
// value: self.username.clone(),
// },
// IndexKey {
// name: "email",
// value: self.email.clone(),
// },
// IndexKey {
// name: "is_active",
// value: self.is_active.to_string(),
// },
// ]
// }
// }
//
// // Marker structs for indexed fields
//
// pub struct UserName;
// pub struct Email;
// pub struct IsActive;
//
// impl Index for UserName {
// type Model = User;
//
// type Key = str;
//
// fn key() -> &'static str {
// "username"
// }
// }
//
// impl Index for Email {
// type Model = User;
//
// type Key = str;
//
// fn key() -> &'static str {
// "email"
// }
// }
//
// impl Index for IsActive {
// type Model = User;
//
// type Key = bool;
//
// fn key() -> &'static str {
// "is_active"
// }
// }