use gitdb_client::GitDBClient;
use std::collections::HashMap;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("GitDB Rust Client - Basic Usage Example");
println!("=======================================");
let client = GitDBClient::new(
"your-github-token",
"your-github-username",
"your-repo-name"
);
println!("Checking server health...");
match client.health().await {
Ok(()) => println!("✅ Server is healthy"),
Err(e) => {
eprintln!("❌ Health check failed: {}", e);
return Ok(());
}
}
println!("\nCreating 'users' collection...");
match client.create_collection("users").await {
Ok(()) => println!("✅ Collection 'users' created"),
Err(e) => println!("⚠️ Collection creation failed (might already exist): {}", e),
}
println!("\nInserting a user document...");
let mut user = HashMap::new();
user.insert("name".to_string(), json!("John Doe"));
user.insert("email".to_string(), json!("john@example.com"));
user.insert("age".to_string(), json!(30));
user.insert("city".to_string(), json!("New York"));
let id = match client.insert("users", &user).await {
Ok(id) => {
println!("✅ User inserted with ID: {}", id);
id
}
Err(e) => {
eprintln!("❌ Failed to insert user: {}", e);
return Ok(());
}
};
println!("\nFinding user by ID...");
match client.find_by_id("users", &id).await {
Ok(found_user) => {
println!("✅ Found user: {:?}", found_user);
}
Err(e) => {
eprintln!("❌ Failed to find user: {}", e);
}
}
println!("\nFinding users older than 25...");
let mut query = HashMap::new();
query.insert("age".to_string(), json!({ "$gte": 25 }));
match client.find("users", &query).await {
Ok(users) => {
println!("✅ Found {} users older than 25", users.len());
for user in users {
println!(" - {:?}", user);
}
}
Err(e) => {
eprintln!("❌ Failed to find users: {}", e);
}
}
println!("\nUpdating user age...");
let mut update = HashMap::new();
update.insert("age".to_string(), json!(31));
update.insert("last_updated".to_string(), json!("2024-01-01"));
match client.update("users", &id, &update).await {
Ok(()) => println!("✅ User updated successfully"),
Err(e) => {
eprintln!("❌ Failed to update user: {}", e);
}
}
println!("\nCounting all users...");
match client.count("users", &HashMap::new()).await {
Ok(count) => println!("✅ Total users: {}", count),
Err(e) => {
eprintln!("❌ Failed to count users: {}", e);
}
}
println!("\nDeleting user...");
match client.delete("users", &id).await {
Ok(()) => println!("✅ User deleted successfully"),
Err(e) => {
eprintln!("❌ Failed to delete user: {}", e);
}
}
println!("\nListing all collections...");
match client.list_collections().await {
Ok(collections) => {
println!("✅ Collections:");
for collection in collections {
println!(" - {} ({} documents)", collection.name, collection.count);
}
}
Err(e) => {
eprintln!("❌ Failed to list collections: {}", e);
}
}
println!("\n🎉 Basic usage example completed!");
Ok(())
}