feat: Support incremental mode:
- Support incremental mode in heromodels - Updated the example to refelct the changes - Updated the tests to reflect the changes
This commit is contained in:
@@ -3,6 +3,29 @@ use heromodels::models::userexample::user::user_index::{is_active, username};
|
||||
use heromodels::models::{Comment, User};
|
||||
use heromodels_core::Model;
|
||||
|
||||
// Helper function to print user details
|
||||
fn print_user_details(user: &User) {
|
||||
println!("\n--- User Details ---");
|
||||
println!("ID: {}", user.get_id());
|
||||
println!("Username: {}", user.username);
|
||||
println!("Email: {}", user.email);
|
||||
println!("Full Name: {}", user.full_name);
|
||||
println!("Active: {}", user.is_active);
|
||||
println!("Created At: {}", user.base_data.created_at);
|
||||
println!("Modified At: {}", user.base_data.modified_at);
|
||||
println!("Comments: {:?}", user.base_data.comments);
|
||||
}
|
||||
|
||||
// Helper function to print comment details
|
||||
fn print_comment_details(comment: &Comment) {
|
||||
println!("\n--- Comment Details ---");
|
||||
println!("ID: {}", comment.get_id());
|
||||
println!("User ID: {}", comment.user_id);
|
||||
println!("Content: {}", comment.content);
|
||||
println!("Created At: {}", comment.base_data.created_at);
|
||||
println!("Modified At: {}", comment.base_data.modified_at);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Create a new DB instance in /tmp/ourdb, and reset before every run
|
||||
let db = heromodels::db::hero::OurDB::new("/tmp/ourdb", true).expect("Can create DB");
|
||||
@@ -10,50 +33,75 @@ fn main() {
|
||||
println!("Hero Models - Basic Usage Example");
|
||||
println!("================================");
|
||||
|
||||
// Create a new user using the fluent interface
|
||||
let user = User::new(1)
|
||||
// Create users with different ID configurations
|
||||
|
||||
// User 1: With explicit ID
|
||||
let user1 = User::new(Some(1))
|
||||
.username("johndoe")
|
||||
.email("john.doe@example.com")
|
||||
.full_name("John Doe")
|
||||
.is_active(false)
|
||||
.build();
|
||||
let user2 = User::new(2)
|
||||
|
||||
// User 2: With auto-generated ID
|
||||
let user2 = User::new(None)
|
||||
.username("janesmith")
|
||||
.email("jane.smith@example.com")
|
||||
.full_name("Jane Smith")
|
||||
.is_active(true)
|
||||
.build();
|
||||
let user3 = User::new(3)
|
||||
|
||||
// User 3: With explicit ID
|
||||
let user3 = User::new(Some(3))
|
||||
.username("willism")
|
||||
.email("willis.masters@example.com")
|
||||
.full_name("Willis Masters")
|
||||
.is_active(true)
|
||||
.build();
|
||||
let user4 = User::new(4)
|
||||
|
||||
// User 4: With explicit ID
|
||||
let user4 = User::new(Some(4))
|
||||
.username("carrols")
|
||||
.email("carrol.smith@example.com")
|
||||
.full_name("Carrol Smith")
|
||||
.is_active(false)
|
||||
.build();
|
||||
|
||||
db.collection()
|
||||
.expect("can open user collection")
|
||||
.set(&user)
|
||||
.expect("can set user");
|
||||
db.collection()
|
||||
.expect("can open user collection")
|
||||
.set(&user2)
|
||||
.expect("can set user");
|
||||
db.collection()
|
||||
.expect("can open user collection")
|
||||
.set(&user3)
|
||||
.expect("can set user");
|
||||
db.collection()
|
||||
.expect("can open user collection")
|
||||
.set(&user4)
|
||||
.expect("can set user");
|
||||
// Save all users to database
|
||||
db.collection().expect("can open user collection").set(&user1).expect("can set user");
|
||||
db.collection().expect("can open user collection").set(&user2).expect("can set user");
|
||||
db.collection().expect("can open user collection").set(&user3).expect("can set user");
|
||||
db.collection().expect("can open user collection").set(&user4).expect("can set user");
|
||||
|
||||
// Perform an indexed lookup on the Username
|
||||
// Retrieve all users from database
|
||||
let db_user1 = db.collection::<User>().expect("can open user collection")
|
||||
.get_by_id(user1.get_id()).expect("can load user").expect("user should exist");
|
||||
let db_user2 = db.collection::<User>().expect("can open user collection")
|
||||
.get_by_id(user2.get_id()).expect("can load user").expect("user should exist");
|
||||
let db_user3 = db.collection::<User>().expect("can open user collection")
|
||||
.get_by_id(user3.get_id()).expect("can load user").expect("user should exist");
|
||||
let db_user4 = db.collection::<User>().expect("can open user collection")
|
||||
.get_by_id(user4.get_id()).expect("can load user").expect("user should exist");
|
||||
|
||||
// Print all users retrieved from database
|
||||
println!("\n--- Users Retrieved from Database ---");
|
||||
println!("\n1. User with explicit ID (1):");
|
||||
print_user_details(&db_user1);
|
||||
|
||||
println!("\n2. User with auto-generated ID:");
|
||||
print_user_details(&db_user2);
|
||||
|
||||
println!("\n3. User with explicit ID (3):");
|
||||
print_user_details(&db_user3);
|
||||
|
||||
println!("\n4. User with explicit ID (4):");
|
||||
print_user_details(&db_user4);
|
||||
|
||||
// Demonstrate different ways to retrieve users from the database
|
||||
|
||||
// 1. Retrieve by username index
|
||||
println!("\n--- Retrieving Users by Different Methods ---");
|
||||
println!("\n1. By Username Index:");
|
||||
let stored_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
@@ -61,72 +109,105 @@ fn main() {
|
||||
.expect("can load stored user");
|
||||
|
||||
assert_eq!(stored_users.len(), 1);
|
||||
let stored_user = &stored_users[0];
|
||||
print_user_details(&stored_users[0]);
|
||||
|
||||
assert_eq!(user.username, stored_user.username);
|
||||
assert_eq!(user.email, stored_user.email);
|
||||
assert_eq!(user.is_active, stored_user.is_active);
|
||||
assert_eq!(user.full_name, stored_user.full_name);
|
||||
|
||||
// Load all active users using the IsActive field index
|
||||
// TODO: expand Index type so it defines the type of the key
|
||||
// 2. Retrieve by active status
|
||||
println!("\n2. By Active Status (Active = true):");
|
||||
let active_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<is_active, _>(&true)
|
||||
.expect("can load stored users");
|
||||
// We should have 2 active users
|
||||
assert_eq!(active_users.len(), 2);
|
||||
|
||||
// Now remove a user
|
||||
assert_eq!(active_users.len(), 2);
|
||||
for (i, active_user) in active_users.iter().enumerate() {
|
||||
print_user_details(active_user);
|
||||
}
|
||||
|
||||
// 3. Delete a user and show the updated results
|
||||
println!("\n3. After Deleting a User:");
|
||||
db.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.delete_by_id(active_users[0].get_id())
|
||||
.expect("can delete existing user");
|
||||
|
||||
// Load the active users again, should be 1 left
|
||||
// Show remaining active users
|
||||
let active_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<is_active, _>(&true)
|
||||
.expect("can load stored users");
|
||||
|
||||
println!(" a. Remaining Active Users:");
|
||||
assert_eq!(active_users.len(), 1);
|
||||
// And verify we still have 2 inactive users
|
||||
for (i, active_user) in active_users.iter().enumerate() {
|
||||
print_user_details(active_user);
|
||||
}
|
||||
|
||||
// Show inactive users
|
||||
let inactive_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<is_active, _>(&false)
|
||||
.expect("can load stored users");
|
||||
assert_eq!(inactive_users.len(), 2);
|
||||
|
||||
println!("Created user: {:?}", user);
|
||||
println!("User ID: {}", user.get_id());
|
||||
println!(" b. Inactive Users:");
|
||||
assert_eq!(inactive_users.len(), 2);
|
||||
for (i, inactive_user) in inactive_users.iter().enumerate() {
|
||||
print_user_details(inactive_user);
|
||||
}
|
||||
|
||||
println!("\n--- User Model Information ---");
|
||||
println!("User DB Prefix: {}", User::db_prefix());
|
||||
|
||||
// Create a comment for the user
|
||||
let comment = Comment::new(5)
|
||||
.user_id(1) // commenter's user ID
|
||||
// Demonstrate comment creation and association with a user
|
||||
println!("\n--- Working with Comments ---");
|
||||
|
||||
// 1. Create and save a comment
|
||||
println!("\n1. Creating a Comment:");
|
||||
let comment = Comment::new(None)
|
||||
.user_id(db_user1.get_id()) // commenter's user ID
|
||||
.content("This is a comment on the user")
|
||||
.build();
|
||||
|
||||
db.collection()
|
||||
.expect("can open commen collection")
|
||||
.expect("can open comment collection")
|
||||
.set(&comment)
|
||||
.expect("can set comment");
|
||||
|
||||
let stored_comment = db
|
||||
// 2. Retrieve the comment from database
|
||||
let db_comment = db
|
||||
.collection::<Comment>()
|
||||
.expect("can open comment collection")
|
||||
.get_by_id(5)
|
||||
.expect("can load stored comment");
|
||||
.get_by_id(comment.get_id())
|
||||
.expect("can load comment")
|
||||
.expect("comment should exist");
|
||||
|
||||
assert!(stored_comment.is_some());
|
||||
let stored_comment = stored_comment.unwrap();
|
||||
println!(" a. Comment Retrieved from Database:");
|
||||
print_comment_details(&db_comment);
|
||||
|
||||
assert_eq!(comment.get_id(), stored_comment.get_id());
|
||||
assert_eq!(comment.content, stored_comment.content);
|
||||
// 3. Associate the comment with a user
|
||||
println!("\n2. Associating Comment with User:");
|
||||
let mut updated_user = db_user1.clone();
|
||||
updated_user.base_data.add_comment(db_comment.get_id());
|
||||
|
||||
println!("\nCreated comment: {:?}", comment);
|
||||
println!("Comment ID: {}", comment.get_id());
|
||||
db.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.set(&updated_user)
|
||||
.expect("can set updated user");
|
||||
|
||||
// 4. Retrieve the updated user
|
||||
let user_with_comment = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get_by_id(updated_user.get_id())
|
||||
.expect("can load user")
|
||||
.expect("user should exist");
|
||||
|
||||
println!(" a. User with Associated Comment:");
|
||||
print_user_details(&user_with_comment);
|
||||
|
||||
println!("\n--- Model Information ---");
|
||||
println!("User DB Prefix: {}", User::db_prefix());
|
||||
println!("Comment DB Prefix: {}", Comment::db_prefix());
|
||||
}
|
||||
|
Reference in New Issue
Block a user