Simplify db code by removing r2d2

This commit is contained in:
Matthew Gordon 2024-02-19 21:55:15 -04:00
parent d18ce42938
commit c502f87fa3
3 changed files with 15 additions and 32 deletions

View File

@ -11,7 +11,6 @@ axum = "0.7"
askama = "0.12" askama = "0.12"
askama_axum = "0.4" askama_axum = "0.4"
tower-http = { version = "0.5", features = ["fs"] } tower-http = { version = "0.5", features = ["fs"] }
r2d2_postgres = "0.18" deadpool-postgres = { version = "0.12", features = ["rt_tokio_1"] }
deadpool-r2d2 = {version = "0.3", features = ["rt_tokio_1"]} tokio-postgres = { version = "0.7", features = ["with-chrono-0_4"] }
deadpool = "0.10"
thiserror = "1.0" thiserror = "1.0"

View File

@ -1,47 +1,31 @@
use { use {
deadpool, deadpool_r2d2::Runtime, thiserror::Error, deadpool_postgres::{CreatePoolError, Pool, Runtime},
thiserror::Error,
tokio_postgres::NoTls,
}; };
type PgManager = deadpool_r2d2::Manager<
r2d2_postgres::PostgresConnectionManager<r2d2_postgres::postgres::NoTls>,
>;
type PgPool = deadpool_r2d2::Pool<PgManager>;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
#[error("Could not initialize DB connection pool.")] #[error("Could not initialize DB connection pool.")]
ConnectionPoolError(#[from] deadpool::managed::BuildError), ConnectionPoolError(#[from] CreatePoolError),
#[error("Error with Postgres database")]
PostrgesError(#[from] r2d2_postgres::postgres::Error)
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Database { pub struct Database {
pg_pool: PgPool, pg_pool: Pool,
} }
impl Database { impl Database {
pub fn create_pool(connection_url: &str, max_size: usize) -> Result<Database, Error> { pub fn create_pool(connection_url: &str) -> Result<Database, Error> {
let pg_config: r2d2_postgres::postgres::Config = connection_url.parse()?; let mut config = deadpool_postgres::Config::new();
let r2d2_manager = r2d2_postgres::PostgresConnectionManager::new( config.url = Some(connection_url.to_string());
pg_config, let pg_pool = config.create_pool(Some(Runtime::Tokio1), NoTls)?;
r2d2_postgres::postgres::NoTls,
);
let manager = PgManager::new(r2d2_manager, Runtime::Tokio1);
let pg_pool = PgPool::builder(manager).max_size(max_size).build()?;
Ok(Database { pg_pool }) Ok(Database { pg_pool })
} }
pub async fn log_num_users(&self) -> usize { pub async fn log_num_users(&self) -> usize {
self.pg_pool let client = self.pg_pool.get().await.unwrap();
.get() let results = client.query("SELECT * FROM users;", &[]).await.unwrap();
.await
.unwrap()
.interact(|conn| {
let results = conn.query("SELECT * FROM users;", &[]).unwrap();
results.len() results.len()
})
.await
.unwrap()
} }
} }

View File

@ -33,7 +33,7 @@ fn main() {
async fn localhub_main() -> Result<(), Error> { async fn localhub_main() -> Result<(), Error> {
let config = get_config()?; let config = get_config()?;
let db_pool = Database::create_pool(&config.database_url, 2)?; let db_pool = Database::create_pool(&config.database_url)?;
let app = app::routes() let app = app::routes()
.with_state(db_pool) .with_state(db_pool)