-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredis.js
53 lines (46 loc) · 1.73 KB
/
predis.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
const Promise = require("bluebird");
const redis = require("redis");
const commands = require("redis-commands");
module.exports = {
createClient(redis_options = {}, _redisClient = redis) {
const opts = {
password: null,
server: "127.0.0.1",
port: 6379,
...redis_options
};
const redisClient = _redisClient.createClient(opts.port, opts.server);
const predis = {};
const exclude = ["multi", "exec"];
redisClient.on("error", (err) => {
console.error(`ERROR in REDIS: ${err}`);
});
if (opts.password) {
redisClient.auth(opts.password, () => {
console.log(`Connected and authenticated to REDIS db at ${opts.server}:${opts.port}`);
});
} else {
redisClient.on("connect", () => {
console.log(`Connected to REDIS db at ${opts.server}:${opts.port}`);
});
}
commands.list.forEach((command) => {
predis[command] = function(...args) {
if (exclude.includes(command)) {
return redisClient[command].apply(redisClient, arguments);
}
return new Promise((resolve, reject) => {
redisClient[command].apply(redisClient, [...args, (error, result) => {
if (error) {
console.error("Predis :: ERROR! ", error, command, args);
reject(error);
} else {
resolve(result);
}
}]);
});
};
});
return predis;
}
};