...
This commit is contained in:
		
							
								
								
									
										83
									
								
								herodb/examples/minimal_ourdb_example.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								herodb/examples/minimal_ourdb_example.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
use herodb::db::{DB, DBBuilder, Model};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use chrono::Utc;
 | 
			
		||||
 | 
			
		||||
// Define a simple Product model
 | 
			
		||||
#[derive(Debug, Clone, Serialize, Deserialize)]
 | 
			
		||||
struct SimpleProduct {
 | 
			
		||||
    id: u32,
 | 
			
		||||
    name: String,
 | 
			
		||||
    description: String,
 | 
			
		||||
    price: f64,
 | 
			
		||||
    created_at: chrono::DateTime<Utc>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Implement the Model trait for SimpleProduct
 | 
			
		||||
impl Model for SimpleProduct {
 | 
			
		||||
    fn get_id(&self) -> u32 {
 | 
			
		||||
        self.id
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn db_prefix() -> &'static str {
 | 
			
		||||
        "simple_product"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
    println!("OurDB Minimal Example");
 | 
			
		||||
    println!("====================\n");
 | 
			
		||||
 | 
			
		||||
    // Create a temporary directory for the database
 | 
			
		||||
    let db_path = std::env::temp_dir().join("herodb_minimal_example");
 | 
			
		||||
    std::fs::create_dir_all(&db_path)?;
 | 
			
		||||
    
 | 
			
		||||
    println!("Creating database at: {}", db_path.display());
 | 
			
		||||
    
 | 
			
		||||
    // Create a new database with SimpleProduct model registered
 | 
			
		||||
    let db = DBBuilder::new(db_path.clone())
 | 
			
		||||
        .register_model::<SimpleProduct>()
 | 
			
		||||
        .build()?;
 | 
			
		||||
    
 | 
			
		||||
    println!("Database created successfully");
 | 
			
		||||
    
 | 
			
		||||
    // Create a product
 | 
			
		||||
    let product = SimpleProduct {
 | 
			
		||||
        id: 1,
 | 
			
		||||
        name: "Test Product".to_string(),
 | 
			
		||||
        description: "A test product for our minimal OurDB example".to_string(),
 | 
			
		||||
        price: 99.99,
 | 
			
		||||
        created_at: Utc::now(),
 | 
			
		||||
    };
 | 
			
		||||
    
 | 
			
		||||
    println!("\nCreated product: {}", product.name);
 | 
			
		||||
    println!("Product ID: {}", product.get_id());
 | 
			
		||||
    
 | 
			
		||||
    // Insert the product into the database
 | 
			
		||||
    db.set(&product)?;
 | 
			
		||||
    println!("Product saved to database");
 | 
			
		||||
    
 | 
			
		||||
    // Retrieve the product from the database
 | 
			
		||||
    let retrieved_product = db.get::<SimpleProduct>(product.get_id())?;
 | 
			
		||||
    println!("\nRetrieved product from database:");
 | 
			
		||||
    println!("  Name: {}", retrieved_product.name);
 | 
			
		||||
    println!("  Description: {}", retrieved_product.description);
 | 
			
		||||
    println!("  Price: ${}", retrieved_product.price);
 | 
			
		||||
    
 | 
			
		||||
    // Delete the product
 | 
			
		||||
    db.delete::<SimpleProduct>(product.get_id())?;
 | 
			
		||||
    println!("\nProduct deleted from database");
 | 
			
		||||
    
 | 
			
		||||
    // Try to retrieve the deleted product (should fail)
 | 
			
		||||
    match db.get::<SimpleProduct>(product.get_id()) {
 | 
			
		||||
        Ok(_) => println!("Product still exists (unexpected)"),
 | 
			
		||||
        Err(e) => println!("Verified deletion: {}", e),
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    println!("\nExample completed successfully!");
 | 
			
		||||
    
 | 
			
		||||
    // Clean up
 | 
			
		||||
    std::fs::remove_dir_all(&db_path)?;
 | 
			
		||||
    println!("Cleaned up database directory");
 | 
			
		||||
    
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
@@ -21,6 +21,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
    
 | 
			
		||||
    // Create a currency for pricing
 | 
			
		||||
    let usd = CurrencyBuilder::new()
 | 
			
		||||
        .id(1) // Add an ID for the currency
 | 
			
		||||
        .amount(99.99)
 | 
			
		||||
        .currency_code("USD")
 | 
			
		||||
        .build()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user