solidb_client/client/
collection.rs

1use super::SoliDBClient;
2use crate::protocol::{Command, DriverError};
3use serde_json::Value;
4
5impl SoliDBClient {
6    pub async fn list_collections(&mut self, database: &str) -> Result<Vec<String>, DriverError> {
7        let response = self
8            .send_command(Command::ListCollections {
9                database: database.to_string(),
10            })
11            .await?;
12        let data = Self::extract_data(response)?
13            .ok_or_else(|| DriverError::ProtocolError("Expected data".to_string()))?;
14
15        serde_json::from_value(data)
16            .map_err(|e| DriverError::ProtocolError(format!("Invalid response: {}", e)))
17    }
18
19    pub async fn create_collection(
20        &mut self,
21        database: &str,
22        name: &str,
23        collection_type: Option<&str>,
24    ) -> Result<(), DriverError> {
25        let response = self
26            .send_command(Command::CreateCollection {
27                database: database.to_string(),
28                name: name.to_string(),
29                collection_type: collection_type.map(|s| s.to_string()),
30            })
31            .await?;
32        Self::extract_data(response)?;
33        Ok(())
34    }
35
36    pub async fn delete_collection(
37        &mut self,
38        database: &str,
39        name: &str,
40    ) -> Result<(), DriverError> {
41        let response = self
42            .send_command(Command::DeleteCollection {
43                database: database.to_string(),
44                name: name.to_string(),
45            })
46            .await?;
47        Self::extract_data(response)?;
48        Ok(())
49    }
50
51    pub async fn collection_stats(
52        &mut self,
53        database: &str,
54        collection: &str,
55    ) -> Result<Value, DriverError> {
56        let response = self
57            .send_command(Command::CollectionStats {
58                database: database.to_string(),
59                name: collection.to_string(),
60            })
61            .await?;
62        Self::extract_data(response)?
63            .ok_or_else(|| DriverError::ProtocolError("Expected data".to_string()))
64    }
65}