key-file-storage
Simple key-value storage (a persistent data structure) directly on file system, maps each key to a separate file.
A very nice replacement for any of these node modules: node-persist, configstore, flat-cache, conf, simple-store and more...
- Simple key-value storage model
- Very easy to learn and use
- Both Synchronous and Asynchronous APIs
- One JSON containing file per each key
- Built-in configurable cache
- Both Promise and Callback support
const kfs = require("key-file-storage")('my/storage/path');
kfs.myfile = { x: 123 };
const x = kfs.myfile.x;
delete kfs.myfile;
Just give it a try, you'll like it!
Installation
Installing package on Node.js:
$ npm install key-file-storage
Initialization
Initializing a key-file storage:
const keyFileStorage = require("key-file-storage");
const kfs = keyFileStorage('/storage/directory/path', caching);
The value of caching
can be
-
true
(By default, if not specified) : Unlimited cache, anything will be cached on memory, good for small data volumes.
-
false
: No cache, read the files from disk every time, good when other applications can modify the files' contents arbitrarily.
-
n
(An integer number) : Limited cache, only the n
latest referred key-values will be cached, good for large data volumes where only a fraction of data is being used frequently .
Usage
Synchronous API
As simple as native javascript objects:
kfs['key'] = value
kfs['key']
delete kfs['key']
delete kfs['*']
'key' in kfs
-
You can use kfs.keyName
instead of kfs['keyName']
anywhere if the key name allows.
-
undefined
is not supported as a savable value, but null
is. Saving a key with value undefined
is equivalent to remove it. So, you can use kfs['key'] = undefined
or even kfs['*'] = undefined
to delete files.
-
Synchronous API will throw an exception if any errors happens, so you shall handle it your way.
Asynchronous API with Promises
Every one of the following calls returns a promise:
kfs('key', value)
kfs('key')
new kfs('key')
new kfs('*')
new kfs()
new kfs
('key' in kfs(), kfs())
- Once again,
undefined
is not supported as a savable value, but null
is. Saving a key with value undefined
is equivalent to remove it. So, you can use kfs('key', undefined)
or even kfs('*', undefined)
to delete files.
Asynchronous API with Callbacks
The same as asynchronous with promises, but with callback function as the last input parameter of kfs()
:
kfs('key', value, cb)
kfs('key', cb)
new kfs('key', cb)
new kfs('*', cb)
new kfs(cb)
'key' in kfs(cb)
('key' in kfs(), kfs(cb))
-
These calls still return a promise on their output (except for 'key' in kfs(callback)
form of existence check).
-
The first input parameter of all callback functions is err
, so you shall handle it within the callback. Reading and Existence checking callbacks provide the return values as their second input parameter.
Folders as Collections
Every folder in the storage can be treated as a collection of key-values.
You can query the list of all containing keys (filenames) within a collection (folder) like this (Note that a collection path must end with a forward slash '/'
):
Synchronous API
try {
const keys = kfs['col/path/']
} catch(error) {
}
Asynchronous API with Promises
kfs('col/path/').then(keys => {
}, error => {
});
Asynchronous API with Callbacks
kfs('col/path/', (error, keys) => {
if (error) {
}
});
Notes
-
NOTE 1 : Each key will map to a separate file (using the key itself as its relative path). Therefore, keys may be relative paths, e.g: 'data.json'
, '/my/key/01'
or 'any/other/relative/path/to/a/file'
. The only exception is strings including '..'
(double dot) which will not be accepted for security reasons.
-
NOTE 2 : You may have hidden key files by simply add a '.'
before the filename in the key path.
-
NOTE 3 : If a key's relative path ends with a forward slash '/'
, it will be considered to be a collection (folder) name. So, 'data/set/'
is a collection and 'data/set/key'
is a key in that collection.
-
NOTE 4 : This module has a built-in implemented cache, so, when activated, accessing a certain key more than once won't require file-system level operations again for that file.
-
NOTE 5 : When activated, caching will include queries on collections too.
Example
const keyFileStorage = require("key-file-storage");
const kfs = keyFileStorage('./db', 100);
kfs['users/hessam'] = {
name: "Hessam",
skills: {
java: 10,
csharp: 15
}
};
kfs('users/hessam').then(hessam => {
console.log(`Hessam's java skill is ${hessam.skills.java}.`);
});
'users/mahdiar' in kfs((error, exists) => {
if (exists) {
console.log("User Mahdiar exists!");
}
});
const allUsers = kfs['users/'];
Contribute
It would be appreciated if you had any suggestions or contribution on this repository or submitted any issue.