db/heromodels
Lee Smet 0a8c5d1c1f
Expand Index struct to also get the original field name
This is needed in postgres, since we store data as jsonb so struct
fields can't get renamed

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
2025-07-31 12:12:06 +02:00
..
docs merge branches and cleanup db 2025-06-27 12:11:04 +03:00
examples Improve postgres example code style 2025-07-31 11:21:33 +02:00
src Expand Index struct to also get the original field name 2025-07-31 12:12:06 +02:00
tests feat: Add new Rhai example demonstrating payment flow 2025-06-25 18:39:47 +03:00
.DS_Store merge branches and cleanup db 2025-06-27 12:11:04 +03:00
.gitignore improve model rhai scripts legibility 2025-05-22 17:55:08 +03:00
Cargo.lock Add postgres code 2025-07-30 16:31:40 +02:00
Cargo.toml Add postgres code 2025-07-30 16:31:40 +02:00
README.md ... 2025-04-22 08:24:17 +04:00
run_all_examples.sh test: Add shell script to run all examples 2025-05-17 16:10:37 +03:00

Hero Models

A Rust library for model management with a base model trait implementation and builder pattern support.

Features

  • Base Model Trait: Common interface for all models with standard methods
  • Builder Pattern: All models use the builder pattern for flexible and clear construction
  • Indexing Support: Built-in support for model indexing with customizable keys
  • Comment System: Integrated comment system that can be attached to any model
  • Timestamps: Automatic creation and modification timestamps

Usage

Add this to your Cargo.toml:

[dependencies]
heromodels = "0.1.0"

Basic Example

use heromodels::{BaseModel, User, Comment};

fn main() {
    // Create a new user using the builder pattern
    let user_result = User::builder(1)
        .username("johndoe")
        .email("john.doe@example.com")
        .full_name("John Doe")
        .build();
        
    let mut user = user_result.expect("Failed to build user");
    
    // Create a comment for the user using the builder pattern
    let comment_result = Comment::builder(1)
        .user_id(2) // commenter's user ID
        .model_id(user.get_id())
        .model_type(User::db_prefix())
        .content("This is a comment on the user")
        .build();
        
    let comment = comment_result.expect("Failed to build comment");
    
    // Add the comment to the user
    user.base_data.add_comment(comment.get_id());
    
    // Get the database keys for the user
    let keys = user.db_keys();
    for key in keys {
        println!("{}: {}", key.name, key.value);
    }
}

Creating Custom Models

To create your own model that inherits from the base model:

use serde::{Deserialize, Serialize};
use heromodels::model::{BaseModel, BaseModelData, IndexKey};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Product {
    pub base_data: BaseModelData,
    pub name: String,
    pub price: f64,
    pub sku: String,
}

impl BaseModel for Product {
    fn db_prefix() -> &'static str {
        "product"
    }
    
    fn get_id(&self) -> u32 {
        self.base_data.id
    }
    
    fn db_keys(&self) -> Vec<IndexKey> {
        vec![
            IndexKey {
                name: "name",
                value: self.name.clone(),
            },
            IndexKey {
                name: "sku",
                value: self.sku.clone(),
            },
        ]
    }
}

// Implement a builder for your custom model
pub struct ProductBuilder {
    id: u32,
    base_data_builder: Option<heromodels::model::BaseModelDataBuilder>,
    name: Option<String>,
    price: Option<f64>,
    sku: Option<String>,
}

impl ProductBuilder {
    pub fn new(id: u32) -> Self {
        Self {
            id,
            base_data_builder: Some(BaseModelData::builder(id)),
            name: None,
            price: None,
            sku: None,
        }
    }
    
    // Add builder methods...
    
    pub fn build(self) -> Result<Product, &'static str> {
        // Implementation...
    }
}

impl Product {
    pub fn builder(id: u32) -> ProductBuilder {
        ProductBuilder::new(id)
    }
}

License

MIT