gitdb-client 1.0.3

Official Rust client for GitDB - GitHub-backed NoSQL database
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!("=======================================");

    // Initialize client
    let client = GitDBClient::new(
        "your-github-token",
        "your-github-username",
        "your-repo-name"
    );

    // Check server health
    println!("Checking server health...");
    match client.health().await {
        Ok(()) => println!("✅ Server is healthy"),
        Err(e) => {
            eprintln!("❌ Health check failed: {}", e);
            return Ok(());
        }
    }

    // Create a collection
    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),
    }

    // Insert a document
    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(());
        }
    };

    // Find the document by ID
    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);
        }
    }

    // Find documents with query
    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);
        }
    }

    // Update the document
    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);
        }
    }

    // Count documents
    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);
        }
    }

    // Delete the document
    println!("\nDeleting user...");
    match client.delete("users", &id).await {
        Ok(()) => println!("✅ User deleted successfully"),
        Err(e) => {
            eprintln!("❌ Failed to delete user: {}", e);
        }
    }

    // List collections
    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(())
}