Configuration System

Khadem provides a robust configuration system based on JSON files, environment-specific overrides, dot-notation access, TTL caching, and runtime modification.

1. Configuration Structure

Config files live in the config/ folder:

bash
config/
├── app.json
├── database.json
└── development/
    ├── app.json
    └── database.json

2. Example Config

config/app.json

json
{
  "name": "Khadem",
  "env": "production",
  "port": 8080
}

config/development/app.json (override)

json
{
  "env": "development",
  "port": 3000
}

3. Accessing Config

Use dot notation to access values:

dart
final config = Khadem.config;

print(config.get<String>('app.name'));   // "Khadem"
print(config.get<int>('app.port'));      // 3000
print(config.get<String>('non.key', 'default'));

4. Runtime Overrides

You can override config at runtime:

dart
config.set('app.name', 'CustomApp');
print(config.get<String>('app.name')); // "CustomApp"

5. Reloading Configs

Manually reload all configuration files:

dart
config.reload(); // Reload all configs

6. Registry-Based Config

Load configuration maps directly from code:

dart
Khadem.loadConfigs({
  'database': {
    'host': 'localhost',
    'port': 5432
  }
});

7. Access Sections

Fetch entire config sections as maps:

dart
final db = config.section('database');
print(db?['host']);

8. Summary

  • get('key'): Retrieve a value
  • set('key', value): Set a value
  • has('key'): Check existence
  • reload(): Reload all configs
  • section('name'): Get section
  • all(): Get all config data

Note: YAML parsing is not supported (yet).

On this page