Initial Commit
This commit is contained in:
28
src/lib/outbound/postgres.rs
Normal file
28
src/lib/outbound/postgres.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
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()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user