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//! Production hosts must set [`PATH_ENV`] or call
11//! [`SqliteStoragePort::open`](crate::SqliteStoragePort::open) with a durable, explicit path.
12//! The temporary fallback is intended only for local development and tests.
13//!
14//! # Example
15//!
16//! ```rust,no_run
17//! use photon_backend_sqlite::SqliteStoragePort;
18//!
19//! # async fn wire() -> photon_backend::Result<()> {
20//! let _port = SqliteStoragePort::open("/var/lib/photon/events.db").await?;
21//! // Photon::builder().storage_port(Arc::new(port)).auto_registry().build()?;
22//! # Ok(())
23//! # }
24//! ```
25
26/// Environment variable for the `SQLite` database file path.
27pub const PATH_ENV: &str = "PHOTON_SQLITE_PATH";
28
29/// Resolve database path from [`PATH_ENV`], or a unique file under the system temp dir.
30///
31/// The temporary fallback is not durable across hosts and must not be used for production data.
32#[must_use]
33pub fn sqlite_path_from_env() -> String {
34    std::env::var(PATH_ENV).unwrap_or_else(|_| {
35        let path = std::env::temp_dir().join(format!("photon-{}.db", uuid::Uuid::new_v4()));
36        path.to_string_lossy().into_owned()
37    })
38}