commit
6c54873ca2
34 changed files with 5502 additions and 0 deletions
80
examples/friends.rs
Normal file
80
examples/friends.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
use owl::{
|
||||
db::{
|
||||
model::person::Person,
|
||||
relation::{clean_graph_traversal, find_path, get_other},
|
||||
},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
#[relation("friend", Person, "friendy", Person, RelationKind::Unidirectional)]
|
||||
pub struct Friendship;
|
||||
|
||||
pub fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let db = Database::filesystem("./db");
|
||||
let alice = Person::new_id("alice", "", "");
|
||||
let alice = db.save(alice);
|
||||
let bob = Person::new_id("bob", "", "");
|
||||
let bob = db.save(bob);
|
||||
|
||||
Friendship {}.add(&alice, &bob, None, &db);
|
||||
|
||||
let charizard = Person::new_id("charizard", "", "");
|
||||
let charizard = db.save(charizard);
|
||||
|
||||
Friendship.add(&alice, &charizard, None, &db);
|
||||
Friendship.add(&charizard, &bob, None, &db);
|
||||
|
||||
let pika = db.save(Person::new_id("pika", "", ""));
|
||||
|
||||
Friendship.add(&pika, &charizard, None, &db);
|
||||
|
||||
let malice = db.save(Person::new_id("malice", "", ""));
|
||||
|
||||
Friendship.add(&pika, &malice, None, &db);
|
||||
|
||||
let drache = db.save(Person::new_id("drache", "", ""));
|
||||
Friendship.add(&drache, &bob, None, &db);
|
||||
Friendship.add(&drache, &malice, None, &db);
|
||||
|
||||
let enid = db.save(Person::new_id("enid", "", ""));
|
||||
|
||||
Friendship.add(&enid, &alice, None, &db);
|
||||
|
||||
print_friends("person::alice", &db);
|
||||
print_friends("person::bob", &db);
|
||||
print_friends("person::charizard", &db);
|
||||
print_friends("person::drache", &db);
|
||||
print_friends("person::enid", &db);
|
||||
print_friends("person::malice", &db);
|
||||
|
||||
println!(
|
||||
"alice to malice? - {:?}",
|
||||
clean_graph_traversal(
|
||||
"person::alice",
|
||||
&find_path(
|
||||
"person::alice".into(),
|
||||
"person::malice".into(),
|
||||
6,
|
||||
|id, db| get_friends_of(id, &db),
|
||||
&db
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
pub fn print_friends(id: &str, db: &Database) {
|
||||
let other: Vec<_> = get_friends_of(id, db);
|
||||
println!("friends of {id} -- {other:?}");
|
||||
}
|
||||
|
||||
pub fn get_friends_of<T: Into<IdRef<Person>>>(id: T, db: &Database) -> Vec<IdRef<Person>> {
|
||||
let id: IdRef<Person> = id.into();
|
||||
let refs = Friendship::get_friend_of(id.to_string(), db);
|
||||
dbg!(&refs);
|
||||
get_other::<Person, _, _>(id, refs, db)
|
||||
.into_iter()
|
||||
.map(|x| IdRef::<Person>::from(&x))
|
||||
.collect()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue