key-file-storage
Simple key-value Node.js storage directly on file system, maps each key to JSON contents of a file.
No database and database overhead anymore, just plain file-system and simple files containing JSON data !
It's great for simple applications with small data.
Installation
- Installing package on Node.js :
$ npm install --save key-file-storage
Initialization
- Initializing key-file storage :
var keyFileStorage = require("key-file-storage");
var kfs = keyFileStorage('/path/to/storage/directory');
var kfs = keyFileStorage();
Usage
- Setting a new value to a key : (Setting to
undefined
is equivalent to remove the key)
var value = ...
kfs.set('key', value, function(err) {
if (err) { }
});
kfs.set('key', value).then(function() {
}, function(err) {
});
kfs.setSync('key', value);
- Getting value of a key : (Value of a not existing key will be
null
)
kfs.get('key', function(err, value) {
if (err) { }
else { }
});
kfs.get('key').then(function(value) {
}, function(err) {
});
var value = kfs.getSync('key');
- Removing a key-file pair :
kfs.remove('key', function(err) {
if (err) { }
});
kfs.remove('key').then(function() {
}, function(err) {
});
kfs.removeSync('key');
- Clearing anything in the database folder :
kfs.clear(function(err) {
if (err) { }
});
kfs.clear().then(function() {
}, function(err) {
});
kfs.clearSync();
Notes
-
NOTE 1 : undefined
is not supported as a savable value, but null
is. Saving a key with value undefined
is equivalent to remove it.
-
NOTE 2 : Each key will map to a separate file (using the key itself as its relative path) so there is no need to load all the database file for any key access. Also, keys can be relative paths, e.g: data.json
, /my/key/01
or any/other/relative/path/to/a/file
.
-
NOTE 3 : There is a built-in implemented cache, so accessing a certain key once again won't require file-system level operations.
Contribute
The code is very simple and straightforward. It would be nice if you had any suggestions or contribution on it or detected any bug or issue.