ruvipers-config-manager
v0.2.3
Published
RCM (RuVIPeR's Config Manager) is a TypeScript-based configuration manager that enables schema-based configuration handling, automatic loading, validation, and file watching for real-time config updates.
Downloads
422
Readme
Overview
ruvipers-config-manager
is a TypeScript-based configuration manager that helps manage application configuration files. It allows you to define a configuration schema with default values, automatically load and validate config files, and watch for changes in the configuration file, updating the in-memory configuration as needed.
This module supports types such as boolean
, number
, string
, array
, and object
. The configuration can be validated against a schema, and if no valid config is found, default values are used.
Features
- Schema-based configuration: Define a config schema with default values and types.
- Auto-loading and validation: Automatically loads the config from a JSON file and validates it against the schema.
- File watching: Watches the configuration file for changes and reloads it automatically (with debounce).
- Custom error handling: Option to throw errors when invalid or missing config values are found.
- Proxy access: Supports accessing and updating config values via a proxy object.
Installation
npm install
Usage
1. Basic Setup
import RCM from "./RCM";
const configManager = new RCM(
{
host: { type: String, default: "localhost" },
port: { type: Number, default: 0 },
isEnabled: { type: Boolean, default: false },
"payment.currencyCodes": { type: Array, default: ["eur", "usd"] }, // You can use dot notation for nested objects
},
{
configPath: "./config.json", // Path to config file
useDefinition: true, // Use the schema definition
throwOnError: false, // Do not throw errors if config is invalid. If disabled, RCM try to use default values
watchConfigFile: true, // Enable file watching
}
);
2. Accessing Configuration Values
You can access configuration values with the get
method:
const host = configManager.get("host");
console.log(host); // Outputs the value of 'host' from the config
Or via proxy object:
const configProxy = configManager.getProxy();
console.log(configProxy.host); // Outputs the same value
3. Setting Configuration Values
You can set values using the set
method:
configManager.set("host", "127.0.0.1");
or via the proxy:
configProxy.host = "value";
configProxy.save(); // if you want save to filesystem
4. Saving Configuration
Changes to the configuration can be saved to the file using the save()
method:
configManager.save();
The proxy object also supports the save
method:
configProxy.save();
5. Watching Configuration File
When the watchConfigFile
option is enabled, the configuration manager automatically reloads the config file if it detects changes:
const configManager = new RCM(..., {
watchConfigFile: true // Enable watching
});
If the config file changes, the initializeConfig()
method is called, and the new configuration is loaded.
Configuration Options
configPath
: Path to the JSON config file (default:'./config.json'
).useDefinition
: Whether to use the provided schema to validate the config file. (default:true
)throwOnError
: If true, throws an error if any value in the config file doesn't match the schema. (default:false
)watchConfigFile
: If true, watches the config file for changes and reloads the config automatically. (default:true
)
Type Schema Definition
The schema defines the expected structure of the config file. Each key in the schema corresponds to a configuration property, with a defined type and an optional default value.
Supported types:
BooleanConstructor
NumberConstructor
StringConstructor
ArrayConstructor
ObjectConstructor
Example schema definition:
const configSchema: ConfigDefinitionSchema = {
isEnabled: { type: Boolean, default: true },
maxConnections: { type: Number, default: 10 },
databaseUrl: { type: String, default: "localhost" },
allowedIPs: { type: Array, default: ["127.0.0.1"] },
settings: { type: Object, default: { someParameters: "someValue" } },
};
Example
Here’s how you can initialize the RCM and use it in a project:
const configManager = new RCM(
{
"express.host": { type: String, default: "localhost" },
"express.port": { type: Number, default: 3001 },
"express.useHttps": { type: Boolean, default: false },
"auth.login": { type: String, default: "root" },
"auth.password": { type: String, default: "changeme" },
"auth.parameters.defaultPermissions": {
type: Array,
default: ["read", "write", "create"],
},
},
{
throwOnError: false,
watchConfigFile: true,
}
);
TODOs
- Regex value verifier
- watchConfigFile event on parameter change
License
This project is licensed under the MIT License.