aboutsummaryrefslogtreecommitdiffstats
path: root/lib/api-config/src/schema/implementation.rs
blob: c414879dcbd935cb4c27965dd68121d937da8089 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use async_trait::async_trait;
use tracing::debug;

use crate::schema::{self, SchemaDriver, SchemaService, TransactionSchema};

#[async_trait]
impl SchemaDriver for SchemaService {
    #[tracing::instrument(skip(self, schema))]
    async fn create_schema(
        &self,
        kind: &str,
        version: &str,
        schema: &serde_json::Value,
    ) -> Result<TransactionSchema, crate::ConfigurationError> {
        schema::create_schema::create_schema(self, kind, version, schema).await
    }

    #[tracing::instrument(skip(self))]
    async fn delete_schema(
        &self,
        kind: &str,
        version: &str,
    ) -> Result<(), crate::ConfigurationError> {
        schema::delete_schema::delete_schema(self, kind, version).await
    }

    #[tracing::instrument(skip(self))]
    async fn get_schema(
        &self,
        kind: &str,
        version: &str,
    ) -> Result<Option<TransactionSchema>, crate::ConfigurationError> {
        schema::get_schema::get_schema(self, kind, version).await
    }

    #[tracing::instrument(skip(self, schema))]
    async fn update_schema(
        &self,
        kind: &str,
        version: &str,
        schema: &serde_json::Value,
    ) -> Result<Option<TransactionSchema>, crate::ConfigurationError> {
        schema::update_schema::update_schema(self, kind, version, schema).await
    }

    #[tracing::instrument(skip(self))]
    async fn get_schemas(
        &self,
        limit: i64,
        first: Option<i64>,
        after: Option<&str>,
    ) -> Result<Vec<TransactionSchema>, crate::ConfigurationError> {
        debug!("getting transaction schemas");
        let limit = first.unwrap_or(limit);
        let mut last_type = String::default();
        let mut last_version = String::default();

        if let Some(s) = after {
            let parts: Vec<&str> = s.split(',').collect();
            if parts.len() == 2 {
                last_type = parts[0].to_string();
                last_version = parts[1].to_string();
            }
        }

        let rows = sqlx::query_as!(
            TransactionSchema,
            "
            select *
            from transaction_schema
            where ($1 = '' or (schema_type, schema_version) > ($1, $2))
            order by schema_type asc, schema_version asc
            limit $3
        ",
            &last_type,
            &last_version,
            limit + 1
        )
        .fetch_all(&self.database)
        .await?;

        Ok(rows)
    }
}