fix: Use incremental ID

This commit is contained in:
Mahmoud Emad
2025-05-17 13:46:16 +03:00
parent a676854251
commit 57f59da43e
10 changed files with 519 additions and 156 deletions

View File

@@ -1,3 +1,4 @@
use heromodels::db::{Collection, Db};
use heromodels_core::{BaseModelData, Model};
use heromodels_derive::model;
use serde::{Deserialize, Serialize};
@@ -15,16 +16,36 @@ fn main() {
println!("Hero Models - Simple Model Example");
println!("==================================");
// Create a new DB instance, reset before every run
let db_path = "/tmp/ourdb_simple_model_example";
let db = heromodels::db::hero::OurDB::new(db_path, true).expect("Can create DB");
// Example usage of the generated implementation
println!("SimpleUser DB Prefix: {}", SimpleUser::db_prefix());
// Create a new user with ID 0 (will be auto-generated when saved)
let user = SimpleUser {
base_data: BaseModelData::new(),
login: "johndoe".to_string(),
full_name: "John Doe".to_string(),
};
println!("\nSimpleUser ID: {}", user.get_id());
println!("SimpleUser DB Keys: {:?}", user.db_keys());
println!("\nBefore saving - SimpleUser ID: {}", user.get_id());
println!("Before saving - SimpleUser DB Keys: {:?}", user.db_keys());
// Save the user to the database
let collection = db.collection::<SimpleUser>().expect("can open user collection");
let (user_id, saved_user) = collection.set(&user).expect("can save user");
println!("\nAfter saving - SimpleUser ID: {}", saved_user.get_id());
println!("After saving - SimpleUser DB Keys: {:?}", saved_user.db_keys());
println!("Returned ID: {}", user_id);
// Verify that the ID was auto-generated
assert_eq!(saved_user.get_id(), user_id);
assert_ne!(saved_user.get_id(), 0);
println!("\nExample finished. DB stored at {}", db_path);
println!("To clean up, you can manually delete the directory: {}", db_path);
}