prefer_db

Crates.ioprefer_db
lib.rsprefer_db
version0.3.4
created_at2026-01-24 08:07:26.502661+00
updated_at2026-01-24 08:07:26.502661+00
descriptionDatabase source for prefer configuration library
homepage
repositoryhttps://github.com/LimpidTech/prefer_db
max_upload_size
id2066382
size38,646
Bailey Stoner (monokrome)

documentation

README

prefer_db

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.

Usage

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(())
}

Supported Formats

Configuration data can be stored in any of these formats:

  • JSON
  • TOML
  • YAML

The format is determined by the format field in ConfigEntry.

License

MIT

Commit count: 0

cargo fmt