based/src/lib.rs
2025-01-20 05:51:22 +01:00

49 lines
1.2 KiB
Rust

#![feature(const_vec_string_slice)]
use tokio::sync::OnceCell;
pub mod auth;
pub mod format;
#[cfg(feature = "htmx")]
pub mod htmx;
pub mod request;
pub mod result;
pub mod ui;
// TODO : CORS?
// Postgres
// TODO : IDEA
// more efficient table join using WHERE ANY instead of multiple SELECTs
// map_tables(Vec<T>, Fn(&T) -> U) -> Vec<U>
pub static PG: OnceCell<sqlx::PgPool> = OnceCell::const_new();
/// A macro to retrieve or initialize the `PostgreSQL` connection pool.
///
/// This macro provides a convenient way to access the `PgPool`. If the pool is not already initialized,
/// it creates a new pool using the connection string from the `$DATABASE_URL` environment variable.
///
/// # Example
/// ```ignore
/// use based::get_pg;
///
/// let pool = get_pg!();
/// ```
#[macro_export]
macro_rules! get_pg {
() => {
if let Some(client) = $crate::PG.get() {
client
} else {
let client = sqlx::postgres::PgPoolOptions::new()
.max_connections(5)
.connect(&std::env::var("DATABASE_URL").unwrap())
.await
.unwrap();
$crate::PG.set(client).unwrap();
$crate::PG.get().unwrap()
}
};
}