mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-03 19:11:22 +01:00
* refactor: start migrating to node-redis * few more zset fixes * fix: db.scan * fix: list methods * fix set methods * fix: hash methods * use hasOwn, remove cloning * sorted set fixes * fix: so data is converted to strings before saving otherwise node-redis throws below error TypeError: "arguments[2]" must be of type "string | Buffer", got number instead. * chore: remove comments * fix: zrank string param * use new close * chore: up dbsearch * test: add log * test: more log * test: log failing test * test: catch errors in formatApiResponse add await so exception goes to catch * tetst: add log * fix: dont set null/undefined values * test: more fixes
80 lines
2.1 KiB
JavaScript
80 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
const nconf = require('nconf');
|
|
const { createClient, createCluster, createSentinel } = require('redis');
|
|
const winston = require('winston');
|
|
|
|
const connection = module.exports;
|
|
|
|
connection.connect = async function (options) {
|
|
return new Promise((resolve, reject) => {
|
|
options = options || nconf.get('redis');
|
|
const redis_socket_or_host = options.host;
|
|
|
|
let cxn;
|
|
if (options.cluster) {
|
|
const rootNodes = options.cluster.map(node => ({ url : `redis://${node.host}:${node.port}` }));
|
|
cxn = createCluster({
|
|
...options.options,
|
|
rootNodes: rootNodes,
|
|
});
|
|
} else if (options.sentinels) {
|
|
const sentinelRootNodes = options.sentinels.map(sentinel => ({ host: sentinel.host, port: sentinel.port }));
|
|
cxn = createSentinel({
|
|
...options.options,
|
|
name: 'sentinel-db',
|
|
sentinelRootNodes,
|
|
});
|
|
} else if (redis_socket_or_host && String(redis_socket_or_host).indexOf('/') >= 0) {
|
|
// If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock
|
|
cxn = createClient({
|
|
...options.options,
|
|
password: options.password,
|
|
database: options.database,
|
|
socket: {
|
|
path: redis_socket_or_host,
|
|
reconnectStrategy: 3000,
|
|
},
|
|
});
|
|
} else {
|
|
// Else, connect over tcp/ip
|
|
cxn = createClient({
|
|
...options.options,
|
|
host: redis_socket_or_host,
|
|
port: options.port,
|
|
password: options.password,
|
|
database: options.database,
|
|
socket: {
|
|
reconnectStrategy: 3000,
|
|
},
|
|
});
|
|
}
|
|
|
|
const dbIdx = parseInt(options.database, 10);
|
|
if (!(dbIdx >= 0)) {
|
|
throw new Error('[[error:no-database-selected]]');
|
|
}
|
|
|
|
cxn.on('error', (err) => {
|
|
winston.error(err.stack);
|
|
reject(err);
|
|
});
|
|
cxn.on('ready', () => {
|
|
// back-compat with node_redis
|
|
cxn.batch = cxn.multi;
|
|
resolve(cxn);
|
|
});
|
|
cxn.connect().then(() => {
|
|
winston.info('Connected to Redis successfully');
|
|
}).catch((err) => {
|
|
winston.error('Error connecting to Redis:', err);
|
|
});
|
|
|
|
if (options.password) {
|
|
cxn.auth(options.password);
|
|
}
|
|
});
|
|
};
|
|
|
|
require('../../promisify')(connection);
|