Config
SlimIO - Reactive JSON Config loader
Features
- Hot-reloading of configuration
- Reactive with observable key(s)
- Safe with JSON Schema validation
Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @slimio/config
$ yarn add @slimio/config
Usage example
Create a simple json file for your project (As below)
{
"loglevel": 5,
"logsize": 4048,
"login": "administrator"
}
Now create a new Configuration instance and read it
const Config = require("@slimio/config");
const cfg = new Config("./path/to/config.json");
cfg.read().then(() => {
console.log(cfg.get("loglevel"));
}).catch(console.error);
API
constructor(configFilePath: string, options?: Config.ConstructorOptions)
Create a new Configuration instance
const options = { autoReload: true };
const cfg = new Config("./path/to/file.json", options);
Available options are
interface ConstructorOptions {
createOnNoEntry?: boolean;
writeOnSet?: boolean;
autoReload?: boolean;
reloadDelay?: number;
defaultSchema?: object;
}
read(defaultPayload?: T): Promise;
Will trigger and read the local configuration (on disk).
const cfg = new Config("./path/to/file.json");
assert.equal(cfg.configHasBeenRead, false);
await cfg.read();
assert.equal(cfg.configHasBeenRead, true);
Retriggering the method will made an hot-reload of all properties. For a cold reload you will have to close the configuration before.
setupAutoReload(): void;
Setup hot reload (with a file watcher). This method is automatically triggered if the Configuration has been created with the option autoReload
set to true.
get(fieldPath: string): H
Get a value from a key (field path).
For example, image a json file with a foo
field
const cfg = new Config("./path/to/file.json");
await cfg.read();
const fooValue = cfg.get("foo");
set(fieldPath: string, fieldValue: H): void;
Set a given field in the configuration
const cfg = new Config("./config.json", {
createOnNoEntry: true
});
await cfg.read({ foo: "bar" });
cfg.set("foo", "hello world!");
await cfg.writeOnDisk();
observableOf(fieldPath: string): ObservableLike;
Observe a given configuration key with an Observable object!
const { writeFile } = require("fs").promises;
const cfg = new Config("./config.json", {
autoReload: true,
createOnNoEntry: true
});
await cfg.read({ foo: "bar" });
cfg.observableOf("foo").subscribe(console.log);
const newPayload = { foo: "world" };
await writeFile("./config.json", JSON.stringify(newPayload, null, 4));
writeOnDisk(): Promise
Write the configuration on the disk
close(): Promise
Close (and write on disk) the configuration (it will close the watcher and clean all active observers).