use crate::config; use crate::error::AppError; use axum::async_trait; use deadpool_diesel::postgres::BuildError; use deadpool_diesel::Status; use diesel_async::pooled_connection::deadpool::{Object, Pool}; use diesel_async::pooled_connection::AsyncDieselConnectionManager; use diesel_async::AsyncPgConnection; pub type PgPool = Pool; #[async_trait] pub trait GetConnection: Clone + Send + Sync { async fn get(&self) -> Result, AppError>; fn status(&self) -> Status; } #[async_trait] impl GetConnection for PgPool { async fn get(&self) -> Result, AppError> { self.get().await.map_err(Into::into) } fn status(&self) -> Status { self.status() } } pub(crate) fn create_pool() -> Result { create_pool_from_url(config::DATABASE_URL) } pub(crate) fn create_pool_from_url(url: impl Into) -> Result { let config = AsyncDieselConnectionManager::::new(url); Pool::builder(config).max_size(config::POOL_SIZE).build() }