kabu_storage_db/
pool.rs

1use diesel_async::pooled_connection::AsyncDieselConnectionManager;
2use diesel_async::AsyncPgConnection;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum PoolError {
7    #[error("Failed to create connection pool: {0}")]
8    PoolError(String),
9}
10
11pub type DbPool = bb8::Pool<AsyncDieselConnectionManager<AsyncPgConnection>>;
12
13pub async fn init_db_pool(db_url: String) -> Result<DbPool, PoolError> {
14    // set up connection pool using bb8 with diesel-async manager
15    let manager = AsyncDieselConnectionManager::<AsyncPgConnection>::new(db_url);
16    let pool = bb8::Pool::builder().build(manager).await.map_err(|e| PoolError::PoolError(e.to_string()))?;
17    Ok(pool)
18}