Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

key-file-storage

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

key-file-storage

Simple key-value storage directly on file system, maps each key to a separate file.

  • 2.1.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

key-file-storage

Simple key-value storage directly on file system, maps each key to a separate file.

No database and database overhead anymore, just plain file-system and simple files containing JSON data! It's great for applications with small and medium data sizes.

  • 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
var kfs = require("key-file-storage")('my/storage/path');

// Write something to file 'my/storage/path/myfile'
kfs.myfile = { mydata: 123 };

// Read contents of file 'my/storage/path/myfile'
var mydata = kfs.myfile.mydata;

// Delete file 'my/storage/path/myfile'
delete kfs.myfile;

Just give it a try, you'll like it!

Installation

Installing package on Node.js (Node.js 6.0.0 or higher is required) :

$ npm install key-file-storage

Initialization

Initializing a key-file storage :

var keyFileStorage = require("key-file-storage");

var kfs = keyFileStorage('/storage/directory/path', caching);

The value of caching can be

  1. true (Default value, if not specified) :

Unlimited cache, anything will be cached on memory, good for small data volumes.

  1. false :

No cache, read the files from disk every time, good when other applications can modify the files' contents arbitrarily.

  1. 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       // Write file
kfs['key']               // Read file
delete kfs['key']        // Delete file
delete kfs['*']          // Delete all storage files
'key' in kfs             // Check for file existence
                         //=> true or false
  • 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)        // Write file
kfs('key')               // Read file
new kfs('key')           // Delete file
new kfs('*')  /* or */
new kfs()     /* or */
new kfs                  // Delete all storage files
('key' in kfs(), kfs())  // Check for file existence
                         // Resolves to true or false
  • 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)   // Write file
kfs('key', cb)          // Read file
new kfs('key', cb)      // Delete file
new kfs('*', cb)   /* or */
new kfs(cb)             // Delete all storage files
'key' in kfs(cb)        // Check for file existence
                        // without promise output
                   /* or */
('key' in kfs(), kfs(cb))
                        // Check for file existence
                        // with promise output
  • 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
var keys = kfs['col/path/']
// keys = ['col/path/key1', 'col/path/sub/key2', ... ]
Asynchronous API with Promises
kfs('col/path/').then(function(keys) {
    // keys = ['col/path/key1', 'col/path/sub/key2', ... ]
});
Asynchronous API with Callbacks
kfs('col/path/', function(error, keys) {
    // keys = ['col/path/key1', 'col/path/sub/key2', ... ]
});

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 : 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 3 : 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.

Example

var keyFileStorage = require("key-file-storage");

// Locate 'db' folder in current directory as the storage path,
// Require 100 latest accessed key-values to be cached:
var kfs = keyFileStorage('./db', 100);

// Create file './db/users/hessam' containing this user data synchronously: 
kfs['users/hessam'] = {
    name: "Hessam",
    skills: {
        java: 10,
        csharp: 15
    }
};

// Read file './db/users/hessam/skills' as a JSON object asynchronously:
kfs('users/hessam').then(function(hessamData) {
    console.log("Hessam's java skill is ",
        hessamData.skills.java);
});

// Check whether file './db/users/mahdiar' exists or not asynchronously:
'users/mahdiar' in kfs(function(error, exists) {
    if(exists) {
        console.log("We have Mahdiar's data!");
    }
});

// List the file names of all users in './db/users/' synchronously:
var allUsers = kfs['users/'];
//=> ['users/hessam', 'users/mahdiar', ... ]

Contribute

The code is simple! It would be appreciated if you had any suggestions or contribution on it or detected any bug or issue.

  • See the code on GitHub.com (key-file-storage)
  • Contact me by my gmail address (Hessam A Shokravi)

Keywords

FAQs

Package last updated on 22 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc