use crate::{
event::{Event, EventId, PersistedEvent},
stream_query::StreamQuery,
};
use async_trait::async_trait;
use futures::stream::BoxStream;
use std::error::Error as StdError;
#[async_trait]
pub trait EventStore<ID, E>
where
ID: EventId,
E: Event + Send + Sync,
{
type Error: Send + Sync;
fn stream<'a, QE>(
&'a self,
query: &'a StreamQuery<ID, QE>,
) -> BoxStream<'a, Result<StreamItem<ID, QE>, Self::Error>>
where
QE: TryFrom<E> + Event + 'static + Clone + Send + Sync,
<QE as TryFrom<E>>::Error: StdError + 'static + Send + Sync;
async fn append<QE>(
&self,
events: Vec<E>,
query: StreamQuery<ID, QE>,
last_event_id: ID,
) -> Result<Vec<PersistedEvent<ID, E>>, Self::Error>
where
E: Clone + 'async_trait,
QE: Event + 'static + Clone + Send + Sync;
async fn append_without_validation(
&self,
events: Vec<E>,
) -> Result<Vec<PersistedEvent<ID, E>>, Self::Error>
where
E: Clone + 'async_trait;
}
#[derive(Debug, Clone)]
pub enum StreamItem<ID: EventId, E: Event> {
Event(PersistedEvent<ID, E>),
End(ID),
}
impl<ID: EventId, E: Event> StreamItem<ID, E> {
pub fn id(&self) -> ID {
match self {
StreamItem::Event(event) => event.id,
StreamItem::End(id) => *id,
}
}
}
impl<ID: EventId, E: Event> From<PersistedEvent<ID, E>> for StreamItem<ID, E> {
fn from(event: PersistedEvent<ID, E>) -> Self {
StreamItem::Event(event)
}
}