use gigabrain::{Graph}; use gigabrain::core::{PropertyValue, relationship::Direction}; use std::sync::Arc; mod dx_helpers; use dx_helpers::{GraphDxExt, NodeBuilder}; #[tokio::main] async fn main() -> Result<(), Box> { println!("🧠 GigaBrain Basic Example"); println!("========================="); // Create a new graph (wrapped in Arc for DX helpers) let graph = Arc::new(Graph::new()); // Setup schema { let mut schema = graph.schema().write(); schema.get_or_create_label("Person"); schema.get_or_create_label("Company"); schema.get_or_create_property_key("name"); schema.get_or_create_property_key("age"); schema.get_or_create_property_key("email"); schema.get_or_create_relationship_type("WORKS_FOR"); schema.get_or_create_relationship_type("KNOWS"); } // Get schema IDs - they were already created above let (person_label, company_label, name_prop, age_prop, email_prop, works_for_rel, knows_rel) = { let mut schema = graph.schema().write(); ( schema.get_or_create_label("Person"), schema.get_or_create_label("Company"), schema.get_or_create_property_key("name"), schema.get_or_create_property_key("age"), schema.get_or_create_property_key("email"), schema.get_or_create_relationship_type("WORKS_FOR"), schema.get_or_create_relationship_type("KNOWS") ) }; // Create some nodes println!("\nπŸ“ Creating nodes..."); // Create Alice let alice_id = graph.create_node(); graph.update_node(alice_id, |node| { node.add_label(person_label); node.properties.insert(name_prop, PropertyValue::String("Alice".to_string())); node.properties.insert(age_prop, PropertyValue::Integer(30)); node.properties.insert(email_prop, PropertyValue::String("alice@example.com".to_string())); })?; println!("βœ… Created Alice (Person)"); // Create Bob let bob_id = graph.create_node(); graph.update_node(bob_id, |node| { node.add_label(person_label); node.properties.insert(name_prop, PropertyValue::String("Bob".to_string())); node.properties.insert(age_prop, PropertyValue::Integer(25)); node.properties.insert(email_prop, PropertyValue::String("bob@example.com".to_string())); })?; println!("βœ… Created Bob (Person)"); // Create TechCorp let techcorp_id = graph.create_node(); graph.update_node(techcorp_id, |node| { node.add_label(company_label); node.properties.insert(name_prop, PropertyValue::String("TechCorp".to_string())); })?; println!("βœ… Created TechCorp (Company)"); // Create relationships println!("\nπŸ”— Creating relationships..."); // Alice WORKS_FOR TechCorp graph.create_relationship(alice_id, techcorp_id, works_for_rel)?; println!("βœ… Alice works for TechCorp"); // Bob WORKS_FOR TechCorp graph.create_relationship(bob_id, techcorp_id, works_for_rel)?; println!("βœ… Bob works for TechCorp"); // Alice KNOWS Bob graph.create_relationship(alice_id, bob_id, knows_rel)?; println!("βœ… Alice knows Bob"); // Query the graph println!("\nπŸ” Querying the graph..."); // Show all nodes let all_nodes = graph.get_all_nodes(); println!("πŸ“Š Total nodes: {}", all_nodes.len()); // Find all people let mut people = Vec::new(); for node_id in &all_nodes { if let Some(node) = graph.get_node(*node_id) { if node.has_label(person_label) { if let Some(PropertyValue::String(name)) = node.properties.get(&name_prop) { if let Some(PropertyValue::Integer(age)) = node.properties.get(&age_prop) { people.push((name.clone(), *age)); } } } } } println!("πŸ‘₯ People in the graph:"); for (name, age) in &people { println!(" - {} (age: {})", name, age); } // Find Alice's relationships println!("\nπŸ•ΈοΈ Alice's relationships:"); let alice_rels = graph.get_node_relationships(alice_id, gigabrain::core::relationship::Direction::Outgoing, None); let schema = graph.schema().read(); for rel in alice_rels { if let Some(target_node) = graph.get_node(rel.end_node) { if let Some(PropertyValue::String(target_name)) = target_node.properties.get(&name_prop) { if let Some(rel_name) = schema.get_relationship_type_name(rel.rel_type) { println!(" - {} -> {}", rel_name, target_name); } } } } drop(schema); // Show graph statistics println!("\nπŸ“ˆ Graph Statistics:"); println!(" Nodes: {}", graph.get_all_nodes().len()); let mut total_relationships = 0; for node_id in &all_nodes { let rels = graph.get_node_relationships(*node_id, gigabrain::core::relationship::Direction::Outgoing, None); total_relationships += rels.len(); } println!(" Relationships: {}", total_relationships); // Demonstrate DX improvements println!("\nπŸš€ DX Helper Demonstration:"); // Extract schema IDs once to avoid repeated lock acquisition let schema_ids = graph.extract_schema_ids(); // Use builder pattern for clean node creation let charlie_id = NodeBuilder::new(graph.clone()) .with_label(schema_ids.user_label)? .with_property(schema_ids.username_prop, PropertyValue::String("charlie".to_string()))? .build(); println!("βœ… Created charlie using NodeBuilder pattern"); // Use ergonomic relationship querying let alice_followers = graph.get_node_relationships_single(alice_id, Direction::Incoming, knows_rel); println!("πŸ‘₯ Alice has {} people who know her", alice_followers.len()); println!("\n✨ Example completed successfully!"); Ok(()) }