diff --git a/src/redis-brain.js b/src/redis-brain.js index e5accd2..482c14e 100644 --- a/src/redis-brain.js +++ b/src/redis-brain.js @@ -32,21 +32,26 @@ module.exports = function (robot, redis = Redis) { let info = null let prefix = '' + let database = null try { info = new URL(redisUrl) - prefix = (info.pathname ? info.pathname.replace('/', '') : undefined) || 'hubot' + database = Number((info.pathname ? info.pathname.replace('/', '') : undefined) || 0) } catch (err) { if (err.code === 'ERR_INVALID_URL') { const urlPath = redisUrl.replace(/rediss?:\/{2}:?(.*@)?/, '') info = new URL(`redis://${urlPath}`) - prefix = info.search?.replace('?', '') || 'hubot' } } + prefix = info.search?.replace('?', '') || 'hubot' let redisOptions = { url: redisUrl } + if (database) { + redisOptions = Object.assign(redisOptions || {}, { database }) + } + let redisSocket = null if (info.protocol === 'rediss:') { diff --git a/test/redis-brain-test.js b/test/redis-brain-test.js index eb47f7f..47b8b81 100644 --- a/test/redis-brain-test.js +++ b/test/redis-brain-test.js @@ -110,8 +110,8 @@ describe('redis-brain', () => { delete process.env.REDIS_NO_CHECK }) - it('Setting the prefix with redis://localhost:6379/prefix-for-redis-key', () => { - process.env.REDIS_URL = 'redis://localhost:6379/prefix-for-redis-key' + it('Setting the prefix with redis://localhost:6379/1?prefix-for-redis-key', () => { + process.env.REDIS_URL = 'redis://localhost:6379/1?prefix-for-redis-key' const robot = new Robot(null, 'shell', false, 'hubot') const delegate = { data: {}, @@ -124,6 +124,49 @@ describe('redis-brain', () => { } redisBrain(robot, { createClient: (options) => { + expect(options.database).to.equal(1) + return new RedisMock(delegate) + } + }) + robot.run() + }) + + it('Setting the prefix with no database number specified redis://localhost?prefix-for-redis-key', () => { + process.env.REDIS_URL = 'redis://localhost?prefix-for-redis-key' + const robot = new Robot(null, 'shell', false, 'hubot') + const delegate = { + data: {}, + async get (key) { + expect(key).to.equal('prefix-for-redis-key:storage') + robot.shutdown() + delete process.env.REDIS_URL + return this.data[key] + } + } + redisBrain(robot, { + createClient: (options) => { + expect(options.database).to.be.undefined + return new RedisMock(delegate) + } + }) + robot.run() + }) + + it('Setting the prefix with no database number specified and a trailing slash redis://localhost:6379/?prefix-for-redis-key', () => { + process.env.REDIS_URL = 'redis://localhost:6379/?prefix-for-redis-key' + const robot = new Robot(null, 'shell', false, 'hubot') + const delegate = { + data: {}, + async get (key) { + expect(key).to.equal('prefix-for-redis-key:storage') + robot.shutdown() + delete process.env.REDIS_URL + return this.data[key] + } + } + redisBrain(robot, { + createClient: (options) => { + expect(options.database).to.be.undefined return new RedisMock(delegate) } }) @@ -144,6 +187,7 @@ describe('redis-brain', () => { } redisBrain(robot, { createClient: (options) => { + expect(options.database).to.be.undefined return new RedisMock(delegate) } })