I initially created this repo to allow CLI apps to access simple key-value stores. It has now grown beyond that. It now provides access to data within a JSON file, performing basic get/put operations, operations on numbers such as incr and decr, operations on arrays (e.g. push, pop, shift, unshift), as well as providing search functionality.
There are several different modules like this on NPM; this is my effort and helps on my Javascript/NodeJS learning journey. One big differentiator is that it also provides array, increment/decrement and find functions over and above the usual get/set methods normally provided.
Updates to the store file happen when any data is added or taken from it, this does mean that it is slower than performing a periodic write, however it does mean that data will not be lost by a crash or application termination.
Due to the use of a local file to hold the data, this library is not suitable for use with Javascript browser applications.
This repo provides both a library for use by nodejs scripts and a CLI (kv) to either be used standalone or within bash scripts.
If you have cloned this repo, then npm install
should do the trick, if you want to use the kv
script for CLI, then you need to run npm link
You may run npm run test
to make sure everything is working as expected.
I have not yet added this to NPM
// until I release it to npm, you will have to reference it with a full path
const KVStore = require('../kvstore/lib/kvstore');
const kv = new KVStore({ filename: '/tmp/kvstore.json', namespace: 'sample' });
kv.put( 'a', 100)
kv.put( 'b', { c: { d: 'item' } })
kv.put( 'e.f.g', [ 'one', "two", "three"])
let found = kv.list();
found.forEach((ele) => {
Object.keys(ele).sort().forEach((key) => {
showKeyValue(key, ele[key]);
});
});
Full documentation for the library can be seen in API documentation
Basic operations
# put
$ kv --ns 'namespace' --file /tmp/sample.js put key1 value
# get
$ VALUE=$(kv --ns 'namespace' /tmp/sample.js get key1)
#delete/del/rm
$ kv --ns 'namespace' /tmp/sample.js delete key1
For more operations see CLI documentation or use the inbuilt help kv -h
If you want to make use of this script with bun then replace node
with bun
in bin/kv
, you will of course need bun
to be installed on your system.