36 lines
1.1 KiB
Rust
36 lines
1.1 KiB
Rust
![]() |
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<AsyncPgConnection>;
|
||
|
|
||
|
#[async_trait]
|
||
|
pub trait GetConnection: Clone + Send + Sync {
|
||
|
async fn get(&self) -> Result<Object<AsyncPgConnection>, AppError>;
|
||
|
fn status(&self) -> Status;
|
||
|
}
|
||
|
|
||
|
#[async_trait]
|
||
|
impl GetConnection for PgPool {
|
||
|
async fn get(&self) -> Result<Object<AsyncPgConnection>, AppError> {
|
||
|
self.get().await.map_err(Into::into)
|
||
|
}
|
||
|
fn status(&self) -> Status {
|
||
|
self.status()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub(crate) fn create_pool() -> Result<PgPool, BuildError> {
|
||
|
create_pool_from_url(config::DATABASE_URL)
|
||
|
}
|
||
|
|
||
|
pub(crate) fn create_pool_from_url(url: impl Into<String>) -> Result<PgPool, BuildError> {
|
||
|
let config = AsyncDieselConnectionManager::<AsyncPgConnection>::new(url);
|
||
|
Pool::builder(config).max_size(config::POOL_SIZE).build()
|
||
|
}
|