EasyFlashStorage
provides a filesystem-based storage solution for large or persistent data, suitable for cases where data size exceeds the practical limits of in-memory storage.
npm i @silver-zepp/easy-storage
import { EasyFlashStorage } from "@silver-zepp/easy-storage";
const flash = new EasyFlashStorage();
flash.setKey("config", { theme: "dark", notifications: true, ... });
console.log(flash.getKey("config"));
setKey(key, value)
: Stores or updates a value for a key, saving it to a file.getKey(key)
: Retrieves the value of a key from a file.removeKey(key)
: Deletes a file associated with a key.hasKey(key)
: Checks for the existence of a file for a key.dataSize(key, unit)
: Calculates the size of a file for a key.size(unit)
: Calculates the total size of all storage files.getAllKeys(stringify)
: Lists all keys in storage.getAllValues(stringify)
: Lists all values in storage.getStorageSnapshot(stringify)
: Returns all storage contents as a string or object.deleteAll()
: Deletes all keys and values, removing associated files.printAllKeys()
: Displays all keys and their values.
Initializes the storage with a specified directory.
directory
{string} - The directory path to use for storage. Defaults to "easy_flash_storage".
const flash = new EasyFlashStorage();
// or
const custom_dir_storage = new EasyFlashStorage('my_custom_dir');
Stores or updates a value associated with a key. The value is serialized to JSON and written to a file named after the key.
key
{string} - The key under which to store the value.value
{any} - The value to store, must be serializable to JSON.
flash.setKey('config', { theme: 'dark' });
Retrieves the value associated with the specified key from the storage.
key
{string} - The key whose value to retrieve.
console.log(flash.getKey('config'));
any
- The value associated with the key, or undefined
if not found.
Checks if the specified key exists in the storage.
key
{string} - The key to check.
console.log(flash.hasKey('config')); // true or false
boolean
- True if the key exists, false otherwise.
Removes the specified key and its associated file from the storage.
key
{string} - The key to remove.
flash.removeKey('config');
Retrieves all keys stored in the storage, optionally stringified.
stringify
{boolean} - If true, returns the keys as a JSON string; otherwise, as an array.
console.log(flash.getAllKeys());
console.log(flash.getAllKeys(true));
string | array
- The keys as a JSON string if stringify
is true, or as an array if false.
Removes all keys and their associated values from the storage.
flash.deleteAll();
Prints all keys and their associated values to the console.
flash.printAllKeys();
Returns the size of the data associated with a specific key.
key
{string} - The key whose data size to retrieve.unit
{string} - The unit of measurement ('B', 'KB', 'MB'). Defaults to 'B'.
console.log(flash.dataSize('config'));
console.log(flash.dataSize('config', 'KB'));
Calculates the total size of all stored data.
unit
{string} - The unit of measurement ('B', 'KB', 'MB'). Defaults to 'B'.
console.log(flash.size());
console.log(flash.size('KB'));