2025-09-14 16:19:44 -04:00
|
|
|
use chrono::{DateTime, Utc};
|
2025-07-18 16:20:03 -07:00
|
|
|
use derive_builder::Builder;
|
2025-07-08 16:54:51 -07:00
|
|
|
use interim_pgtypes::pg_attribute::PgAttribute;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2025-08-04 13:59:42 -07:00
|
|
|
use sqlx::{Decode, Postgres, Row as _, TypeInfo as _, ValueRef as _, postgres::PgRow, query_as};
|
2025-07-08 16:54:51 -07:00
|
|
|
use thiserror::Error;
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2025-08-04 13:59:42 -07:00
|
|
|
use crate::client::AppDbClient;
|
2025-09-23 13:08:51 -07:00
|
|
|
use crate::datum::Datum;
|
2025-09-08 15:56:57 -07:00
|
|
|
use crate::presentation::Presentation;
|
|
|
|
|
|
|
|
|
|
/// A materialization of a database column, fit for consumption by an end user.
|
|
|
|
|
///
|
|
|
|
|
/// There may be zero or more fields per column/attribute in a Postgres view.
|
|
|
|
|
/// There may in some case also be fields with no underlying column, if it has
|
|
|
|
|
/// been removed or altered.
|
2025-07-08 16:54:51 -07:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
|
pub struct Field {
|
2025-09-08 15:56:57 -07:00
|
|
|
/// Internal ID for application use.
|
2025-07-18 16:20:03 -07:00
|
|
|
pub id: Uuid,
|
2025-09-08 15:56:57 -07:00
|
|
|
|
|
|
|
|
/// Name of the database column.
|
2025-07-08 16:54:51 -07:00
|
|
|
pub name: String,
|
2025-09-08 15:56:57 -07:00
|
|
|
|
|
|
|
|
/// Refer to documentation for `Presentation`.
|
|
|
|
|
pub presentation: sqlx::types::Json<Presentation>,
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
/// Optional human friendly label.
|
|
|
|
|
pub table_label: Option<String>,
|
|
|
|
|
|
2025-09-08 15:56:57 -07:00
|
|
|
/// Width of UI table column in pixels.
|
2025-09-14 16:19:44 -04:00
|
|
|
pub table_width_px: i32,
|
2025-07-08 16:54:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Field {
|
2025-09-08 15:56:57 -07:00
|
|
|
/// Constructs a brand new field to be inserted into the application db.
|
|
|
|
|
pub fn insert() -> InsertableFieldBuilder {
|
2025-07-18 16:20:03 -07:00
|
|
|
InsertableFieldBuilder::default()
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-08 15:56:57 -07:00
|
|
|
/// Generate a default field config based on an existing column's name and
|
|
|
|
|
/// type.
|
|
|
|
|
pub fn default_from_attr(attr: &PgAttribute) -> Option<Self> {
|
|
|
|
|
Presentation::default_from_attr(attr).map(|presentation| Self {
|
2025-07-18 16:20:03 -07:00
|
|
|
id: Uuid::now_v7(),
|
2025-07-08 16:54:51 -07:00
|
|
|
name: attr.attname.clone(),
|
2025-09-14 16:19:44 -04:00
|
|
|
table_label: None,
|
2025-09-08 15:56:57 -07:00
|
|
|
presentation: sqlx::types::Json(presentation),
|
2025-09-14 16:19:44 -04:00
|
|
|
table_width_px: 200,
|
2025-09-08 15:56:57 -07:00
|
|
|
})
|
2025-07-08 16:54:51 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 13:08:51 -07:00
|
|
|
pub fn get_datum(&self, row: &PgRow) -> Result<Datum, ParseError> {
|
2025-07-08 16:54:51 -07:00
|
|
|
let value_ref = row
|
|
|
|
|
.try_get_raw(self.name.as_str())
|
|
|
|
|
.or(Err(ParseError::FieldNotFound))?;
|
|
|
|
|
let type_info = value_ref.type_info();
|
|
|
|
|
let ty = type_info.name();
|
2025-09-08 15:56:57 -07:00
|
|
|
dbg!(&ty);
|
2025-07-08 16:54:51 -07:00
|
|
|
Ok(match ty {
|
|
|
|
|
"TEXT" | "VARCHAR" => {
|
2025-09-23 13:08:51 -07:00
|
|
|
Datum::Text(<Option<String> as Decode<Postgres>>::decode(value_ref).unwrap())
|
2025-07-08 16:54:51 -07:00
|
|
|
}
|
2025-09-23 13:08:51 -07:00
|
|
|
"UUID" => Datum::Uuid(<Option<Uuid> as Decode<Postgres>>::decode(value_ref).unwrap()),
|
|
|
|
|
"TIMESTAMPTZ" => Datum::Timestamp(
|
2025-09-14 16:19:44 -04:00
|
|
|
<Option<DateTime<Utc>> as Decode<Postgres>>::decode(value_ref).unwrap(),
|
|
|
|
|
),
|
2025-07-08 16:54:51 -07:00
|
|
|
_ => return Err(ParseError::UnknownType),
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-08-04 13:59:42 -07:00
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
pub fn belonging_to_portal(portal_id: Uuid) -> BelongingToPortalQuery {
|
|
|
|
|
BelongingToPortalQuery { portal_id }
|
2025-08-04 13:59:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2025-09-14 16:19:44 -04:00
|
|
|
pub struct BelongingToPortalQuery {
|
|
|
|
|
portal_id: Uuid,
|
2025-08-04 13:59:42 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 16:19:44 -04:00
|
|
|
impl BelongingToPortalQuery {
|
2025-08-04 13:59:42 -07:00
|
|
|
pub async fn fetch_all(self, app_db: &mut AppDbClient) -> Result<Vec<Field>, sqlx::Error> {
|
|
|
|
|
query_as!(
|
|
|
|
|
Field,
|
|
|
|
|
r#"
|
|
|
|
|
select
|
|
|
|
|
id,
|
|
|
|
|
name,
|
2025-09-14 16:19:44 -04:00
|
|
|
table_label,
|
2025-09-08 15:56:57 -07:00
|
|
|
presentation as "presentation: sqlx::types::Json<Presentation>",
|
2025-09-14 16:19:44 -04:00
|
|
|
table_width_px
|
2025-08-04 13:59:42 -07:00
|
|
|
from fields
|
2025-09-14 16:19:44 -04:00
|
|
|
where portal_id = $1
|
2025-08-04 13:59:42 -07:00
|
|
|
"#,
|
2025-09-14 16:19:44 -04:00
|
|
|
self.portal_id
|
2025-08-04 13:59:42 -07:00
|
|
|
)
|
|
|
|
|
.fetch_all(&mut *app_db.conn)
|
|
|
|
|
.await
|
|
|
|
|
}
|
2025-07-08 16:54:51 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-18 16:20:03 -07:00
|
|
|
#[derive(Builder, Clone, Debug)]
|
|
|
|
|
pub struct InsertableField {
|
2025-09-14 16:19:44 -04:00
|
|
|
portal_id: Uuid,
|
2025-07-18 16:20:03 -07:00
|
|
|
name: String,
|
|
|
|
|
#[builder(default)]
|
2025-09-14 16:19:44 -04:00
|
|
|
table_label: Option<String>,
|
2025-09-08 15:56:57 -07:00
|
|
|
presentation: Presentation,
|
2025-07-18 16:20:03 -07:00
|
|
|
#[builder(default = 200)]
|
2025-09-14 16:19:44 -04:00
|
|
|
table_width_px: i32,
|
2025-07-18 16:20:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InsertableField {
|
2025-08-04 13:59:42 -07:00
|
|
|
pub async fn insert(self, app_db: &mut AppDbClient) -> Result<Field, sqlx::Error> {
|
2025-07-18 16:20:03 -07:00
|
|
|
query_as!(
|
|
|
|
|
Field,
|
|
|
|
|
r#"
|
|
|
|
|
insert into fields
|
2025-09-14 16:19:44 -04:00
|
|
|
(portal_id, name, table_label, presentation, table_width_px)
|
|
|
|
|
values ($1, $2, $3, $4, $5)
|
2025-07-18 16:20:03 -07:00
|
|
|
returning
|
|
|
|
|
id,
|
|
|
|
|
name,
|
2025-09-14 16:19:44 -04:00
|
|
|
table_label,
|
2025-09-08 15:56:57 -07:00
|
|
|
presentation as "presentation: sqlx::types::Json<Presentation>",
|
2025-09-14 16:19:44 -04:00
|
|
|
table_width_px
|
2025-07-18 16:20:03 -07:00
|
|
|
"#,
|
2025-09-14 16:19:44 -04:00
|
|
|
self.portal_id,
|
2025-07-18 16:20:03 -07:00
|
|
|
self.name,
|
2025-09-14 16:19:44 -04:00
|
|
|
self.table_label,
|
2025-09-08 15:56:57 -07:00
|
|
|
sqlx::types::Json::<_>(self.presentation) as sqlx::types::Json<Presentation>,
|
2025-09-14 16:19:44 -04:00
|
|
|
self.table_width_px,
|
2025-07-18 16:20:03 -07:00
|
|
|
)
|
2025-08-04 13:59:42 -07:00
|
|
|
.fetch_one(&mut *app_db.conn)
|
2025-07-18 16:20:03 -07:00
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InsertableFieldBuilder {
|
|
|
|
|
pub fn default_from_attr(attr: &PgAttribute) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
name: Some(attr.attname.clone()),
|
2025-09-08 15:56:57 -07:00
|
|
|
presentation: Presentation::default_from_attr(attr),
|
2025-07-18 16:20:03 -07:00
|
|
|
..Self::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 16:54:51 -07:00
|
|
|
/// Error when parsing a sqlx value to JSON
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
|
pub enum ParseError {
|
2025-09-14 16:19:44 -04:00
|
|
|
// TODO: can this be removed?
|
2025-07-08 16:54:51 -07:00
|
|
|
#[error("incompatible json type")]
|
|
|
|
|
BadJsonType,
|
2025-09-14 16:19:44 -04:00
|
|
|
|
2025-07-08 16:54:51 -07:00
|
|
|
#[error("field not found in row")]
|
|
|
|
|
FieldNotFound,
|
|
|
|
|
#[error("unknown postgres type")]
|
|
|
|
|
UnknownType,
|
|
|
|
|
}
|