@abibv/kval-dstore
v1.0.1
Published
A simple file based key-value data store that supports basic CRU
Downloads
5
Maintainers
Readme
kval-dstore
A simple file based key-value data store that supports basic CRUD operations in nodejs.
It creates a json file that can be used as a key-value database for learning purpose or even for micro-apps.
It provides five different API for accessing the key-value data store.
Initializing data store
new dstore(filename [,sizeLimit, initData]);
- filename (String) - The file name with its path for the json file.
- [optional] sizeLimit (String) - Specifies the sizeLimit in MegaByte (default is 1024 MB)
- [optional] initData (JSON object) - Initial data to be stored at the time of file creation (default is empty json object {})
const dstore = require('@abibv/kval-dstore');
const store = new dstore('./dstore.json');
(or)
const store = new dstore(__dirname + '/dstore.json');
Write the data
store.write(object [,path]);
- object (JSON object) - Data to be stored.
- [optional] path (string) - Key path where the data should be placed (should be referred from the root) (default is the root of the json data '/')
write
is an Asynchronous method which returns nothing.
var object = {
name: 'myname',
no: 123456789,
timeSpent: {
tech: '50%',
music: '30%',
sleep: '20%',
}
};
//Write the data at the root
store.write(object)
.then(() => { console.log('data written'); })
.catch((error) => { console.log(error); });
//Write the data at the specified
store.write(object, '/department/year/students/')
.then(() => { console.log('data written'); })
.catch((error) => { console.log(error); });
Read the data
store.read([path]);
- [optional] path (String) - JSON key path from where the data should be read. (default is root '/')
If the path is not specified, then the whole data was read.
read
is an Asynchronous method which returns the data read.
store.read('/department/year/students/')
.then((data) => { console.log(data); })
.catch((error) => { console.log(error); });
Update the data
store.update(data, key [,path]);
- data (any) - data to be updated.
- key (String) - JSON key whose value should be replaced (must be a Single key string).
- [optional] path (String) - path of the key in the JSON object referred from the root '/'.
update
is an Asynchronous method which returns nothing.
store.update({ misc: '10%' }, '/timeSpent')
.then(() => { console.log('updated'); })
.catch((err) => { console.log(err); });
Delete a Data
store.delete([path])
- [optional] path (String path) - JSON key path from where the key-value pair should be deleted (default is root '/')
If no path is specified, then the whole data store with the file will be deleted
delete
is an Asynchronous method which returns nothing.
store.delete('/timeSpent')
.then(() => { console.log('timeSpent object deleted'); })
.catch((error) => { console.log(error); });
Calculate data store size
store.calcFileSize()
calcFileSize
is an Synchronous method which returns the size of the data store file in MegaBytes.
console.log(store.calcFileSize());