prefer_db 0.3.4

Database source for prefer configuration library
Documentation

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