diff --git a/README.md b/README.md index 04113c0b..913c7871 100644 --- a/README.md +++ b/README.md @@ -37,16 +37,28 @@ use agdb::Db; use agdb::QueryBuilder; use agdb::Comparison::Equal; -let mut db = Db::new("user_db.agdb")?; +let mut db = Db::new("db_file.agdb")?; db.exec_mut(&QueryBuilder::insert().nodes().aliases("users").query())?; -let users = db.exec_mut(&QueryBuilder::insert() - .nodes() - .values(vec![vec![("username", "Alice").into(), ("joined", 2023).into()], - vec![("username", "Bob").into(), ("joined", 2015).into()], - vec![("username", "John").into()]]) - .query())?; -db.exec_mut(&QueryBuilder::insert().edges().from("users").to(&users).query())?; + +let users = db.exec_mut( + &QueryBuilder::insert() + .nodes() + .values(vec![ + vec![("id", 1).into(), ("username", "user_1").into()], + vec![("id", 2).into(), ("username", "user_2").into()], + vec![("id", 3).into(), ("username", "user_3").into()], + ]) + .query(), +)?; + +db.exec_mut( + &QueryBuilder::insert() + .edges() + .from("users") + .to(&users) + .query(), +)?; ``` This code creates a database called `user_db.agdb` with a simple graph of 4 nodes. The first node is aliased `users` and 3 user nodes for Alice, Bob and John are then connected with edges to the `users` node. The arbitrary `username` property and sparse `joined` property are attached to the user nodes. @@ -59,28 +71,32 @@ println!("{:?}", user_elements); // QueryResult { // result: 3, // elements: [ -// DbElement { id: DbId(2), values: [DbKeyValue { key: String("username"), value: String("Alice") }, DbKeyValue { key: String("joined"), value: Int(2023) }] }, -// DbElement { id: DbId(3), values: [DbKeyValue { key: String("username"), value: String("Bob") }, DbKeyValue { key: String("joined"), value: Int(2015) }] }, -// DbElement { id: DbId(4), values: [DbKeyValue { key: String("username"), value: String("John") }] } +// DbElement { id: DbId(2), values: [DbKeyValue { key: String("id"), value: Int(1) }, DbKeyValue { key: String("username"), value: String("user_1") }] }, +// DbElement { id: DbId(3), values: [DbKeyValue { key: String("id"), value: Int(2) }, DbKeyValue { key: String("username"), value: String("user_2") }] }, +// DbElement { id: DbId(4), values: [DbKeyValue { key: String("id"), value: Int(3) }, DbKeyValue { key: String("username"), value: String("user_3") }] } // ] } ``` You can also search through the graph to get back only the elements you want: ```Rust -let user = db.exec(&QueryBuilder::select() - .search(QueryBuilder::search() - .from("users") - .where_() - .key("username") - .value(Equal("John".into())) - .query()) - .query())?; +let user_id = db.exec( + &QueryBuilder::select() + .ids( + QueryBuilder::search() + .from("users") + .where_() + .key("username") + .value(Equal("user_2".into())) + .query(), + ) + .query(), +)?; println!("{:?}", user); // QueryResult { // result: 1, // elements: [ -// DbElement { id: DbId(4), values: [DbKeyValue { key: String("username"), value: String("John") }] } +// DbElement { id: DbId(3), values: [DbKeyValue { key: String("id"), value: Int(2) }, DbKeyValue { key: String("username"), value: String("user_2") }] } // ] } ``` diff --git a/tests/quickstart.rs b/tests/quickstart.rs index 04381078..fb2b5f79 100644 --- a/tests/quickstart.rs +++ b/tests/quickstart.rs @@ -11,15 +11,19 @@ fn quickstart() -> Result<(), QueryError> { let _test_file = TestFile::from("db_file.agdb"); let mut db = Db::new("db_file.agdb")?; - let insert_users_root = QueryBuilder::insert().nodes().aliases("users").query(); - db.exec_mut(&insert_users_root)?; - let user_values = vec![ - vec![("id", 1).into(), ("username", "user_1").into()], - vec![("id", 2).into(), ("username", "user_2").into()], - vec![("id", 3).into(), ("username", "user_3").into()], - ]; - let users = db.exec_mut(&QueryBuilder::insert().nodes().values(user_values).query())?; + db.exec_mut(&QueryBuilder::insert().nodes().aliases("users").query())?; + + let users = db.exec_mut( + &QueryBuilder::insert() + .nodes() + .values(vec![ + vec![("id", 1).into(), ("username", "user_1").into()], + vec![("id", 2).into(), ("username", "user_2").into()], + vec![("id", 3).into(), ("username", "user_3").into()], + ]) + .query(), + )?; db.exec_mut( &QueryBuilder::insert() @@ -30,7 +34,6 @@ fn quickstart() -> Result<(), QueryError> { )?; let user_elements = db.exec(&QueryBuilder::select().ids(users).query())?; - println!("{:?}", user_elements); // QueryResult { // result: 3,