Skip to content

Commit edfb960

Browse files
darkgl0wEomm
authored andcommitted
Allow multiple redis instances registering (#29)
1 parent 9b0febc commit edfb960

File tree

3 files changed

+267
-24
lines changed

3 files changed

+267
-24
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,68 @@ fastify.register(fastifyRedis, { client: redis })
5656
// ...
5757
```
5858

59+
## Registering multiple Redis client instances
60+
61+
By using the `namespace` option you can register multiple Redis client instances.
62+
63+
```js
64+
const fastify = require('fastify')
65+
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
66+
67+
fastify
68+
.register(require('fastify-redis'), {
69+
host: '127.0.0.1',
70+
port: 6380,
71+
namespace: 'hello'
72+
})
73+
.register(require('fastify-redis'), {
74+
client: redis,
75+
namespace: 'world'
76+
})
77+
78+
// Here we will use the `hello` named instance
79+
fastify.get('/hello', (req, reply) => {
80+
const { redis } = fastify
81+
82+
redis.hello.get(req.query.key, (err, val) => {
83+
reply.send(err || val)
84+
})
85+
})
86+
87+
fastify.post('/hello', (req, reply) => {
88+
const { redis } = fastify
89+
90+
redis['hello'].set(req.body.key, req.body.value, (err) => {
91+
reply.send(err || { status: 'ok' })
92+
})
93+
})
94+
95+
// Here we will use the `world` named instance
96+
fastify.get('/world', (req, reply) => {
97+
const { redis } = fastify
98+
99+
redis['world'].get(req.query.key, (err, val) => {
100+
reply.send(err || val)
101+
})
102+
})
103+
104+
fastify.post('/world', (req, reply) => {
105+
const { redis } = fastify
106+
107+
redis.world.set(req.body.key, req.body.value, (err) => {
108+
reply.send(err || { status: 'ok' })
109+
})
110+
})
111+
112+
fastify.listen(3000, function (err) {
113+
if (err) {
114+
fastify.log.error(err)
115+
process.exit(1)
116+
}
117+
})
118+
119+
```
120+
59121
## Acknowledgements
60122

61123
This project is kindly sponsored by:

index.js

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,52 @@ const fp = require('fastify-plugin')
44
const Redis = require('ioredis')
55

66
function fastifyRedis (fastify, options, next) {
7-
var client = options.client || null
7+
const namespace = options.namespace
8+
delete options.namespace
89

9-
if (!client) {
10-
try {
11-
client = new Redis(options)
12-
} catch (err) {
13-
return next(err)
10+
let client = options.client || null
11+
12+
if (namespace) {
13+
if (!fastify.redis) {
14+
fastify.decorate('redis', {})
1415
}
15-
fastify.addHook('onClose', close)
16-
}
1716

18-
fastify.decorate('redis', client)
17+
if (fastify.redis[namespace]) {
18+
return next(new Error(`Redis '${namespace}' instance namespace has already been registered`))
19+
}
20+
21+
const closeNamedInstance = (fastify, done) => {
22+
fastify.redis[namespace].quit(done)
23+
}
24+
25+
if (!client) {
26+
try {
27+
client = new Redis(options)
28+
} catch (err) {
29+
return next(err)
30+
}
31+
32+
fastify.addHook('onClose', closeNamedInstance)
33+
}
34+
35+
fastify.redis[namespace] = client
36+
} else {
37+
if (fastify.redis) {
38+
next(new Error('fastify-redis has already been registered'))
39+
} else {
40+
if (!client) {
41+
try {
42+
client = new Redis(options)
43+
} catch (err) {
44+
return next(err)
45+
}
46+
47+
fastify.addHook('onClose', close)
48+
}
49+
50+
fastify.decorate('redis', client)
51+
}
52+
}
1953

2054
next()
2155
}

test.js

Lines changed: 162 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const test = t.test
55
const Fastify = require('fastify')
66
const fastifyRedis = require('./index')
77

8-
t.beforeEach(done => {
8+
t.beforeEach((done) => {
99
const fastify = Fastify()
1010

1111
fastify.register(fastifyRedis, {
1212
host: '127.0.0.1'
1313
})
1414

15-
fastify.ready(err => {
15+
fastify.ready((err) => {
1616
t.error(err)
1717

1818
fastify.redis.flushall(() => {
@@ -22,33 +22,33 @@ t.beforeEach(done => {
2222
})
2323
})
2424

25-
test('fastify.redis should exist', t => {
25+
test('fastify.redis should exist', (t) => {
2626
t.plan(2)
2727
const fastify = Fastify()
2828
fastify.register(fastifyRedis, {
2929
host: '127.0.0.1'
3030
})
3131

32-
fastify.ready(err => {
32+
fastify.ready((err) => {
3333
t.error(err)
3434
t.ok(fastify.redis)
3535

3636
fastify.close()
3737
})
3838
})
3939

40-
test('fastify.redis should be the redis client', t => {
40+
test('fastify.redis should be the redis client', (t) => {
4141
t.plan(4)
4242
const fastify = Fastify()
4343

4444
fastify.register(fastifyRedis, {
4545
host: '127.0.0.1'
4646
})
4747

48-
fastify.ready(err => {
48+
fastify.ready((err) => {
4949
t.error(err)
5050

51-
fastify.redis.set('key', 'value', err => {
51+
fastify.redis.set('key', 'value', (err) => {
5252
t.error(err)
5353
fastify.redis.get('key', (err, val) => {
5454
t.error(err)
@@ -60,41 +60,84 @@ test('fastify.redis should be the redis client', t => {
6060
})
6161
})
6262

63-
test('promises support', t => {
63+
test('fastify.redis.test namespace should exist', (t) => {
64+
t.plan(3)
65+
66+
const fastify = Fastify()
67+
fastify.register(fastifyRedis, {
68+
host: '127.0.0.1',
69+
namespace: 'test'
70+
})
71+
72+
fastify.ready((err) => {
73+
t.error(err)
74+
t.ok(fastify.redis)
75+
t.ok(fastify.redis.test)
76+
77+
fastify.close()
78+
})
79+
})
80+
81+
test('fastify.redis.test should be the redis client', (t) => {
82+
t.plan(4)
83+
const fastify = Fastify()
84+
85+
fastify.register(fastifyRedis, {
86+
host: '127.0.0.1',
87+
namespace: 'test'
88+
})
89+
90+
fastify.ready((err) => {
91+
t.error(err)
92+
93+
fastify.redis.test.set('key_namespace', 'value_namespace', (err) => {
94+
t.error(err)
95+
fastify.redis.test.get('key_namespace', (err, val) => {
96+
t.error(err)
97+
t.equal(val, 'value_namespace')
98+
99+
fastify.close()
100+
})
101+
})
102+
})
103+
})
104+
105+
test('promises support', (t) => {
64106
t.plan(2)
65107
const fastify = Fastify()
66108

67109
fastify.register(fastifyRedis, {
68110
host: '127.0.0.1'
69111
})
70112

71-
fastify.ready(err => {
113+
fastify.ready((err) => {
72114
t.error(err)
73115

74-
fastify.redis.set('key', 'value')
116+
fastify.redis
117+
.set('key', 'value')
75118
.then(() => {
76119
return fastify.redis.get('key')
77120
})
78-
.then(val => {
121+
.then((val) => {
79122
t.equal(val, 'value')
80123
fastify.close()
81124
})
82-
.catch(err => t.fail(err))
125+
.catch((err) => t.fail(err))
83126
})
84127
})
85128

86-
test('custom client', t => {
129+
test('custom client', (t) => {
87130
t.plan(7)
88131
const fastify = Fastify()
89132
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
90133

91134
fastify.register(fastifyRedis, { client: redis })
92135

93-
fastify.ready(err => {
136+
fastify.ready((err) => {
94137
t.error(err)
95138
t.is(fastify.redis, redis)
96139

97-
fastify.redis.set('key', 'value', err => {
140+
fastify.redis.set('key', 'value', (err) => {
98141
t.error(err)
99142
fastify.redis.get('key', (err, val) => {
100143
t.error(err)
@@ -110,3 +153,107 @@ test('custom client', t => {
110153
})
111154
})
112155
})
156+
157+
test('custom client inside a namespace', (t) => {
158+
t.plan(7)
159+
const fastify = Fastify()
160+
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
161+
162+
fastify.register(fastifyRedis, {
163+
namespace: 'test',
164+
client: redis
165+
})
166+
167+
fastify.ready((err) => {
168+
t.error(err)
169+
t.is(fastify.redis.test, redis)
170+
171+
fastify.redis.test.set('key', 'value', (err) => {
172+
t.error(err)
173+
fastify.redis.test.get('key', (err, val) => {
174+
t.error(err)
175+
t.equal(val, 'value')
176+
177+
fastify.close(function (err) {
178+
t.error(err)
179+
fastify.redis.test.quit(function (err) {
180+
t.error(err)
181+
})
182+
})
183+
})
184+
})
185+
})
186+
})
187+
188+
test('fastify.redis.test should throw with duplicate connection namespaces', (t) => {
189+
t.plan(1)
190+
191+
const namespace = 'test'
192+
193+
const fastify = Fastify()
194+
t.teardown(() => fastify.close())
195+
196+
fastify
197+
.register(fastifyRedis, {
198+
host: '127.0.0.1',
199+
namespace
200+
})
201+
.register(fastifyRedis, {
202+
host: '127.0.0.1',
203+
namespace
204+
})
205+
206+
fastify.ready((err) => {
207+
t.is(err.message, `Redis '${namespace}' instance namespace has already been registered`)
208+
})
209+
})
210+
211+
test('Should throw when trying to register multiple instances without giving a namespace', (t) => {
212+
t.plan(1)
213+
214+
const fastify = Fastify()
215+
t.teardown(() => fastify.close())
216+
217+
fastify
218+
.register(fastifyRedis, {
219+
host: '127.0.0.1'
220+
})
221+
.register(fastifyRedis, {
222+
host: '127.0.0.1'
223+
})
224+
225+
fastify.ready((err) => {
226+
t.is(err.message, 'fastify-redis has already been registered')
227+
})
228+
})
229+
230+
test('Should not throw within different contexts', (t) => {
231+
t.plan(1)
232+
233+
const fastify = Fastify()
234+
t.teardown(() => fastify.close())
235+
236+
fastify.register(function (instance, options, next) {
237+
instance.register(fastifyRedis, {
238+
host: '127.0.0.1'
239+
})
240+
next()
241+
})
242+
243+
fastify.register(function (instance, options, next) {
244+
instance
245+
.register(fastifyRedis, {
246+
host: '127.0.0.1',
247+
namespace: 'test1'
248+
})
249+
.register(fastifyRedis, {
250+
host: '127.0.0.1',
251+
namespace: 'test2'
252+
})
253+
next()
254+
})
255+
256+
fastify.ready((error) => {
257+
t.is(error, null)
258+
})
259+
})

0 commit comments

Comments
 (0)