Skip to main content

sbol_db_postgres/repo/
projection.rs

1use sbol_db_core::{DomainError, IriString};
2use serde_json::Value;
3
4use crate::repo::db_err;
5use crate::PgPool;
6
7#[derive(Clone)]
8pub struct ProjectionEventRepository {
9    _pool: PgPool,
10}
11
12#[derive(Clone, Debug)]
13pub struct ProjectionEvent {
14    pub event_type: String,
15    pub subject_iri: Option<IriString>,
16    pub graph_iri: Option<IriString>,
17    pub payload: Value,
18}
19
20impl ProjectionEventRepository {
21    pub fn new(pool: PgPool) -> Self {
22        Self { _pool: pool }
23    }
24
25    pub async fn append(
26        &self,
27        conn: &mut sqlx::PgConnection,
28        event: ProjectionEvent,
29    ) -> Result<i64, DomainError> {
30        use sqlx::Row;
31        let row = sqlx::query(
32            r#"
33            INSERT INTO sbol_rdf_projection_events (event_type, subject_iri, graph_iri, payload)
34            VALUES ($1, $2, $3, $4)
35            RETURNING id
36            "#,
37        )
38        .bind(event.event_type)
39        .bind(event.subject_iri.as_ref().map(|i| i.as_str()))
40        .bind(event.graph_iri.as_ref().map(|i| i.as_str()))
41        .bind(event.payload)
42        .fetch_one(&mut *conn)
43        .await
44        .map_err(db_err)?;
45        let id: i64 = row.try_get("id").map_err(db_err)?;
46        Ok(id)
47    }
48}