Configuration
Tymber provides a centralized and reactive configuration management system. It allows components to declare their own configuration schema and react to configuration changes at runtime.
Usage
Components can subscribe to configuration by declaring a schema and a handler. This is typically done in the component's constructor.
Tymber uses JSON Schema for configuration validation.
import { Component, INJECT, ConfigService } from "@tymber/core";
interface Config {
MY_SERVICE_API_URL: string;
MY_SERVICE_API_KEY: string;
}
export class MyService extends Component {
static [INJECT] = [ConfigService];
private config: Config = {
MY_SERVICE_API_URL: "",
MY_SERVICE_API_KEY: "",
};
constructor(configService: ConfigService) {
super();
configService.subscribe<Config>(
{
MY_SERVICE_API_URL: {
type: "string",
format: "uri",
},
MY_SERVICE_API_KEY: {
type: "string",
},
},
(config) => {
this.config = config;
},
);
}
async doSomething() {
// use this.config
}
}
When the application starts, ConfigService will:
- Collect all subscribed schemas.
- Fetch current values from the active configuration source.
- Validate values against the combined schema.
- Notify all subscribers with their specific configuration.
Configuration sources
Environment variables
By default, Tymber reads configuration from environment variables.
You can use it with the dotenv package (or Node.js built-in .env support) to load environment variables from a .env file.
Database
For more advanced needs, the @tymber/config module provides a database-backed implementation with versioning and encryption.