Skip to main content

atomr_persistence_sql/
query.rs

1//! `ReadJournal` adapter that reuses the SQL journal for event-by-id /
2//! event-by-tag queries.
3
4use std::sync::Arc;
5
6use async_trait::async_trait;
7use atomr_persistence::{Journal, JournalError};
8use atomr_persistence_query::{EventEnvelope, ReadJournal};
9
10use crate::journal::SqlJournal;
11
12pub struct SqlReadJournal {
13    journal: Arc<SqlJournal>,
14}
15
16impl SqlReadJournal {
17    pub fn new(journal: Arc<SqlJournal>) -> Self {
18        Self { journal }
19    }
20
21    pub async fn events_by_tag(
22        &self,
23        tag: &str,
24        from_offset: u64,
25        max: u64,
26    ) -> Result<Vec<EventEnvelope>, JournalError> {
27        let reprs = self.journal.events_by_tag(tag, from_offset, max).await?;
28        Ok(reprs.into_iter().map(Into::into).collect())
29    }
30}
31
32#[async_trait]
33impl ReadJournal for SqlReadJournal {
34    async fn events_by_persistence_id(
35        &self,
36        persistence_id: &str,
37        from: u64,
38        to: u64,
39    ) -> Result<Vec<EventEnvelope>, JournalError> {
40        let reprs = self.journal.replay_messages(persistence_id, from, to, u64::MAX).await?;
41        Ok(reprs.into_iter().map(Into::into).collect())
42    }
43}