From 3fb3d0214a69017d49c91aa180fb565533edcc54 Mon Sep 17 00:00:00 2001 From: rtkay123 Date: Sun, 29 Mar 2026 16:51:09 +0200 Subject: refactor: use api-config to create schema --- lib/api-config/Cargo.toml | 1 + lib/api-config/src/error.rs | 4 ++-- lib/api-config/src/schema/create.rs | 32 ++++++++++++++++++++++++++++++++ lib/api-config/src/schema/mod.rs | 12 +++++++----- 4 files changed, 42 insertions(+), 7 deletions(-) (limited to 'lib/api-config') diff --git a/lib/api-config/Cargo.toml b/lib/api-config/Cargo.toml index ec31262..b114944 100644 --- a/lib/api-config/Cargo.toml +++ b/lib/api-config/Cargo.toml @@ -9,6 +9,7 @@ homepage.workspace = true publish.workspace = true [dependencies] +async-trait.workspace = true serde.workspace = true serde_json.workspace = true thiserror.workspace = true diff --git a/lib/api-config/src/error.rs b/lib/api-config/src/error.rs index 6f7b099..b2dc9ad 100644 --- a/lib/api-config/src/error.rs +++ b/lib/api-config/src/error.rs @@ -2,8 +2,8 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum ConfigurationError { - #[error("data store disconnected")] - Disconnect(#[from] sqlx::Error), + #[error(transparent)] + Database(#[from] sqlx::Error), #[error("the data for key `{0}` is not available")] Redaction(String), #[error("invalid header (expected {expected:?}, found {found:?})")] diff --git a/lib/api-config/src/schema/create.rs b/lib/api-config/src/schema/create.rs index 5c91f64..de2d214 100644 --- a/lib/api-config/src/schema/create.rs +++ b/lib/api-config/src/schema/create.rs @@ -1,4 +1,8 @@ +use async_trait::async_trait; use serde::{Deserialize, Serialize}; +use warden_core::state::AppState; + +use crate::schema::{SchemaDriver, TransactionSchema}; #[derive(Deserialize, Serialize)] #[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] @@ -13,3 +17,31 @@ pub struct CreateSchema { #[serde(rename = "json_schema")] pub schema: serde_json::Value, } + +#[async_trait] +impl SchemaDriver for AppState { + async fn create_schema( + &self, + name: impl AsRef + Send + Sync, + version: impl AsRef + Send + Sync, + schema: &serde_json::Value, + ) -> Result { + sqlx::query_as!( + TransactionSchema, + "insert into transaction_schema (type, version, json_schema) values ($1, $2, $3) + returning + type as kind, + version, + json_schema as schema, + created_at, + updated_at + ", + name.as_ref(), + version.as_ref(), + sqlx::types::Json(&schema) as _ + ) + .fetch_one(&self.database) + .await + .map_err(|e| e.into()) + } +} diff --git a/lib/api-config/src/schema/mod.rs b/lib/api-config/src/schema/mod.rs index 654e6ad..3893254 100644 --- a/lib/api-config/src/schema/mod.rs +++ b/lib/api-config/src/schema/mod.rs @@ -1,5 +1,6 @@ pub mod create; +use async_trait::async_trait; use serde::{Deserialize, Serialize}; use time::OffsetDateTime; @@ -23,11 +24,12 @@ pub struct TransactionSchema { pub updated_at: OffsetDateTime, } +#[async_trait] pub trait SchemaDriver { - fn create( + async fn create_schema( &self, - name: impl AsRef, - version: impl AsRef, - schema: serde_json::Value, - ) -> impl std::future::Future> + Send + Sync; + name: impl AsRef + Send + Sync, + version: impl AsRef + Send + Sync, + schema: &serde_json::Value, + ) -> Result; } -- cgit v1.2.3