A configuration loader for the Sailboat Computer system that provides:
- Loading configuration from JSON files
- Validating configuration against JSON schemas
- Watching for configuration changes
- Environment variable overrides
- Default values
- Configuration versioning and rollback support
npm install @sailboat-computer/config-loader
import { loadConfig } from '@sailboat-computer/config-loader';
// Load configuration with default values
const config = await loadConfig<MyConfig>(
'my-service/config.json',
defaultConfig
);
// Use configuration
console.log(config.someValue);
import { loadConfig } from '@sailboat-computer/config-loader';
// Load configuration with validation
const config = await loadConfig<MyConfig>(
'my-service/config.json',
defaultConfig,
{ validate: true }
);
// The loader will automatically look for 'my-service/config.schema.json'
// and validate the configuration against it
import { createConfigLoader } from '@sailboat-computer/config-loader';
// Create a loader with watching enabled
const loader = createConfigLoader({ watch: true });
// Load configuration
const config = await loader.load<MyConfig>('my-service/config.json');
// Watch for changes
loader.watch('my-service/config.json', (event) => {
console.log('Configuration changed:', event.config);
});
The configuration loader supports overriding configuration values using environment variables. The environment variable names are constructed as follows:
CONFIG_<PATH>_<KEY>
Where:
-
<PATH>
is the configuration file path with slashes and dots replaced by underscores and converted to uppercase -
<KEY>
is the configuration key with dots replaced by underscores and converted to uppercase
For example, to override the database.host
value in the my-service/config.json
file, you would set the environment variable:
CONFIG_MY_SERVICE_CONFIG_DATABASE_HOST=localhost
import { saveConfig } from '@sailboat-computer/config-loader';
// Save configuration
await saveConfig<MyConfig>(
'my-service/config.json',
config,
'Updated database settings', // Optional description
'admin' // Optional user
);
import {
saveConfig,
getConfigVersions,
getConfigVersion,
rollbackConfig
} from '@sailboat-computer/config-loader';
// Save configuration with description
await saveConfig<MyConfig>(
'my-service/config.json',
config,
'Updated database settings'
);
// Get all versions
const versions = await getConfigVersions('my-service/config.json');
console.log(`Available versions: ${versions.length}`);
// Get a specific version
const version = await getConfigVersion<MyConfig>('my-service/config.json', 1);
console.log(`Version 1 created at: ${version?.metadata.timestamp}`);
// Rollback to a previous version
const rolledBackConfig = await rollbackConfig<MyConfig>(
'my-service/config.json',
1,
'Rollback to initial configuration'
);
Loads configuration from a file.
-
path
: Path to the configuration file (relative to baseDir) -
defaultConfig
: Default configuration to use if the file doesn't exist or to merge with -
options
: Configuration loader options
saveConfig<T>(path: string, config: T, description?: string, user?: string, options?: ConfigLoaderOptions): Promise<void>
Saves configuration to a file.
-
path
: Path to the configuration file (relative to baseDir) -
config
: Configuration to save -
description
: Optional description of the change -
user
: Optional user who made the change -
options
: Configuration loader options
Gets all available versions for a configuration file.
-
path
: Path to the configuration file (relative to baseDir) -
options
: Configuration loader options - Returns: List of version metadata
getConfigVersion<T>(path: string, version: number, options?: ConfigLoaderOptions): Promise<ConfigVersion<T> | null>
Gets a specific version of a configuration file.
-
path
: Path to the configuration file (relative to baseDir) -
version
: Version number -
options
: Configuration loader options - Returns: Configuration version or null if not found
rollbackConfig<T>(path: string, version: number, description?: string, user?: string, options?: ConfigLoaderOptions): Promise<T>
Rolls back to a specific version of a configuration file.
-
path
: Path to the configuration file (relative to baseDir) -
version
: Version number -
description
: Optional description of the rollback -
user
: Optional user who performed the rollback -
options
: Configuration loader options - Returns: The rolled back configuration
Creates a configuration loader.
-
options
: Configuration loader options
Options for the configuration loader:
-
baseDir
: Base directory for configuration files (default: 'config') -
envPrefix
: Environment variable prefix for configuration overrides (default: 'CONFIG_') -
validate
: Whether to validate configuration against schema (default: true) -
watch
: Whether to watch for configuration changes (default: false) -
required
: Whether to throw an error if configuration file is not found (default: false) -
mergeDefaults
: Whether to merge with default values (default: true) -
logger
: Custom logger
MIT