use cqrs_es::persist::PersistedEventStore;
use cqrs_es::{Aggregate, CqrsFramework, Query};
use crate::{SqliteCqrs, SqliteEventRepository};
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous};
use sqlx::{Pool, Sqlite};
use std::str::FromStr;
pub async fn default_sqlite_pool(connection_string: &str) -> Pool<Sqlite> {
let options = SqliteConnectOptions::from_str(connection_string)
.unwrap()
.create_if_missing(true)
.journal_mode(SqliteJournalMode::Wal)
.synchronous(SqliteSynchronous::Normal)
.foreign_keys(true);
SqlitePoolOptions::new()
.max_connections(10)
.connect_with(options)
.await
.expect("unable to connect to database")
}
pub fn sqlite_cqrs<A>(
pool: Pool<Sqlite>,
query_processor: Vec<Box<dyn Query<A>>>,
services: A::Services,
) -> SqliteCqrs<A>
where
A: Aggregate,
{
let repo = SqliteEventRepository::new(pool);
let store = PersistedEventStore::new_event_store(repo);
CqrsFramework::new(store, query_processor, services)
}
pub fn sqlite_snapshot_cqrs<A>(
pool: Pool<Sqlite>,
query_processor: Vec<Box<dyn Query<A>>>,
snapshot_size: usize,
services: A::Services,
) -> SqliteCqrs<A>
where
A: Aggregate,
{
let repo = SqliteEventRepository::new(pool);
let store = PersistedEventStore::new_snapshot_store(repo, snapshot_size);
CqrsFramework::new(store, query_processor, services)
}
pub fn sqlite_aggregate_cqrs<A>(
pool: Pool<Sqlite>,
query_processor: Vec<Box<dyn Query<A>>>,
services: A::Services,
) -> SqliteCqrs<A>
where
A: Aggregate,
{
let repo = SqliteEventRepository::new(pool);
let store = PersistedEventStore::new_aggregate_store(repo);
CqrsFramework::new(store, query_processor, services)
}
#[cfg(test)]
mod test {
use crate::testing::tests::{
TestAggregate, TestQueryRepository, TestServices, TestView, TEST_CONNECTION_STRING,
};
use crate::{default_sqlite_pool, sqlite_cqrs, SqliteViewRepository};
use std::sync::Arc;
#[tokio::test]
async fn test_valid_cqrs_framework() {
let pool = default_sqlite_pool(TEST_CONNECTION_STRING).await;
sqlx::migrate!().run(&pool).await.unwrap();
let repo = SqliteViewRepository::<TestView, TestAggregate>::new("test_view", pool.clone());
let query = TestQueryRepository::new(Arc::new(repo));
let _ps = sqlite_cqrs(pool, vec![Box::new(query)], TestServices);
}
}