Skip to main content

photon_backend_sqlite/
config.rs

1//! `SQLite` adapter configuration.
2//!
3//! **Configuration lives here.** Open a path explicitly or resolve from env:
4//!
5//! | API / env | Default | Purpose |
6//! |-----------|---------|---------|
7//! | [`SqliteStoragePort::open`](crate::SqliteStoragePort::open) | — | Open (or create) a database file. |
8//! | [`SqliteStoragePort::from_env`](crate::SqliteStoragePort::from_env) / [`PATH_ENV`] | temp file | Resolve path via [`sqlite_path_from_env`]. |
9//!
10//! # Example
11//!
12//! ```rust,no_run
13//! use photon_backend_sqlite::SqliteStoragePort;
14//!
15//! # async fn wire() -> photon_backend::Result<()> {
16//! let _port = SqliteStoragePort::open("/var/lib/photon/events.db").await?;
17//! // Photon::builder().storage_port(Arc::new(port)).auto_registry().build()?;
18//! # Ok(())
19//! # }
20//! ```
21
22/// Environment variable for the `SQLite` database file path.
23pub const PATH_ENV: &str = "PHOTON_SQLITE_PATH";
24
25/// Resolve database path from [`PATH_ENV`], or a unique file under the system temp dir.
26#[must_use]
27pub fn sqlite_path_from_env() -> String {
28    std::env::var(PATH_ENV).unwrap_or_else(|_| {
29        let path = std::env::temp_dir().join(format!("photon-{}.db", uuid::Uuid::new_v4()));
30        path.to_string_lossy().into_owned()
31    })
32}