storage-to-json
v1.0.48
Published
Save data to json, persistent!
Downloads
9
Maintainers
Readme
Storage-to-json
Installation
//Install it at your project directory!
npm i storage-to-json
Usage
// Example on how to utilize.
const Storage = require('storage-to-json');
const defaultSettings = {
This_Needs_To_Exist: 'OK',
ThisAlso: 'OK',
};
const JsonFile = 'testeroni';
const Store = new Storage(JsonFile);
//=> Applies only for non memory storage.
Store.clear();
//=> Returns MyName
Store.set('MyName', { name: 'Gerkiz' });
Store.logger('Name is: "' + Store.get('MyName').name + '"');
//=> Replaces MyName with FirstName
Store.replace('MyName', 'FirstName');
Store.logger('Name is: "' + Store.get('FirstName').name + '"');
//=> MyName is undefined.
Store.logger('Name is: "' + Store.get('MyName').name + '"');
//=> Default value set for "default"
Store.ensure('default', defaultSettings);
Store.each((key, val) => {
console.log(JSON.stringify(key));
console.log(JSON.stringify(val));
});
//=> We can also use the storage as a proxy, like below:
const data = Store.proxy();
data.awesome_storage = true;
Store.logger('Does awesome_storage exist? ' + Store.get('awesome_storage'));
//=> Backups the persistent storage.
Store.backup(JsonFile);
//=> Removes a key
Store.remove('FirstName');
Store.remove('default');
Store.remove('awesome_storage');
API
//=> Require at first.
const Storage = require('storage-to-json');
//=> Use a storage that already exists or creates it if it does not exist.
//=> You can now utilize multiple instances of Storage.
//=>
//=> NOTE!
//=> If no name is provided for the datastore then
//=> the storage is not persistent but is instead
//=> kept in memory.
const Store = new Storage('datastore');
//=> Set a key, value -- .set can also be used to replace an old value of a key.
Store.set('key', { name: 'value' });
//=> Get key.
Store.get('key').name;
//=> value
//=> Validates that given key exists.
Store.validate('key');
//=> Same as Store.validate
Store.has('key');
//=> Fetches the whole storage.
Store.get_storage();
//=> Remove key.
Store.remove('key');
//=> Remove value only.
Store.remove('key', 'value');
//=> Same as Store.remove
Store.delete('key', 'value');
//=> Backup a file for future use. Appends %date%.
Store.backup('datastore');
//=> Clear all keys.
//=> Only applies if we're using non memory storage.
Store.clear();
//=> Replace a key with another key.
Store.replace('old', 'new');
//=> enableLogger, boolean (default true).
Store.enableLogger(boolean);
//=> Ensures that the key default exists, if not then obj is copied to key default.
Store.ensure('defualt', obj);
//=> If enableLogger, then stdout to console.
Store.logger(str);
//=> Loops over all stored values.
Store.each(function (value, key) {
console.log(key, '==', value);
});
//=> We can also use the storage as a proxy to modify nested datasets.
Store.proxy();
// Hint, most of the API is awaitable
await Store.setAsync(...)
await Store.eachAsync(...)
await Store.ensureAsync(async (...) => {})