-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (45 loc) · 1.29 KB
/
index.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
const redis = require('redis');
const cast = require('./lib/cast');
const originalCreateClient = redis.createClient;
// Overwrite the createClient() method so we can add a custom method to the redis client.
redis.createClient = (...args) => {
const client = originalCreateClient(...args);
client.cmd = (cmds, opts) => new Promise((resolve, reject) => {
const options = opts instanceof Object ? opts : {};
if (cmds === null || cmds === undefined) {
return resolve(undefined);
}
if (!Array.isArray(cmds)) {
return reject(Error('redis.cmd() first argument must be an array'));
}
if (cmds.length === 0) {
return resolve(undefined);
}
// Execute multiple commands
// Calling multi with an empty array or even null will return an empty array
if (Array.isArray(cmds[0])) {
return client.multi(cmds).exec((err, resp) => {
if (err) {
return reject(err);
}
if (options.noCast) {
return resolve(resp);
}
return resolve(cast(resp));
});
}
// Execute a single command
return client[cmds[0]](...cmds.slice(1), (err, resp) => {
if (err) {
return reject(err);
}
if (options.noCast) {
return resolve(resp);
}
return resolve(cast(resp));
});
});
return client;
};
// Export the module for convenience
module.exports = redis;