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};
@@ -22,16 +23,35 @@ fn main() {
println!("Hero Models - Custom Model Example");
println!("==================================");
// Create a new DB instance, reset before every run
let db_path = "/tmp/ourdb_custom_model_example";
let db = heromodels::db::hero::OurDB::new(db_path, true).expect("Can create DB");
// Example usage of the generated implementation
println!("CustomUser DB Prefix: {}", CustomUser::db_prefix());
let user = CustomUser {
base_data: BaseModelData::new(1),
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
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());
println!("\nBefore saving - CustomUser ID: {}", user.get_id());
println!("Before saving - CustomUser DB Keys: {:?}", user.db_keys());
// Save the model to the database
let collection = db.collection::<CustomUser>().expect("can open user collection");
let (user_id, saved_user) = collection.set(&user).expect("can save user");
println!("\nAfter saving - CustomUser ID: {}", saved_user.get_id());
println!("After saving - CustomUser 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);
}