| Crates.io | prefer_db |
| lib.rs | prefer_db |
| version | 0.3.4 |
| created_at | 2026-01-24 08:07:26.502661+00 |
| updated_at | 2026-01-24 08:07:26.502661+00 |
| description | Database source for prefer configuration library |
| homepage | |
| repository | https://github.com/LimpidTech/prefer_db |
| max_upload_size | |
| id | 2066382 |
| size | 38,646 |
Database source for the 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.
Add to your Cargo.toml:
[dependencies]
prefer_db = "0.3"
prefer = "0.3"
async-trait = "0.1"
Implement the ConfigLoader trait for your database:
use prefer_db::{ConfigLoader, ConfigEntry, DbSource};
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"
}
}
Use with prefer's ConfigBuilder:
use prefer::Config;
use prefer_db::DbSource;
#[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(())
}
Configuration data can be stored in any of these formats:
The format is determined by the format field in ConfigEntry.
MIT