solidb_client/client/
document.rs

1use super::SoliDBClient;
2use crate::protocol::{Command, DriverError, Response};
3use serde_json::Value;
4
5impl SoliDBClient {
6    pub async fn get(
7        &mut self,
8        database: &str,
9        collection: &str,
10        key: &str,
11    ) -> Result<Value, DriverError> {
12        let response = self
13            .send_command(Command::Get {
14                database: database.to_string(),
15                collection: collection.to_string(),
16                key: key.to_string(),
17            })
18            .await?;
19        Self::extract_data(response)?
20            .ok_or_else(|| DriverError::ProtocolError("Expected data".to_string()))
21    }
22
23    pub async fn insert(
24        &mut self,
25        database: &str,
26        collection: &str,
27        key: Option<&str>,
28        document: Value,
29    ) -> Result<Value, DriverError> {
30        let response = self
31            .send_command(Command::Insert {
32                database: database.to_string(),
33                collection: collection.to_string(),
34                key: key.map(|s| s.to_string()),
35                document,
36            })
37            .await?;
38        Self::extract_data(response)?
39            .ok_or_else(|| DriverError::ProtocolError("Expected data".to_string()))
40    }
41
42    pub async fn update(
43        &mut self,
44        database: &str,
45        collection: &str,
46        key: &str,
47        document: Value,
48        merge: bool,
49    ) -> Result<Value, DriverError> {
50        let response = self
51            .send_command(Command::Update {
52                database: database.to_string(),
53                collection: collection.to_string(),
54                key: key.to_string(),
55                document,
56                merge,
57            })
58            .await?;
59        Self::extract_data(response)?
60            .ok_or_else(|| DriverError::ProtocolError("Expected data".to_string()))
61    }
62
63    pub async fn delete(
64        &mut self,
65        database: &str,
66        collection: &str,
67        key: &str,
68    ) -> Result<(), DriverError> {
69        let response = self
70            .send_command(Command::Delete {
71                database: database.to_string(),
72                collection: collection.to_string(),
73                key: key.to_string(),
74            })
75            .await?;
76        Self::extract_data(response)?;
77        Ok(())
78    }
79
80    pub async fn list(
81        &mut self,
82        database: &str,
83        collection: &str,
84        limit: Option<usize>,
85        offset: Option<usize>,
86    ) -> Result<(Vec<Value>, usize), DriverError> {
87        let response = self
88            .send_command(Command::List {
89                database: database.to_string(),
90                collection: collection.to_string(),
91                limit,
92                offset,
93            })
94            .await?;
95
96        match response {
97            Response::Ok { data, count, .. } => {
98                let docs: Vec<Value> = data
99                    .and_then(|d| serde_json::from_value(d).ok())
100                    .unwrap_or_default();
101                let len = docs.len();
102                Ok((docs, count.unwrap_or(len)))
103            }
104            Response::Error { error } => Err(error),
105            _ => Err(DriverError::ProtocolError(
106                "Unexpected response".to_string(),
107            )),
108        }
109    }
110}