Files
avam/src/lib/outbound/postgres.rs
2024-10-17 00:56:02 +02:00

30 lines
692 B
Rust

pub mod oauth_repository;
pub mod user_repository;
use std::str::FromStr;
use anyhow::Context;
use sqlx::{postgres::PgConnectOptions, PgPool, Pool};
#[derive(Debug, Clone)]
pub struct Postgres {
pool: Pool<sqlx::Postgres>,
}
impl Postgres {
pub async fn new(url: &str) -> Result<Self, anyhow::Error> {
let pool = PgPool::connect_with(
PgConnectOptions::from_str(url)
.with_context(|| format!("Invalid database url: {}", url))?,
)
.await
.with_context(|| format!("Failed to open database at {}", url))?;
Ok(Self { pool })
}
pub fn pool(&self) -> Pool<sqlx::Postgres> {
self.pool.clone()
}
}