Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nodejs + LevelDB Talk

Intro into level db

  • It's a database, it's by google, and is what supports IndexDB in the chrome browser
  • It's also been used to store data in riak
  • It lives in your process (a bit like sqlite)
  • Inspired by Big Table, but designed to work on mobile devices
  • There is great support for leveldb in the node.js
  • Node is a great tool for sticking technologies together
  • This is driving a new database ecosystem, and way of creating a modular database
$ mkdir talk
$ cd talk
$ sudo npm install level

$ node

> var level = require('level');
> var db = level('db');

> db.put('hello', 'norwich');
> db.put('goodbye', 'cruel world');

> db.get('hello', console.log);

> db.put('d', 'true')
> db.put('c', 'true')
> db.put('b', 'true')
> db.put('a', 'true')

> var x = db.readStream()
> x.on('data', console.log)

// this is a sorted key list

> var x = db.readStream({start:'b', end:'d'}).on('data', console.log);

// you can sort sub-ranges, and in reverse

> var x = db.readStream({start:'b', end:'d', reverse:true}).on('data', console.log);

There is a also a batch insert mode

Level-sub level

Databases don't normally contain just one kind of data, you want to mix types together. We can use level-sublevel for this.

$ sudo npm install level-sublevel
$ node

> var db = require('level')('db');
> var sublevel = require('level-sublevel')(db);
> var stuffdb = sublevel.sublevel('stuff');
> stuffdb.put('foo', 'bar')l
> var _ = stuffdb.readStream().on('data', console.log);

// what's going on?

> var _ = sublevel.readStream().on('data', console.log);
> var _ = db.readStream().on('data', console.log);

// it's better to use sub-key ranges than having multiple databases


$ sudo npm install express
$ nano server.js
var app = require('express')();
var db = require('level')('db');

app.get("/:key", function(req, res){
  db.get(req.params.key, function(err, data){
    res.json({value: data});

app.get("/", function(req, res){
  var results = [];
  db.readStream().on('data', function(data){
  .on('end', function(){

$ node server


lev is a command line tool to inspect your database

$ npm install lev -g
$ lev db

> ls
> get hello
> cd stuff
> ls


$ npm install level-geospatial
$ node load
$ less uk-postcodes.csv
$ less load.js

> var db = require('level')('geo');
> var geo = require('level-geospatial')(db)
> var _={lat:52.6, lon: 1.3}, 10000).on('data', console.log)
> geo.put({lat:52.6, lon:1.3}, 'nordevcon', 'hello world')
> var _={lat:52.6, lon: 1.3}, 10000).on('data', console.log)

Other places to look

  • levelgraph

A graph database (triplestore) built on leveldb

  • azureleveldown

An implementation of LevelDown for Windows Azure Table Storage

  • LevelUp Wiki

A more complete list of leveldb modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.