2 个不稳定版本
0.2.0 | 2022年7月20日 |
---|---|
0.1.0 | 2022年6月25日 |
1070 in 数据库接口
45KB
988 行
redisgraphio
这是一个用于与RedisGraph模块(为Redis)交互的Rust客户端库。
它通过与redis连接实现一个trait与redis-rs协同工作。
特性
同步使用
[dependencies]
redis = "0.21" # or higher
redisgraphio = "0.1"
use redis::RedisResult;
use redisgraphio::*;
fn rider_example() -> RedisResult<()> {
let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_connection()?;
con.graph_query_void("my_graph", query!(
"CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'})"
))?;
// Assuming this could be malicious user input you need to escape
let team_name = "Yamaha";
let riders: Vec<(Node,)> = con.graph_query(
"my_graph", // graph to query
query!(
"MATCH (rider:Rider)-[:rides]->(:Team {name: $team}) RETURN rider",
{
"team" => team_name
},
true // Optinal parameter to enable read only access, default is false
)
)?.data;
for (rider,) in riders {
let name: String = rider.get_property_by_index(0)?;
println!("{name}")
}
Ok(())
}
异步使用
要启用redisgraphio异步命令,请启用tokio-comp
或async-std-comp
[dependencies]
redis = "0.21.0"
redis-graph = { version = "0.1", features = ['tokio-comp'] }
use redis::RedisResult;
use redisgraphio::*;
async fn rider_example() -> RedisResult<()> {
let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_async_connection().await?;
con.graph_query_void("my_graph", query!(
"CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'})"
)).await?;
// Assuming this could be malicious user input you need to escape
let team_name = "Yamaha";
let riders: Vec<(Node,)> = con.graph_query(
"my_graph",
query!(
"MATCH (rider:Rider)-[:rides]->(:Team {name: $team}) RETURN rider",
{
"team" => team_name
},
true // Optinal parameter to enable read only access, default is false
)
).await?.data;
for (rider,) in riders {
let name: String = rider.get_property_by_index(0)?;
println!("{name}")
}
Ok(())
}
致谢
Crate的API受到了redis-graph crate的启发,该crate也在redis连接上实现了traits。
序列化受到了redis-rs本身的启发。
依赖
~4–15MB
~216K SLoC