yggdrasil/api/database/
schema.rs1#![allow(clippy::unused_unit, deprecated)]
2
3use dioxus::prelude::*;
6
7#[cfg(feature = "server")]
9use crate::api::auth::get_current_admin_user;
10#[cfg(feature = "server")]
11use crate::api::error::AppError;
12#[cfg(feature = "server")]
13use crate::db::pool::get_conn;
14use crate::models::sql_schema::SqlSchema;
17#[cfg(feature = "server")]
18use crate::models::sql_schema::SqlTable;
19
20#[server(GetDbSchema, "/api")]
22pub async fn get_db_schema() -> Result<SqlSchema, ServerFnError> {
23 let _user = get_current_admin_user().await?;
24
25 #[cfg(feature = "server")]
26 {
27 let client = get_conn().await.map_err(AppError::db_conn)?;
28 let rows = client
29 .query(
30 "SELECT t.table_name, \
31 string_agg(c.column_name, ',' ORDER BY c.ordinal_position) \
32 FROM information_schema.tables t \
33 JOIN information_schema.columns c USING (table_schema, table_name) \
34 WHERE t.table_schema = 'public' AND t.table_type = 'BASE TABLE' \
35 GROUP BY t.table_name ORDER BY t.table_name",
36 &[],
37 )
38 .await
39 .map_err(AppError::query)?;
40 let tables = rows
41 .into_iter()
42 .map(|r| {
43 let cols: String = r.get(1);
44 SqlTable {
45 name: r.get(0),
46 columns: cols.split(',').map(|s| s.to_string()).collect(),
47 }
48 })
49 .collect();
50 Ok(SqlSchema { tables })
51 }
52 #[cfg(not(feature = "server"))]
53 {
54 Ok(SqlSchema::default())
55 }
56}