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
use event_store_backend_postgres::PostgresBackend;
use event_store_core::{
    event_bus::{BoxedStream, EventBus},
    storage::Storage,
};
use event_store_eventbus_postgres::PostgresEventBus;

#[derive(Debug, Default)]
pub struct PostgresStorage {
    backend: PostgresBackend,
    event_bus: PostgresEventBus,
}

impl PostgresStorage {
    /// # Errors
    ///
    /// In case of Postgres connection error
    #[tracing::instrument(name = "PostgresBackend", skip(url))]
    pub async fn with_url(url: &str) -> Result<Self, sqlx::Error> {
        Ok(Self {
            backend: PostgresBackend::with_url(url).await?,
            // TODO Add DatabaseError convertor
            event_bus: PostgresEventBus::initiate(url.into()).await.unwrap(),
        })
    }
}

impl Storage for PostgresStorage {
    type Backend = PostgresBackend;
    type EventBus = PostgresEventBus;

    fn storage_name() -> &'static str {
        "Postgres"
    }

    fn backend(&mut self) -> &mut Self::Backend {
        &mut self.backend
    }

    fn create_stream(&mut self) -> BoxedStream {
        self.event_bus.create_stream()
    }
}