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:
		@@ -1,5 +1,5 @@
 | 
			
		||||
use heromodels::db::{Collection, Db};
 | 
			
		||||
use heromodels::models::userexample::user::{IsActive, UserName};
 | 
			
		||||
use heromodels::models::userexample::user::index::{is_active, username};
 | 
			
		||||
use heromodels::models::{Comment, Model, User};
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
@@ -64,7 +64,7 @@ fn main() {
 | 
			
		||||
    let stored_users = db
 | 
			
		||||
        .collection::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .get::<UserName>("johndoe")
 | 
			
		||||
        .get::<username, _>("johndoe")
 | 
			
		||||
        .expect("can load stored user");
 | 
			
		||||
 | 
			
		||||
    assert_eq!(stored_users.len(), 1);
 | 
			
		||||
@@ -80,7 +80,7 @@ fn main() {
 | 
			
		||||
    let active_users = db
 | 
			
		||||
        .collection::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .get::<IsActive>(&true)
 | 
			
		||||
        .get::<is_active, _>(&true)
 | 
			
		||||
        .expect("can load stored users");
 | 
			
		||||
    // We should have 2 active users
 | 
			
		||||
    assert_eq!(active_users.len(), 2);
 | 
			
		||||
@@ -95,14 +95,14 @@ fn main() {
 | 
			
		||||
    let active_users = db
 | 
			
		||||
        .collection::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .get::<IsActive>(&true)
 | 
			
		||||
        .get::<is_active, _>(&true)
 | 
			
		||||
        .expect("can load stored users");
 | 
			
		||||
    assert_eq!(active_users.len(), 1);
 | 
			
		||||
    // And verify we still have 2 inactive users
 | 
			
		||||
    let inactive_users = db
 | 
			
		||||
        .collection::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .get::<IsActive>(&false)
 | 
			
		||||
        .get::<is_active, _>(&false)
 | 
			
		||||
        .expect("can load stored users");
 | 
			
		||||
    assert_eq!(inactive_users.len(), 2);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,37 +1,37 @@
 | 
			
		||||
use heromodels::model;
 | 
			
		||||
use heromodels::models::core::model::{BaseModelData, Model, Index, IndexKey};
 | 
			
		||||
use serde::{Serialize, Deserialize};
 | 
			
		||||
use heromodels::models::core::model::{BaseModelData, Model};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
 | 
			
		||||
// Define a custom attribute for indexing
 | 
			
		||||
#[derive(Debug, Clone, Serialize, Deserialize)]
 | 
			
		||||
#[model]
 | 
			
		||||
pub struct CustomUser {
 | 
			
		||||
    pub base_data: BaseModelData,
 | 
			
		||||
    
 | 
			
		||||
    // Mark fields for indexing with a comment
 | 
			
		||||
    // #[index]
 | 
			
		||||
 | 
			
		||||
    // Mark fields for indexing with attributes
 | 
			
		||||
    #[index]
 | 
			
		||||
    pub login: String,
 | 
			
		||||
    
 | 
			
		||||
    // #[index]
 | 
			
		||||
 | 
			
		||||
    #[index]
 | 
			
		||||
    pub is_active: bool,
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    pub full_name: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    println!("Hero Models - Custom Model Example");
 | 
			
		||||
    println!("==================================");
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    // Example usage of the generated implementation
 | 
			
		||||
    println!("CustomUser DB Prefix: {}", CustomUser::db_prefix());
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    let user = CustomUser {
 | 
			
		||||
        base_data: BaseModelData::new(1),
 | 
			
		||||
        login: "johndoe".to_string(),
 | 
			
		||||
        is_active: true,
 | 
			
		||||
        full_name: "John Doe".to_string(),
 | 
			
		||||
    };
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    println!("\nCustomUser ID: {}", user.get_id());
 | 
			
		||||
    println!("CustomUser DB Keys: {:?}", user.db_keys());
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ use serde::{Serialize, Deserialize};
 | 
			
		||||
pub struct SimpleUser {
 | 
			
		||||
    pub base_data: BaseModelData,
 | 
			
		||||
    
 | 
			
		||||
    /// @index
 | 
			
		||||
    #[index]
 | 
			
		||||
    pub login: String,
 | 
			
		||||
    
 | 
			
		||||
    pub full_name: String,
 | 
			
		||||
@@ -20,10 +20,10 @@ pub struct SimpleUser {
 | 
			
		||||
pub struct CustomUser {
 | 
			
		||||
    pub base_data: BaseModelData,
 | 
			
		||||
    
 | 
			
		||||
    /// @index(name = "user_name", key_type = "str")
 | 
			
		||||
    #[index(name = "user_name")]
 | 
			
		||||
    pub login_name: String,
 | 
			
		||||
    
 | 
			
		||||
    /// @index(key_type = "bool")
 | 
			
		||||
    #[index]
 | 
			
		||||
    pub is_active: bool,
 | 
			
		||||
    
 | 
			
		||||
    pub full_name: String,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user