aboutsummaryrefslogtreecommitdiffstats
path: root/lib/api-config/src/schema/create.rs
blob: de2d2145c2b5fe888494616572daafc69bc21053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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))]
/// Transaction to monitor
pub struct CreateSchema {
    #[serde(rename = "type")]
    /// Transaction schema type
    pub kind: String,
    /// The schema's version
    pub version: String,
    /// Transaction data
    #[serde(rename = "json_schema")]
    pub schema: serde_json::Value,
}

#[async_trait]
impl SchemaDriver for AppState {
    async fn create_schema(
        &self,
        name: impl AsRef<str> + Send + Sync,
        version: impl AsRef<str> + Send + Sync,
        schema: &serde_json::Value,
    ) -> Result<super::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
        ",
            name.as_ref(),
            version.as_ref(),
            sqlx::types::Json(&schema) as _
        )
        .fetch_one(&self.database)
        .await
        .map_err(|e| e.into())
    }
}