node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org ยป

fs_db

fs_db.js

This is a simple key-value database, which secretly uses the filesystem for all storage.

To ensure that the user may use key-names which contain characters not permitted in filenames all keys are hashed, via SHA1, prior to being used on the filesystem. This behaviour can be disabled if you need to enumerate the keys which have previously been used.

Installation

You should be able to install this module by executing:

$ npm install fs_db

If that does not suit then you may clone the main repository, and load the module via the following:

require( "./fs_db" );

Examples

var DB = require( "fs_db" );

// Create a new store, writing data beneath /tmp.
var store = new DB( "/tmp" );

// This will write the value "kemp" to the key "steve".
store.set( "steve", "kemp" );

// This will return the value set previously.
store.get( "steve", function( error, value ) {
    console.log("Value is " + value );
});

Notes

As previously mentioned the files used on-disk will default to being based upon the SHA1 hash of the key-name.

If you leave this default behavior in-place then the original key-name is lost - so you cannot enumerate all the keys that have previously been used. If you trust all the key-names that will be used you can disable the default and use keys literally - providing that the key-names contain no characters which are disallowed in filenames.

The choice really becomes between :

  • Allowing keys such as "/../../etc/passwd", or "#&.:/\moi", via the use of hash of some kind.
  • Allowing keys to be bare/naked and enumerable, but accepting that not all key-names are valid.

To change the default option simply pass the no_hash parameter to the constructor:

var DB = require( "fs_db" );

//
// Default behavior - keys are hashed, so this writes the value "kemp" to
// the file /tmp/9ce5770b3bb4b2a1d59be2d97e34379cd192299f
//
var store = new DB( "/tmp" );
store.set( "steve", "kemp" );

//
// Disable the use of hashing, so this example writes "kemp" to the
// file /tmp/steve.
//
var store = new DB( "/tmp", { "no_hash": true } )
store.set( "steve", "kemp" )

This code was abstracted from the blogspam.js repository.

License

Please consider this licensed under the two-clause BSD license, included in the file LICENSE.

Steve