Fork me on GitHub Keys

Keys

Unified interface for node key/value stores.

keys

lib/keys.js

Generate auto-loading getters.

Example

keys.Redis

(function(name){
    exports.__defineGetter__(name, function(){
        return require('./stores/' + name.toLowerCase());
    });
    return arguments.callee;
})('Memory')('Redis')('nStore')('Riak');

memory

lib/stores/memory.js

Initialize memory store.

Options

  • data Object containing pre-defined store data

  • param: Object options

  • api: public

var Memory = module.exports = function Memory(options) {
    var self = this;
    options = options || {};
    this.data = options.data || {};
};

Set key to val, then callback fn(err).

  • param: String key

  • param: String val

  • param: Function fn

  • api: public

Memory.prototype.set = function(key, val, fn){
    this.data[key] = val;
    fn && fn();
};

Get key, then callback fn(err, val).

  • param: String key

  • param: Function fn

  • api: public

Memory.prototype.get = function(key, fn){
    fn(null, this.data[key]);
};

Remove key, then callback fn(err).

  • param: String key

  • param: Function fn

  • api: public

Memory.prototype.remove = function(key, fn){
    delete this.data[key];
    fn && fn();
};

Check if key exists, callback fn(err, exists).

  • param: String key

  • param: Function fn

  • api: public

Memory.prototype.has = function(key, fn){
    fn(null, key in this.data);
};

Fetch number of keys, callback fn(err, len).

  • param: Function fn

  • api: public

Memory.prototype.length = function(fn){
    fn(null, Object.keys(this.data).length);
};

Clear all keys, then callback fn(err).

  • param: Function fn

  • api: public

Memory.prototype.clear = function(fn){
    this.data = {};
    fn && fn();
};

Iterate with fn(err, val, key), then done() when finished.

  • param: Function fn

  • param: Function done

  • api: public

Memory.prototype.each = function(fn, done){
    var keys = Object.keys(this.data);
    for (var i = 0, len = keys.length; i < len; ++i) {
         fn(this.data[keys[i]], keys[i]);
    }
    done && done();
};

nstore

lib/stores/nstore.js

Module dependencies.

var Store = require('nstore');

Initialize nStore ... store.

Options

  • path Path to database, defaults to CWD/store.db

  • param: Object options

  • api: public

var nStore = module.exports = function nStore(options) {
    options = options || {};
    var path = options.path || process.cwd() + '/store.db';
    this.client = Store(path);
};

Set key to val, then callback fn(err).

  • param: String key

  • param: String val

  • param: Function fn

  • api: public

nStore.prototype.get = function(key, fn){
    this.client.get(key, fn);
};

Get key, then callback fn(err, val).

  • param: String key

  • param: Function fn

  • api: public

nStore.prototype.set = function(key, val, fn){
    this.client.save(key, val, fn);
};

Remove key, then callback fn(err).

  • param: String key

  • param: Function fn

  • api: public

nStore.prototype.remove = function(key, fn){
    this.client.remove(key, fn);
};

Check if key exists, callback fn(err, exists).

  • param: String key

  • param: Function fn

  • api: public

nStore.prototype.has = function(key, fn){
    this.client.get(key, function(err, val){
        fn(err, !!val);
    });
};

Fetch number of keys, callback fn(err, len).

  • param: Function fn

  • api: public

nStore.prototype.length = function(fn){
    fn(null, this.client.length);
};

Clear all keys, then callback fn(err).

  • param: Function fn

  • api: public

nStore.prototype.clear = function(fn){
    this.client.clear();
    fn && fn();
};

Iterate with fn(err, val, key), then done() when finished.

  • param: Function fn

  • param: Function done

  • api: public

nStore.prototype.each = function(fn, done){
    var stream = this.client.stream();
    stream.addListener('data', function(doc, meta){
        fn(doc, meta.key);
    });
    stream.addListener('end', function(){
        done && done();
    });
};

redis

lib/stores/redis.js

Module dependencies.

var redis = require('redis-client'),
    noop = function(){};

Initialize redis store.

Options

  • port Optional redis-server port
  • host Optional redis-server host
  • ... Other options passed to redis.createClient()

  • param: Object options

  • api: public

var Redis = module.exports = function Redis(options) {
    options = options || {};
    this.client = new redis.createClient(options.port, options.host, options);
};

Set key to val, then callback fn(err).

  • param: String key

  • param: String val

  • param: Function fn

  • api: public

Redis.prototype.set = function(key, val, fn){
    this.client.set(key, val, fn || noop);
};

Get key, then callback fn(err, val).

  • param: String key

  • param: Function fn

  • api: public

Redis.prototype.get = function(key, fn){
    this.client.get(key, function(err, buf){
        fn(err, buf ? buf.toString() : buf);
    });
};

Remove key, then callback fn(err).

  • param: String key

  • param: Function fn

  • api: public

Redis.prototype.remove = function(key, fn){
    this.client.del(key, fn);
};

Check if key exists, callback fn(err, exists).

  • param: String key

  • param: Function fn

  • api: public

Redis.prototype.has = function(key, fn){
    this.client.exists(key, function(err, exists){
        fn(err, !!exists);
    });
};

Fetch number of keys, callback fn(err, len).

  • param: Function fn

  • api: public

Redis.prototype.length = function(fn){
    this.client.dbsize(fn);
};

Clear all keys, then callback fn(err).

  • param: Function fn

  • api: public

Redis.prototype.clear = function(fn){
    this.client.flushdb(fn || noop);
};

Iterate with fn(err, val, key), then done() when finished.

  • param: Function fn

  • param: Function done

  • api: public

Redis.prototype.each = function(fn, done){
    var self = this;
    this.client.keys('*', function(err, keys){
        var keys = keys.toString().split(' '),
            pending = keys.length;
        for (var i = 0, len = keys.length; i < len; ++i) {
            (function(key){
                self.get(key, function(err, val){
                    fn(val, key);
                    --pending || (done && done());
                });
            })(keys[i]);
        }
    });
};