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::codemirror_bridge::SqlSchema;
16
17#[server(GetDbSchema, "/api")]
19pub async fn get_db_schema() -> Result<SqlSchema, ServerFnError> {
20 let _user = get_current_admin_user().await?;
21
22 #[cfg(feature = "server")]
23 {
24 let client = get_conn().await.map_err(AppError::db_conn)?;
25 let rows = client
26 .query(
27 "SELECT t.table_name, \
28 string_agg(c.column_name, ',' ORDER BY c.ordinal_position) \
29 FROM information_schema.tables t \
30 JOIN information_schema.columns c USING (table_schema, table_name) \
31 WHERE t.table_schema = 'public' AND t.table_type = 'BASE TABLE' \
32 GROUP BY t.table_name ORDER BY t.table_name",
33 &[],
34 )
35 .await
36 .map_err(AppError::query)?;
37 let tables = rows
38 .into_iter()
39 .map(|r| {
40 let cols: String = r.get(1);
41 crate::codemirror_bridge::SqlTable {
42 name: r.get(0),
43 columns: cols.split(',').map(|s| s.to_string()).collect(),
44 }
45 })
46 .collect();
47 Ok(SqlSchema { tables })
48 }
49 #[cfg(not(feature = "server"))]
50 {
51 Ok(SqlSchema::default())
52 }
53}