-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
49 lines (37 loc) · 1.02 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::sync::OnceLock;
use std::{
sync::Arc,
thread,
time::{Duration, Instant},
};
use anyhow::Result;
use tokio_postgres::NoTls;
use vantage::prelude::Postgres;
pub mod bakery;
pub use bakery::*;
pub mod client;
pub use client::*;
pub mod product;
pub use product::*;
pub mod lineitem;
pub use lineitem::*;
pub mod order;
pub use order::*;
static POSTGRESS: OnceLock<Postgres> = OnceLock::new();
pub fn set_postgres(postgres: Postgres) -> Result<()> {
POSTGRESS
.set(postgres)
.map_err(|e| anyhow::anyhow!("Failed to set Postgres instance: {:?}", e))
}
pub fn postgres() -> Postgres {
POSTGRESS
.get()
.expect("Postgres has not been initialized. use connect_postgress()")
.clone()
}
pub async fn connect_postgres() -> Result<()> {
let connection_string = std::env::var("DATABASE_URL")
.unwrap_or_else(|_| "postgres://postgres@localhost:5432/postgres".to_string());
let postgres = Postgres::new(&connection_string).await;
set_postgres(postgres)
}