use cqrs_es::persist::PersistedEventStore;
use cqrs_es::{Aggregate, CqrsFramework, Query};
use crate::{PostgresCqrs, PostgresEventRepository};
use sqlx::postgres::PgPoolOptions;
use sqlx::{Pool, Postgres};
pub async fn default_postgress_pool(connection_string: &str) -> Pool<Postgres> {
PgPoolOptions::new()
.max_connections(10)
.connect(connection_string)
.await
.expect("unable to connect to database")
}
pub fn postgres_cqrs<A>(
pool: Pool<Postgres>,
query_processor: Vec<Box<dyn Query<A>>>,
services: A::Services,
) -> PostgresCqrs<A>
where
A: Aggregate,
{
let repo = PostgresEventRepository::new(pool);
let store = PersistedEventStore::new_event_store(repo);
CqrsFramework::new(store, query_processor, services)
}
pub fn postgres_snapshot_cqrs<A>(
pool: Pool<Postgres>,
query_processor: Vec<Box<dyn Query<A>>>,
snapshot_size: usize,
services: A::Services,
) -> PostgresCqrs<A>
where
A: Aggregate,
{
let repo = PostgresEventRepository::new(pool);
let store = PersistedEventStore::new_snapshot_store(repo, snapshot_size);
CqrsFramework::new(store, query_processor, services)
}
pub fn postgres_aggregate_cqrs<A>(
pool: Pool<Postgres>,
query_processor: Vec<Box<dyn Query<A>>>,
services: A::Services,
) -> PostgresCqrs<A>
where
A: Aggregate,
{
let repo = PostgresEventRepository::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_postgress_pool, postgres_cqrs, PostgresViewRepository};
use std::sync::Arc;
#[tokio::test]
async fn test_valid_cqrs_framework() {
let pool = default_postgress_pool(TEST_CONNECTION_STRING).await;
let repo =
PostgresViewRepository::<TestView, TestAggregate>::new("test_view", pool.clone());
let query = TestQueryRepository::new(Arc::new(repo));
let _ps = postgres_cqrs(pool, vec![Box::new(query)], TestServices);
}
}