Skip to main content

arcadedb_protocol/
admin.rs

1//! Admin operations — the [`ArcadeDbClient::admin`] facade.
2//!
3//! Server and database lifecycle calls (ping, databases, server info).
4
5use crate::client::{map_rpc_error, ArcadeDbClient};
6use crate::error::Result;
7use crate::proto::com::arcadedb::grpc::{
8    CreateDatabaseRequest, DropDatabaseRequest, ExistsDatabaseRequest, GetDatabaseInfoRequest,
9    GetDatabaseInfoResponse, GetServerInfoRequest, GetServerInfoResponse, ListDatabasesRequest,
10    PingRequest, PingResponse,
11};
12
13/// Server-lifecycle and inspection operations. Obtain via
14/// [`ArcadeDbClient::admin`].
15#[derive(Clone)]
16pub struct Admin {
17    client: ArcadeDbClient,
18}
19
20impl Admin {
21    pub(crate) fn new(client: ArcadeDbClient) -> Self {
22        Self { client }
23    }
24
25    /// Ping the server to verify admin connectivity.
26    pub async fn ping(&self) -> Result<PingResponse> {
27        let resp = self
28            .client
29            .admin_stub()
30            .ping(PingRequest {
31                credentials: Some(self.client.creds()),
32            })
33            .await
34            .map_err(|e| map_rpc_error("ping", "", e))?
35            .into_inner();
36        Ok(resp)
37    }
38
39    /// Create a new database. `db_type` is `"graph"` or `"document"`
40    /// (logical; see the proto spec).
41    pub async fn create_database(&self, name: &str, db_type: &str) -> Result<()> {
42        self.client
43            .admin_stub()
44            .create_database(CreateDatabaseRequest {
45                credentials: Some(self.client.creds()),
46                name: name.into(),
47                r#type: db_type.into(),
48            })
49            .await
50            .map_err(|e| map_rpc_error("create database", name, e))?;
51        tracing::info!(db = %name, "database created");
52        Ok(())
53    }
54
55    /// Drop a database by name.
56    pub async fn drop_database(&self, name: &str) -> Result<()> {
57        self.client
58            .admin_stub()
59            .drop_database(DropDatabaseRequest {
60                credentials: Some(self.client.creds()),
61                name: name.into(),
62            })
63            .await
64            .map_err(|e| map_rpc_error("drop database", name, e))?;
65        tracing::info!(db = %name, "database dropped");
66        Ok(())
67    }
68
69    /// Whether a database with this name exists on the server.
70    pub async fn exists_database(&self, name: &str) -> Result<bool> {
71        let resp = self
72            .client
73            .admin_stub()
74            .exists_database(ExistsDatabaseRequest {
75                credentials: Some(self.client.creds()),
76                name: name.into(),
77            })
78            .await
79            .map_err(|e| map_rpc_error("exists database", name, e))?
80            .into_inner();
81        Ok(resp.exists)
82    }
83
84    /// All database names the credentials can see.
85    pub async fn list_databases(&self) -> Result<Vec<String>> {
86        let resp = self
87            .client
88            .admin_stub()
89            .list_databases(ListDatabasesRequest {
90                credentials: Some(self.client.creds()),
91            })
92            .await
93            .map_err(|e| map_rpc_error("list databases", "", e))?
94            .into_inner();
95        Ok(resp.databases)
96    }
97
98    /// Server version / edition / uptime / ports.
99    pub async fn get_server_info(&self) -> Result<GetServerInfoResponse> {
100        Ok(self
101            .client
102            .admin_stub()
103            .get_server_info(GetServerInfoRequest {
104                credentials: Some(self.client.creds()),
105            })
106            .await
107            .map_err(|e| map_rpc_error("get server info", "", e))?
108            .into_inner())
109    }
110
111    /// Schema/types/statistics summary for one database.
112    pub async fn get_database_info(&self, name: &str) -> Result<GetDatabaseInfoResponse> {
113        Ok(self
114            .client
115            .admin_stub()
116            .get_database_info(GetDatabaseInfoRequest {
117                credentials: Some(self.client.creds()),
118                name: name.into(),
119            })
120            .await
121            .map_err(|e| map_rpc_error("get database info", name, e))?
122            .into_inner())
123    }
124}