Expand description
Database source for prefer configuration library.
This crate provides a DbSource that implements prefer::Source,
allowing configuration to be loaded from a database and layered
with other prefer sources.
§Example
use prefer::{Config, ConfigBuilder};
use prefer_db::{DbSource, ConfigLoader, ConfigEntry};
use async_trait::async_trait;
struct MyDbLoader {
// your database connection
}
#[async_trait]
impl ConfigLoader for MyDbLoader {
async fn load_config(&self) -> Option<ConfigEntry> {
// Load from your database
Some(ConfigEntry {
format: "json".to_string(),
data: r#"{"key": "value"}"#.to_string(),
})
}
fn name(&self) -> &str {
"my_database"
}
}
#[tokio::main]
async fn main() -> prefer::Result<()> {
let config = Config::builder()
.add_source(DbSource::new(MyDbLoader { /* ... */ }))
.add_file("config.toml") // File overrides DB
.build()
.await?;
Ok(())
}Structs§
- Config
Entry - A configuration entry loaded from the database.
- DbSource
- A prefer Source that loads configuration from a database.
Traits§
- Config
Loader - Trait for loading configuration from a database.