var kyoto = require('kyoto'), uuid = require(__dirname + '/uuid'); var db; var generateId = function(callback) { var id = uuid.generate(14); db.get(id, function(err, value) { if (value) { generateId(); } else { callback(id); } }); }; var get = function(id, callback) { db.get(id, function(err, value) { if (value) { value = JSON.parse(value); } callback(value); }); }; var add = function(post, callback) { generateId(function(id) { var data = { content: post.content, language: post.language, time: new Date() }; db.set(id, JSON.stringify(data), function(err) { callback(err, id); }); }); }; var init = function(config, callback) { db = new kyoto.open(config.database, 'a+', function(err) { if (err) throw err; process.on('uncaughtException', function(exeption) { console.error('%j', exeption); process.exit(1); }); process.on('exit', function() { db.close(function(err) { console.log(err); }); }); var thismodule = { get: get, add: add }; callback(thismodule); }); }; module.exports = { init: init };