use manifoldb::{Database, Error, Value};
fn main() -> Result<(), Error> {
let db = Database::in_memory()?;
println!("ManifoldDB Basic Usage Example");
println!("==============================\n");
let alice_id = {
let mut tx = db.begin()?;
let alice = tx
.create_entity()?
.with_label("Person")
.with_label("Employee")
.with_property("name", "Alice")
.with_property("age", 30i64)
.with_property("email", "alice@example.com");
let alice_id = alice.id;
tx.put_entity(&alice)?;
let bob = tx
.create_entity()?
.with_label("Person")
.with_property("name", "Bob")
.with_property("age", 25i64);
tx.put_entity(&bob)?;
tx.commit()?;
println!("Created Alice (id: {:?}) and Bob", alice_id);
alice_id
};
{
let tx = db.begin_read()?;
if let Some(alice) = tx.get_entity(alice_id)? {
println!("\nRetrieved Alice:");
println!(" ID: {:?}", alice.id);
println!(" Labels: {:?}", alice.labels.iter().collect::<Vec<_>>());
if let Some(Value::String(name)) = alice.get_property("name") {
println!(" Name: {}", name);
}
if let Some(Value::Int(age)) = alice.get_property("age") {
println!(" Age: {}", age);
}
}
}
{
let mut tx = db.begin()?;
if let Some(mut alice) = tx.get_entity(alice_id)? {
alice.set_property("age", 31i64);
alice.set_property("department", "Engineering");
tx.put_entity(&alice)?;
tx.commit()?;
println!("\nUpdated Alice's age to 31 and added department");
}
}
{
let tx = db.begin_read()?;
if let Some(alice) = tx.get_entity(alice_id)? {
println!("\nAfter update:");
if let Some(Value::Int(age)) = alice.get_property("age") {
println!(" Age: {}", age);
}
if let Some(Value::String(dept)) = alice.get_property("department") {
println!(" Department: {}", dept);
}
}
}
{
let mut tx = db.begin()?;
if let Some(mut alice) = tx.get_entity(alice_id)? {
alice.set_property("age", 999i64);
tx.put_entity(&alice)?;
tx.rollback()?;
println!("\nRolled back changes (age would have been 999)");
}
}
{
let tx = db.begin_read()?;
if let Some(alice) = tx.get_entity(alice_id)? {
if let Some(Value::Int(age)) = alice.get_property("age") {
println!("After rollback, age is still: {}", age);
}
}
}
{
let mut tx = db.begin()?;
let temp = tx.create_entity()?.with_label("Temporary");
let temp_id = temp.id;
tx.put_entity(&temp)?;
tx.commit()?;
let mut tx = db.begin()?;
let deleted = tx.delete_entity(temp_id)?;
tx.commit()?;
println!("\nDeleted temporary entity: {}", deleted);
}
println!("\nBasic usage example complete!");
Ok(())
}