@curium.rocks/json-chronicler
v0.95.0
Published
Persists objects in JSON format to rolling archive files
Downloads
155
Readme
Json-Chronicler
What does it do?
This library facilitates saving records to local files in JSON format. It will append a record to a json file and honor chronological rotation settings.
How to install
npm install --save @curium.rocks/json-chronicler
How do I use it?
Setup your options
import {JsonChroniclerOptions} from "@curium.rocks/json-chronicler";
const logOptions: JsonChroniclerOptions = {
name: "yourLoggerName", // used for prefixing the log files
logDirectory: "./logs", // path to your log directory
rotationSettings: {
days: 7,
hours: 1,
minutes: 5,
milliseconds: 750
} // sums all the properties and will rotate the logs on that interval
}
Create and save a record
import {JsonChronicler} from "@curium.rocks/json-chronicler";
import {IChronicler} from "@curium.rocks/data-emitter-base";
const chronciler: IChronicler = new JsonChronicler(logOptions);
await chronciler.saveRecord({
toJSON: () => {
return {
property1: 0.123,
property2: "blue"
}
}
})
Make your models compatible
import {IJsonSerializable} from "@curium.rocks/data-emitter-base";
/**
* Example implementation of a model that could be directly passed to
* a chronicler
*/
export class Model implements IJsonSerializable {
private hidden = "will not be saved";
private property1 = 0.123;
private property2 = "blue";
/**
* Provide a map with the desired names and values
* @return {Record<string, unknown>} map that will be serialized
*/
toJSON(): Record<string, unknown> {
return {
desiredSavedPropName1: this.property1,
desiredSavedPropName2: this.property2
};
}
}