systemprompt_runtime/
database_context.rs1use crate::error::RuntimeResult;
10use std::sync::Arc;
11use systemprompt_database::{Database, DbPool};
12
13#[derive(Debug, Clone)]
14pub struct DatabaseContext {
15 database: DbPool,
16}
17
18impl DatabaseContext {
19 #[must_use]
20 pub const fn from_pool(pool: DbPool) -> Self {
21 Self { database: pool }
22 }
23
24 pub async fn from_url(database_url: &str) -> RuntimeResult<Self> {
25 let db = Database::new_postgres(database_url).await?;
26 Ok(Self {
27 database: Arc::new(db),
28 })
29 }
30
31 pub async fn from_urls(read_url: &str, write_url: Option<&str>) -> RuntimeResult<Self> {
32 let db = Database::from_config_with_write(
33 "postgres",
34 read_url,
35 write_url,
36 &systemprompt_database::PoolConfig::default(),
37 )
38 .await?;
39 Ok(Self {
40 database: Arc::new(db),
41 })
42 }
43
44 pub const fn db_pool(&self) -> &DbPool {
45 &self.database
46 }
47
48 pub fn db_pool_arc(&self) -> DbPool {
49 Arc::clone(&self.database)
50 }
51}