From 64a22e73db52a199137275b83a04afd1ebdaa6b6 Mon Sep 17 00:00:00 2001 From: zhangmz Date: Wed, 22 Nov 2017 10:37:09 +0800 Subject: [PATCH 1/3] add driver option to support custom redis client. --- README.md | 12 +++++++--- index.js | 5 +++-- package.json | 1 + test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 50ad48c..6fd68e3 100644 --- a/README.md +++ b/README.md @@ -4,20 +4,26 @@ Fastify Redis connection plugin, with this you can share the same Redis connection in every part of your server. -Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the options that you pass to `register` will be passed to the Redis client. +Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the ``options`` that you pass to `register` which the ``redis`` option will be passed to the Redis client. ## Install ``` npm i fastify-redis --save ``` ## Usage -Add it to you project with `register` and you are done! +Add it to you project with `register` and you are done! You can access the *Redis* client via `fastify.redis`. + +And you can custom your own redis client, use ``driver`` option, now only support [ioredis](https://github.com/luin/ioredis) client, default is [redis](https://github.com/NodeRedis/node_redis). + ```js const fastify = require('fastify') fastify.register(require('fastify-redis'), { - host: '127.0.0.1' + driver: require('ioredis'), // when you custom your redis client. optional + redis: { + host: '127.0.0.1' + } }, err => { if (err) throw err }) diff --git a/index.js b/index.js index 521e1de..738e908 100644 --- a/index.js +++ b/index.js @@ -5,9 +5,10 @@ const redis = require('redis') function fastifyRedis (fastify, options, next) { var client = null - try { - client = redis.createClient(options) + // if custom redis module, default is redis. + var Redis = options.driver + client = Redis ? new Redis(options.redis) : redis.createClient(options.redis) } catch (err) { return next(err) } diff --git a/package.json b/package.json index 61c6ab6..ef41d85 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "homepage": "https://github.com/fastify/fastify-redis#readme", "devDependencies": { "fastify": "^0.30.2", + "ioredis": "^3.2.1", "standard": "^10.0.3", "tap": "^10.7.2" }, diff --git a/test.js b/test.js index 892ab40..1aa5569 100644 --- a/test.js +++ b/test.js @@ -9,7 +9,9 @@ t.beforeEach(done => { const fastify = Fastify() fastify.register(fastifyRedis, { - host: '127.0.0.1' + redis: { + host: '127.0.0.1' + } }, (err) => { t.error(err) }) @@ -27,9 +29,10 @@ t.beforeEach(done => { test('fastify.redis should exist', t => { t.plan(3) const fastify = Fastify() - fastify.register(fastifyRedis, { - host: '127.0.0.1' + redis: { + host: '127.0.0.1' + } }, (err) => { t.error(err) }) @@ -47,7 +50,58 @@ test('fastify.redis should be the redis client', t => { const fastify = Fastify() fastify.register(fastifyRedis, { - host: '127.0.0.1' + redis: { + host: '127.0.0.1' + } + }, (err) => { + t.error(err) + }) + + fastify.ready(err => { + t.error(err) + + fastify.redis.set('key', 'value', err => { + t.error(err) + fastify.redis.get('key', (err, val) => { + t.error(err) + t.equal(val, 'value') + + fastify.close() + }) + }) + }) +}) + +test('fastify.redis should exist when use the custom redis driver', t => { + t.plan(3) + const fastify = Fastify() + + fastify.register(fastifyRedis, { + driver: require('ioredis'), + redis: { + host: '127.0.0.1' + } + }, (err) => { + t.error(err) + }) + + fastify.ready(err => { + t.error(err) + t.ok(fastify.redis) + + fastify.close() + }) +}) + +test('fastify.redis should be the redis client when use the custom redis driver', t => { + t.plan(5) + const fastify = Fastify() + + fastify.register(fastifyRedis, { + driver: require('ioredis'), + redis: { + host: '127.0.0.1' + } }, (err) => { t.error(err) }) From ab081584494d00347b8edfd956ee689903297ddb Mon Sep 17 00:00:00 2001 From: wotermelon <593244920@qq.com> Date: Wed, 22 Nov 2017 22:14:46 +0800 Subject: [PATCH 2/3] To maintain backwards compatible. --- README.md | 8 +++----- index.js | 5 +++-- test.js | 20 +++++--------------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6fd68e3..829f3bd 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Fastify Redis connection plugin, with this you can share the same Redis connection in every part of your server. -Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the ``options`` that you pass to `register` which the ``redis`` option will be passed to the Redis client. +Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the ``options`` that you pass to `register` will be passed to the Redis client. ## Install ``` @@ -14,16 +14,14 @@ npm i fastify-redis --save Add it to you project with `register` and you are done! You can access the *Redis* client via `fastify.redis`. -And you can custom your own redis client, use ``driver`` option, now only support [ioredis](https://github.com/luin/ioredis) client, default is [redis](https://github.com/NodeRedis/node_redis). +And you can custom your own redis client, use ``driver`` option, now support [ioredis](https://github.com/luin/ioredis) client, default is [redis](https://github.com/NodeRedis/node_redis). ```js const fastify = require('fastify') fastify.register(require('fastify-redis'), { driver: require('ioredis'), // when you custom your redis client. optional - redis: { - host: '127.0.0.1' - } + host: '127.0.0.1' }, err => { if (err) throw err }) diff --git a/index.js b/index.js index 738e908..18ebced 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,9 @@ function fastifyRedis (fastify, options, next) { var client = null try { // if custom redis module, default is redis. - var Redis = options.driver - client = Redis ? new Redis(options.redis) : redis.createClient(options.redis) + const Driver = options.driver + delete options.driver + client = Driver ? new Driver(options) : redis.createClient(options) } catch (err) { return next(err) } diff --git a/test.js b/test.js index 1aa5569..e76986d 100644 --- a/test.js +++ b/test.js @@ -9,9 +9,7 @@ t.beforeEach(done => { const fastify = Fastify() fastify.register(fastifyRedis, { - redis: { - host: '127.0.0.1' - } + host: '127.0.0.1' }, (err) => { t.error(err) }) @@ -30,9 +28,7 @@ test('fastify.redis should exist', t => { t.plan(3) const fastify = Fastify() fastify.register(fastifyRedis, { - redis: { - host: '127.0.0.1' - } + host: '127.0.0.1' }, (err) => { t.error(err) }) @@ -50,9 +46,7 @@ test('fastify.redis should be the redis client', t => { const fastify = Fastify() fastify.register(fastifyRedis, { - redis: { - host: '127.0.0.1' - } + host: '127.0.0.1' }, (err) => { t.error(err) }) @@ -78,9 +72,7 @@ test('fastify.redis should exist when use the custom redis driver', t => { fastify.register(fastifyRedis, { driver: require('ioredis'), - redis: { - host: '127.0.0.1' - } + host: '127.0.0.1' }, (err) => { t.error(err) }) @@ -99,9 +91,7 @@ test('fastify.redis should be the redis client when use the custom redis driver' fastify.register(fastifyRedis, { driver: require('ioredis'), - redis: { - host: '127.0.0.1' - } + host: '127.0.0.1' }, (err) => { t.error(err) }) From 75d4c87f6328699225abc37479a8de5b2500f476 Mon Sep 17 00:00:00 2001 From: wotermelon <593244920@qq.com> Date: Wed, 22 Nov 2017 22:46:57 +0800 Subject: [PATCH 3/3] update usage. --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 829f3bd..491b628 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,14 @@ npm i fastify-redis --save Add it to you project with `register` and you are done! You can access the *Redis* client via `fastify.redis`. -And you can custom your own redis client, use ``driver`` option, now support [ioredis](https://github.com/luin/ioredis) client, default is [redis](https://github.com/NodeRedis/node_redis). +If needed, you can pass a custom ``driver`` option, such as [ioredis](https://github.com/luin/ioredis). By default the official [redis](https://github.com/NodeRedis/node_redis) client is used. + ```js const fastify = require('fastify') fastify.register(require('fastify-redis'), { - driver: require('ioredis'), // when you custom your redis client. optional + driver: require('ioredis'), host: '127.0.0.1' }, err => { if (err) throw err