redis_om/json_model/
async.rs1use super::{cmds, parse_from_get_resp};
2use crate::{RedisModel, RedisSearchModel};
3use redis::{aio::ConnectionLike, AsyncIter, RedisResult};
4use serde::{de::DeserializeOwned, Serialize};
5
6#[async_trait::async_trait]
8pub trait JsonModel: RedisModel + RedisSearchModel + Serialize + DeserializeOwned {
9 fn redissearch_schema() -> &'static str {
11 <Self as RedisSearchModel>::_REDIS_SEARCH_SCHEMA
12 }
13
14 fn redis_prefix() -> &'static str {
17 Self::_prefix_key()
18 }
19
20 async fn save<C>(&mut self, conn: &mut C) -> RedisResult<()>
22 where
23 C: ConnectionLike + Send,
24 {
25 self._ensure_pk();
26 let cmd = cmds::save(self._get_redis_key(), self)?;
27
28 cmd.query_async(conn).await
29 }
30
31 async fn all_pks<C>(conn: &mut C) -> RedisResult<AsyncIter<'_, String>>
33 where
34 C: ConnectionLike + Send,
35 {
36 let cmd = cmds::all_pks::<Self>()?;
37
38 cmd.iter_async(conn).await
39 }
40
41 async fn get<S, C>(pk: S, conn: &mut C) -> RedisResult<Self>
43 where
44 S: AsRef<str> + Send,
45 C: ConnectionLike + Send,
46 {
47 let pk = pk.as_ref();
48 let cmd = cmds::get::<Self>(pk)?;
49 let resp = cmd.query_async(conn).await?;
50
51 parse_from_get_resp(resp)
52 }
53
54 async fn delete<S, C>(pk: S, conn: &mut C) -> RedisResult<()>
56 where
57 S: AsRef<str> + Send,
58 C: ConnectionLike + Send,
59 {
60 let cmd = cmds::delete::<Self>(pk)?;
61
62 cmd.query_async(conn).await
63 }
64
65 async fn expire<C>(&self, secs: usize, conn: &mut C) -> RedisResult<()>
67 where
68 C: ConnectionLike + Send,
69 {
70 let cmd = self._expire_cmd(secs)?;
71
72 cmd.query_async(conn).await
73 }
74}