-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathredis_client.js
51 lines (41 loc) · 1.47 KB
/
redis_client.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
const redis = require('redis');
const bluebird = require('bluebird');
const config = require('better-config');
const fs = require('fs');
// Add extra definitions for RedisTimeSeries commands.
redis.addCommand('ts.add'); // redis.ts_addAsync
redis.addCommand('ts.range'); // redis.ts_rangeAsync
// Promisify all the functions exported by node_redis.
bluebird.promisifyAll(redis);
// Create a client and connect to Redis using configuration
// from config.json.
const clientConfig = {
host: config.get('dataStores.redis.host'),
port: config.get('dataStores.redis.port')
};
if (config.get('dataStores.redis.database')) {
database: parseInt(config.get('dataStores.redis.database'))
}
if (config.get('dataStores.redis.password')) {
clientConfig.password = config.get('dataStores.redis.password');
}
if (config.get('dataStores.redis.username')) {
clientConfig.username = config.get('dataStores.redis.username');
}
if (config.get('dataStores.redis.ca-file')) {
clientConfig.tls = {};
clientConfig.tls.ca = [ fs.readFileSync(config.get('dataStores.redis.ca-file')) ];
clientConfig.tls.rejectUnauthorized = false;
clientConfig.tls.requestCert = true;
}
const client = redis.createClient(clientConfig);
// This is a catch all basic error handler.
client.on('error', error => console.log(error));
module.exports = {
/**
* Get the application's connected Redis client instance.
*
* @returns {Object} - a connected node_redis client instance.
*/
getClient: () => client,
};