mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2025-03-04 07:18:50 -07:00
35 lines
927 B
Rust
35 lines
927 B
Rust
pub mod error;
|
|
|
|
use sea_orm::{Database, DatabaseConnection};
|
|
|
|
use crate::error::Error;
|
|
|
|
static DB_CONN: once_cell::sync::OnceCell<DatabaseConnection> = once_cell::sync::OnceCell::new();
|
|
|
|
pub async fn init_database(connection_uri: impl Into<String>) -> Result<(), Error> {
|
|
let conn = Database::connect(connection_uri.into()).await?;
|
|
DB_CONN.get_or_init(move || conn);
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get_database() -> Result<&'static DatabaseConnection, Error> {
|
|
DB_CONN.get().ok_or(Error::Uninitialized)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::get_database;
|
|
use crate::{error::Error, init_database};
|
|
|
|
#[test]
|
|
fn unit_lib_error_uninitialized() {
|
|
assert_eq!(get_database().unwrap_err(), Error::Uninitialized);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn unit_lib_connect_in_memory_sqlite() -> Result<(), Error> {
|
|
init_database("sqlite::memory:").await?;
|
|
get_database()?;
|
|
Ok(())
|
|
}
|
|
}
|