-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtambo.js
More file actions
50 lines (43 loc) · 1.26 KB
/
tambo.js
File metadata and controls
50 lines (43 loc) · 1.26 KB
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
var net = require('net');
var delimiter = '\r\n';
var create_message = function(action, key, value) {
// don't add delimiter if value is not specified
value = value || "";
if (value) {
value += delimiter;
}
return action + delimiter + key + delimiter + value + delimiter + delimiter + delimiter;
}
var connect = function(config, callback) {
var client = net.connect({host: config.host, port: config.port}, function() {
});
var send_action = function(action, key, value, callback) {
client.once('data', function(data) {
callback("error", data.toString());
});
client.write(create_message(action, key, value));
}
var api = {
create: function(key, value, callback) {
send_action("CREATE", key, value, callback);
},
read: function(key, callback) {
send_action("READ", key, null, callback);
},
update: function(key, value, callback) {
send_action("UPDATE", key, value, callback);
},
remove: function(key, value, callback) {
send_action("DELETE", key, null, callback);
},
close: function() {
client.end();
}
}
client.once('data', function(data) {
send_action('USE', config.dbname, null, function () {
callback(api);
});
});
}
exports.connect = connect;