9 - LevelDB

So far our chat server has only been receiving messages when other people are online. Wouldn't it be nice if we could add persistence to our p2p chat system so you would receive messages from your friends even though you were not online when they sent it?

Luckily there is a really great small database for node called LevelDB. The best thing about LevelDB is that it does very few things! It is a small key value store that runs inside your process and persists to disk.

You can use by installing the level module from npm

npm install level

You can read the documentation for leveldb here, https://github.com/level/levelup

Tasks

To complete this excercise you should make two new programs.

The first one, put.js should put the value world into the key hello and store it in a leveldb called test.db.

The other one, get.js should get the key hello from the same leveldb and print it out

Tips

To create a new levelup do var db = level(dbName), to put a key/value pair you can do db.put(key, value, callback) and to get a key you can do db.get(key, callback)

Testing

To test if your solution is corrent first run

node put.js

and then

node get.js

If you inserted the value correctly get.js should print out world

When you are done click here to go to the next exercise