2024-06-20 14:50:36 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use anyhow::Error;
|
|
|
|
|
use sqlx::{self, postgres::PgPoolOptions, Executor, Pool, Postgres};
|
|
|
|
|
|
|
|
|
|
use crate::config;
|
|
|
|
|
|
|
|
|
|
pub type DB = Pool<Postgres>;
|
|
|
|
|
pub trait Queryer<'c>: Executor<'c, Database = sqlx::Postgres> {}
|
|
|
|
|
|
2024-12-17 14:23:27 +01:00
|
|
|
impl Queryer<'_> for &DB {}
|
2024-06-20 14:50:36 +02:00
|
|
|
|
|
|
|
|
pub async fn connect(database: &config::Database) -> Result<DB, Error> {
|
|
|
|
|
PgPoolOptions::new()
|
|
|
|
|
.max_connections(database.pool_size)
|
|
|
|
|
.max_lifetime(Duration::from_secs(database.max_lifetime))
|
|
|
|
|
.connect(&database.url)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
tracing::error!("{}", err);
|
|
|
|
|
err.into()
|
|
|
|
|
})
|
|
|
|
|
}
|