diff options
Diffstat (limited to 'lib/api-config/src/schema/mod.rs')
| -rw-r--r-- | lib/api-config/src/schema/mod.rs | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/lib/api-config/src/schema/mod.rs b/lib/api-config/src/schema/mod.rs index 3893254..b058060 100644 --- a/lib/api-config/src/schema/mod.rs +++ b/lib/api-config/src/schema/mod.rs @@ -1,8 +1,10 @@ -pub mod create; +mod create; +pub use create::CreateSchema; use async_trait::async_trait; use serde::{Deserialize, Serialize}; use time::OffsetDateTime; +use warden_core::state::AppState; use crate::ConfigurationError; @@ -28,8 +30,57 @@ pub struct TransactionSchema { pub trait SchemaDriver { async fn create_schema( &self, - name: impl AsRef<str> + Send + Sync, + kind: impl AsRef<str> + Send + Sync, version: impl AsRef<str> + Send + Sync, schema: &serde_json::Value, ) -> Result<TransactionSchema, ConfigurationError>; + + async fn delete_schema( + &self, + kind: impl AsRef<str> + Send + Sync, + version: impl AsRef<str> + Send + Sync, + ) -> Result<(), ConfigurationError>; +} + +#[async_trait] +impl SchemaDriver for AppState { + async fn create_schema( + &self, + kind: impl AsRef<str> + Send + Sync, + version: impl AsRef<str> + Send + Sync, + schema: &serde_json::Value, + ) -> Result<TransactionSchema, crate::ConfigurationError> { + 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 + ", + kind.as_ref(), + version.as_ref(), + sqlx::types::Json(&schema) as _ + ) + .fetch_one(&self.database) + .await + .map_err(|e| e.into()) + } + + async fn delete_schema( + &self, + kind: impl AsRef<str> + Send + Sync, + version: impl AsRef<str> + Send + Sync, + ) -> Result<(), crate::ConfigurationError> { + // sqlx::query!("delete * from transaction_schema where type = $1 and version $2", + // name.as_ref(), + // version.as_ref(), + // ) + // .execute(&self.database) + // .await + // .map_err(|e| e.into()) + Ok(()) + } } |
