pub use docbox_core::database::*;
use crate::config::AdminDatabaseConfiguration;
pub trait DatabaseProvider: Send + Sync + 'static {
fn connect(&self, database: &str) -> impl Future<Output = DbResult<DbPool>> + Send;
}
pub struct CloseOnDrop(DbPool);
pub fn close_pool_on_drop(pool: &DbPool) -> CloseOnDrop {
CloseOnDrop(pool.clone())
}
impl Drop for CloseOnDrop {
fn drop(&mut self) {
let pool = self.0.clone();
tokio::spawn(async move {
tracing::debug!("closing dropped pool");
pool.close().await;
tracing::debug!("closed dropped pool");
});
}
}
pub struct ServerDatabaseProvider {
pub config: AdminDatabaseConfiguration,
pub username: String,
pub password: String,
}
impl DatabaseProvider for ServerDatabaseProvider {
fn connect(&self, database: &str) -> impl Future<Output = DbResult<DbPool>> + Send {
let options = PgConnectOptions::new()
.host(&self.config.host)
.port(self.config.port)
.username(&self.username)
.password(&self.password)
.database(database);
PgPool::connect_with(options)
}
}