fix: Use incremental ID
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user