1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
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
};
|