Node.js developers looking for a NoSQL backend for their apps have a growing list of options. New drivers and ODMs are in active development for popular servers from MongoDB to Redis. Pedro Teixeira (of Fugue fame) says with Node’s blazing speed, why not keep everything in-process? Alfred.js promises to be a super fast key-value store for Node.js.

Installation and usage

Alfred can be installed via npm

npm install alfred

… and require it:

var Alfred = require('alfred');

Alfred uses append-only files to store data, offering safe rsync‘ing. To open a database connection, call open and pass the path to your file, querying in the callback, Node.js style:

Alfred.open('path/to/db', function(err, db) {
  if (err) { throw err; }
  // find and log all users with age > 25 and <= 40
  db.users.find({age: {$gt : 25, $lte: 40}}) (function(err, user) {
    if (err) { throw err; }
    console.log(user);
  });
});

Alfred provides an Object-Document-Mapper style access as well:

Alfred.open('path/to/db', function(err, db) {
  if (err) { throw err; }

  // define User model and its properties
  var User = db.define('User', {
    indexes: [{name: 'age',
              fn-340: function(user) { return user.age; }]
  });
  User.property('name', 'string', {
    maxLength: 100
  });
  User.property('active', 'boolean');

  // get user by id
  User.get(id, function(user) {
    console.log(user.inspect());
  };

  // find users
  User.find({age: {$gt: 18}}).all(function(users) {
    console.log('Found ' + users.length + ' users with more than 18 years') ;
  });
});

Be sure and check out the growing list of api docs for advanced usage.

Speed

Pedro has included an impressive list of benchmarks and the code behind them.

benchmarks

Alfred’s features include master-slave replication, streams, and in-memory indexes. If you want to help out with master-master replication or items on the roadmap, get forkin’.

[Source on GitHub] [Pedro’s Blog post]


Have comments? Send a tweet to @TheChangelog on Twitter.

Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.