Skip to main content

arcly_http/data/drivers/
seaorm.rs

1//! SeaORM adapter — `DatabaseConnection` behind the unified facade.
2//!
3//! SeaORM wraps SQLx internally, so the connection is `Clone + Send + Sync`
4//! with lock-free async acquisition; it drops into `DbDriver` directly.
5//! Entity/ActiveModel code keeps full access to the native connection by
6//! matching `DbDriver::SeaOrm(conn)` — the facade never hides it.
7
8use crate::data::db::DbDriver;
9use crate::data::DataError;
10
11/// Build a SeaORM-backed driver for [`ArclyDbPool`](crate::data::db::ArclyDbPool).
12///
13/// ```ignore
14/// let primary = seaorm_driver("postgres://app@db-primary/orders").await?;
15/// ctx.provide(DataSourceRegistry::new(ArclyDbPool::new("default", primary)));
16/// ```
17pub async fn seaorm_driver(url: &str) -> Result<DbDriver, DataError> {
18    let conn = sea_orm::Database::connect(url)
19        .await
20        .map_err(|e| DataError::connection(format!("sea-orm connect failed: {e}")))?;
21    Ok(DbDriver::SeaOrm(conn))
22}
23
24/// Native handle accessor for entity queries.
25pub fn native(driver: &DbDriver) -> Option<&sea_orm::DatabaseConnection> {
26    match driver {
27        DbDriver::SeaOrm(c) => Some(c),
28        #[allow(unreachable_patterns)]
29        _ => None,
30    }
31}