Skip to content

Commit 61a9b37

Browse files
committed
First code commit
1 parent 00edb30 commit 61a9b37

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const fp = require('fastify-plugin')
4+
const redis = require('redis')
5+
6+
function fastifyRedis (fastify, options, next) {
7+
var client = null
8+
9+
try {
10+
client = redis.createClient(options)
11+
} catch (err) {
12+
return next(err)
13+
}
14+
15+
fastify
16+
.decorate('redis', client)
17+
.addHook('onClose', close)
18+
19+
next()
20+
}
21+
22+
function close (fastify, done) {
23+
fastify.redis.quit(done)
24+
}
25+
26+
module.exports = fp(fastifyRedis, '>=0.13.1')

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "fastify-redis",
3+
"version": "0.1.0",
4+
"description": "Plugin to share a common Redis connection across Fastify.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "standard && tap test.js",
8+
"redis": "docker run -p 6379:6379 redis:3.0.7"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/fastify/fastify-redis.git"
13+
},
14+
"keywords": [
15+
"fastify",
16+
"redis",
17+
"database",
18+
"speed",
19+
"cache"
20+
],
21+
"author": "Tomas Della Vedova - @delvedor (http://delved.org)",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/fastify/fastify-redis/issues"
25+
},
26+
"homepage": "https://github.com/fastify/fastify-redis#readme",
27+
"devDependencies": {
28+
"fastify": "^0.14.0",
29+
"standard": "^10.0.0",
30+
"tap": "^10.3.1"
31+
},
32+
"dependencies": {
33+
"fastify-plugin": "^0.1.0",
34+
"redis": "^2.7.1"
35+
}
36+
}

test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const test = t.test
5+
const Fastify = require('fastify')
6+
const fastifyRedis = require('./index')
7+
8+
t.beforeEach(done => {
9+
const fastify = Fastify()
10+
11+
fastify.register(fastifyRedis, {
12+
host: '127.0.0.1'
13+
}, (err) => {
14+
t.error(err)
15+
})
16+
17+
fastify.ready(err => {
18+
t.error(err)
19+
20+
fastify.redis.flushdb(() => {
21+
fastify.close()
22+
done()
23+
})
24+
})
25+
})
26+
27+
test('fastify.redis should exist', t => {
28+
t.plan(3)
29+
const fastify = Fastify()
30+
31+
fastify.register(fastifyRedis, {
32+
host: '127.0.0.1'
33+
}, (err) => {
34+
t.error(err)
35+
})
36+
37+
fastify.ready(err => {
38+
t.error(err)
39+
t.ok(fastify.redis)
40+
41+
fastify.close()
42+
})
43+
})
44+
45+
test('fastify.redis should be the redis client', t => {
46+
t.plan(5)
47+
const fastify = Fastify()
48+
49+
fastify.register(fastifyRedis, {
50+
host: '127.0.0.1'
51+
}, (err) => {
52+
t.error(err)
53+
})
54+
55+
fastify.ready(err => {
56+
t.error(err)
57+
58+
fastify.redis.set('key', 'value', err => {
59+
t.error(err)
60+
fastify.redis.get('key', (err, val) => {
61+
t.error(err)
62+
t.equal(val, 'value')
63+
64+
fastify.close()
65+
})
66+
})
67+
})
68+
})

0 commit comments

Comments
 (0)