Skip to content

Instantly share code, notes, and snippets.

@brandonb927
Created March 16, 2014 18:13
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brandonb927/9587436 to your computer and use it in GitHub Desktop.
Save brandonb927/9587436 to your computer and use it in GitHub Desktop.
Simple JSON database with Node.JS

From: http://run-node.com/littlest-database-that-could/

I've written numerous tiny databases. They don't have much features, but they don't need much features. Usually I'm looking for fast simple key/value stores and Node never disappoints. The point here is, why abstract key value store when JS gives us one for free, as it's most basic component: object.

Will it meet every need? No. But it will meet ALOT of scenarios.

In memory JS object lookups, were talking hundreds of thousands of lookups (you'll easily flood http before the db), and save hundreds of thousands of records in a JSON file written to disk. Not a 200ms r/t to some hosted Redis. Hey, that's fine if that's your thing.

Here's the requirements:

  1. In-memory key value store. JS gives us this already, it's called Object. Why must we build abstractions on it?
  2. Save to disk. I've done them where they only save on program exit for fun and extreme minimal disk action, but here we'll just save when we add a key, and pretty up the JSON a bit in case we like to view our db in a text editor later.
  3. Easy access via http, and we'll use curl because its awesome.

Node, HTTP, POST, JSON - all we need.

Here's the entire program:

var js = JSON.stringify;  
var db = {};

require('http').createServer(function(req, res) {

  req.on('data', function(b) {
    var s = b.toString();

    if(s == "ls") {
      return res.end(js(Object.keys(db), null, 4));
    }

    var kv = s.split(';');
    if(kv.length == 1) {
      return res.end(js(db[kv], null, 4));
    }

    res.end(JSON.stringify(db[kv.splice(0, 1)] = kv));
    require('fs').writeFile('./db.json', js(db, null, 4));
  })

}).listen(80);

We'll just separate keys and values with a semicolon, and even save arrays by using multiple semicolons.

using curl:

# save key
curl -d "key;value" localhost  
# save array
curl -d "key;value;value;value" localhost  
# show keys
curl -d "ls" localhost  
# get value
curl -d "key" localhost  

Of course we could also use XHR POST from the browser as well.

var post = function(u, d, cb) {
  var x = new XMLHttpRequest;
    x.open('POST', u, true);
    x.send(d);
    x.onload = cb;
}
post('http://localhost', 'a=1', console.log.bind(console));

And we're good. Instant database.

Does the concept work for a real app?

Now, I'm not Netflix, but I run this type of thing in Production with decent loads, and it's good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment