redis_graph/
async_commands.rs

1use crate::types::*;
2use redis::aio::ConnectionLike;
3use redis::{cmd, FromRedisValue, RedisFuture, ToRedisArgs};
4
5/// Provides a high level asynchronous API to work with Redis graph data types.
6/// The graph command becomes directly available on ConnectionLike types from
7/// the redis crate when you import the GraphCommands trait.
8/// ```rust,no_run
9/// # async fn run() -> redis::RedisResult<()> {
10/// use redis::AsyncCommands;
11/// use redis_graph::{AsyncGraphCommands, GraphResultSet};
12///
13/// let client = redis::Client::open("redis://127.0.0.1/")?;
14/// let mut con = client.get_async_connection().await?;
15///
16/// let res:GraphResultSet = con.graph_query(
17///     "my_graph",
18///     "CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'})"
19/// ).await?;
20///
21/// let res_read_only:GraphResultSet = con.graph_ro_query(
22///     "my_graph",
23///     "MATCH (rider:Rider)-[:rides]->(:Team {name:'Yamaha'}) RETURN rider"
24/// ).await?;
25/// # Ok(()) }
26/// ```
27///
28pub trait AsyncGraphCommands: ConnectionLike + Send + Sized {
29    fn graph_query<'a, K: ToRedisArgs + Send + Sync + 'a, Q: ToRedisArgs + Send + Sync + 'a>(
30        &'a mut self,
31        key: K,
32        query: Q,
33    ) -> RedisFuture<GraphResultSet> {
34        Box::pin(async move {
35            cmd("GRAPH.QUERY")
36                .arg(key)
37                .arg(query)
38                .query_async(self)
39                .await
40        })
41    }
42
43    fn graph_ro_query<'a, K: ToRedisArgs + Send + Sync + 'a, Q: ToRedisArgs + Send + Sync + 'a>(
44        &'a mut self,
45        key: K,
46        query: Q,
47    ) -> RedisFuture<GraphResultSet> {
48        Box::pin(async move {
49            cmd("GRAPH.RO_QUERY")
50                .arg(key)
51                .arg(query)
52                .query_async(self)
53                .await
54        })
55    }
56
57    fn graph_profile<
58        'a,
59        K: ToRedisArgs + Send + Sync + 'a,
60        Q: ToRedisArgs + Send + Sync + 'a,
61        RV: FromRedisValue,
62    >(
63        &'a mut self,
64        key: K,
65        query: Q,
66    ) -> RedisFuture<RV> {
67        Box::pin(async move {
68            cmd("GRAPH.PROFILE")
69                .arg(key)
70                .arg(query)
71                .query_async(self)
72                .await
73        })
74    }
75
76    fn graph_delete<'a, K: ToRedisArgs + Send + Sync + 'a>(
77        &'a mut self,
78        key: K,
79    ) -> RedisFuture<String> {
80        Box::pin(async move { cmd("GRAPH.DELETE").arg(key).query_async(self).await })
81    }
82
83    fn graph_explain<
84        'a,
85        K: ToRedisArgs + Send + Sync + 'a,
86        Q: ToRedisArgs + Send + Sync + 'a,
87        RV: FromRedisValue,
88    >(
89        &'a mut self,
90        key: K,
91        query: Q,
92    ) -> RedisFuture<RV> {
93        Box::pin(async move {
94            cmd("GRAPH.EXPLAIN")
95                .arg(key)
96                .arg(query)
97                .query_async(self)
98                .await
99        })
100    }
101
102    fn graph_slowlog<'a, K: ToRedisArgs + Send + Sync + 'a>(
103        &'a mut self,
104        key: K,
105    ) -> RedisFuture<Vec<SlowLogEntry>> {
106        Box::pin(async move { cmd("GRAPH.SLOWLOG").arg(key).query_async(self).await })
107    }
108
109    fn graph_config_set<
110        'a,
111        K: ToRedisArgs + Send + Sync + 'a,
112        V: ToRedisArgs + Send + Sync + 'a,
113    >(
114        &'a mut self,
115        name: K,
116        value: V,
117    ) -> RedisFuture<bool> {
118        Box::pin(async move {
119            cmd("GRAPH.CONFIG")
120                .arg("SET")
121                .arg(name)
122                .arg(value)
123                .query_async(self)
124                .await
125        })
126    }
127
128    fn graph_config_get<'a, K: ToRedisArgs + Send + Sync + 'a, RV: FromRedisValue>(
129        &'a mut self,
130        name: K,
131    ) -> RedisFuture<RV> {
132        Box::pin(async move {
133            value_from_pair(
134                &cmd("GRAPH.CONFIG")
135                    .arg("GET")
136                    .arg(name)
137                    .query_async(self)
138                    .await?,
139            )
140        })
141    }
142
143    fn graph_config_get_all<'a>(&'a mut self) -> RedisFuture<GraphConfig> {
144        Box::pin(async move {
145            cmd("GRAPH.CONFIG")
146                .arg("GET")
147                .arg("*")
148                .query_async(self)
149                .await
150        })
151    }
152}
153
154impl<T> AsyncGraphCommands for T where T: Send + ConnectionLike {}