use ff_core::backend::{BackendConfig, BackendConnection, PostgresConnection};
use ff_core::engine_error::EngineError;
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
use crate::error::map_sqlx_error;
pub async fn build_pool(config: &BackendConfig) -> Result<PgPool, EngineError> {
let BackendConnection::Postgres(pg) = &config.connection else {
return Err(EngineError::Unavailable {
op: "pg.pool.build (non-Postgres BackendConnection)",
});
};
build_pool_from_connection(pg).await
}
pub async fn build_pool_from_connection(pg: &PostgresConnection) -> Result<PgPool, EngineError> {
PgPoolOptions::new()
.max_connections(pg.max_connections)
.min_connections(pg.min_connections)
.acquire_timeout(pg.acquire_timeout)
.connect(&pg.url)
.await
.map_err(map_sqlx_error)
}