-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_storage.js
64 lines (57 loc) · 2.17 KB
/
redis_storage.js
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
var redis = require('redis'); //https://github.com/NodeRedis/node_redis
/*
* All optional
*
* config = {
* namespace: namespace,
* host: host,
* port: port
* }
* // see
* https://github.com/NodeRedis/node_redis
* #options-is-an-object-with-the-following-possible-properties for a full list of the valid options
*/
module.exports = function(config) {
config = config || {};
config.namespace = config.namespace || 'botkit:store';
var storage = {},
client = redis.createClient(config), // could pass specific redis config here
methods = config.methods || ['teams', 'users', 'channels'];
// Implements required API methods
for (var i = 0; i < methods.length; i++) {
storage[methods[i]] = function(hash) {
return {
get: function(id, cb) {
client.hget(config.namespace + ':' + hash, id, function(err, res) {
cb(err, JSON.parse(res));
});
},
save: function(object, cb) {
if (!object.id) // Silently catch this error?
return cb(new Error('The given object must have an id property'), {});
client.hset(config.namespace + ':' + hash, object.id, JSON.stringify(object), cb);
},
all: function(cb, options) {
client.hgetall(config.namespace + ':' + hash, function(err, res) {
if (err)
return cb(err, {});
if (null === res)
return cb(err, res);
var parsed;
var array = [];
for (var i in res) {
parsed = JSON.parse(res[i]);
res[i] = parsed;
array.push(parsed);
}
cb(err, options && options.type === 'object' ? res : array);
});
},
allById: function(cb) {
this.all(cb, {type: 'object'});
}
};
}(methods[i]);
}
return storage;
};