@heliomarpm/kvs
Version:
A simple and robust KeyValues Storage's library
98 lines (97 loc) • 3.17 kB
TypeScript
import type { Options, ValueType } from "@/core/types/types";
/**
* This module provides a helper class for managing JSON files in a key-value store.
* It includes methods for loading, saving, and ensuring the existence of JSON files and directories.
* It is designed to work with a customizable directory and file name for storing key-value pairs.
*
* @module JsonFileHelper
* @author Heliomar Marques
* @internal
* @ignore
*/
export declare class JsonFileHelper {
/**
* The options for the JsonFileHelper.
* @type {@link Options}
* @private
*/
options: Options;
/**
* Creates an instance of JsonFileHelper.
*
* @param {@link Options} options - The options for the JsonFileHelper.
*/
constructor(options: Options);
/**
* Returns the path to the keyvalues directory. The path
* may be customized by the developer by using
* `configure()`.
*
* @returns The path to the keyvalues directory.
*/
private getJsonDirPath;
/**
* Returns the path to the keyvalues file. The file name
* may be customized by the developer using `configure()`.
*
* @returns The path to the keyvalues file.
*/
getJsonFilePath(): string;
/**
* Ensures that the keyvalues file exists. If it does not
* exist, then it is created.
*
* @returns A promise which resolves when the keyvalues file exists.
*/
private ensureJsonFile;
/**
* Ensures that the keyvalues file exists. If it does not
* exist, then it is created.
*
* @returns {void}
*/
private ensureJsonFileSync;
/**
* Ensures that the KeyValues directory exists. If it does
* not exist, then it is created.
*
* @returns A promise which resolves when the keyvalues dir exists.
*/
private ensureJsonDir;
/**
* Ensures that the KeyValues directory exists synchronously. If it does not exist,
* then it is created.
*
* @returns {void}
*/
private ensureJsonDirSync;
/**
* Asynchronously loads key-value pairs from a JSON file. First ensures that the file exists,
* then reads the file and parses its contents into a JavaScript object.
*
* @template T - The type of the key-value pairs.
* @return {Promise<T>} A promise that resolves with the key-value pairs.
*/
loadKeyValues<T extends ValueType>(): Promise<T>;
/**
* Loads the key-value pairs synchronously from the JSON file.
*
* @template T - The type of the key-value pairs.
* @returns {T} - The loaded key-value pairs.
*/
loadKeyValuesSync<T extends ValueType>(): T;
/**
* Saves the keyvalues to the disk.
*
* @param {T} obj - The keyvalues object to save.
* @return {Promise<void>} A promise that resolves when the keyvalues have been saved.
*/
saveKeyValues<T>(obj: T): Promise<void>;
/**
* Saves the keyvalues to the disk synchronously.
*
* @param {T} obj - The keyvalues object to save.
* @return {void} This function does not return anything.
*/
saveKeyValuesSync<T>(obj: T): void;
}