Created
July 20, 2020 11:48
-
-
Save vasilakisfil/1e65f60eff71664c8ad0247e71870d7c to your computer and use it in GitHub Desktop.
deadpool postgres simple setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use deadpool_postgres::tokio_postgres::NoTls; | |
use deadpool_postgres::{Config, ManagerConfig, Pool, PoolError, RecyclingMethod}; | |
use once_cell::sync::Lazy; | |
use std::sync::Arc; | |
static DB_POOL: Lazy<Arc<Pool>> = Lazy::new(|| { | |
let mut cfg = Config::new(); | |
cfg.dbname = Some( | |
std::env::var("DATABASE_URL") | |
.map_err(|_| String::from("Environment variable Database URL could not be read")) | |
.unwrap(), | |
); | |
cfg.manager = Some(ManagerConfig { | |
recycling_method: RecyclingMethod::Fast, | |
}); | |
let pool = cfg.create_pool(NoTls).unwrap(); | |
Arc::new(pool) | |
}); | |
type ManagedObject = deadpool::managed::Object< | |
deadpool_postgres::ClientWrapper, | |
deadpool_postgres::tokio_postgres::error::Error, | |
>; | |
pub async fn db_conn() -> Result<ManagedObject, PoolError> { | |
DB_POOL.get().await | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment