@fastify/redis
Advanced tools
Comparing version 6.1.0 to 6.1.1
14
index.js
@@ -7,3 +7,3 @@ 'use strict' | ||
function fastifyRedis (fastify, options, next) { | ||
const { namespace, url, ...redisOptions } = options | ||
const { namespace, url, closeClient = false, ...redisOptions } = options | ||
@@ -25,3 +25,7 @@ let client = options.client || null | ||
if (!client) { | ||
if (client) { | ||
if (closeClient === true) { | ||
fastify.addHook('onClose', closeNamedInstance) | ||
} | ||
} else { | ||
try { | ||
@@ -45,3 +49,7 @@ if (url) { | ||
} else { | ||
if (!client) { | ||
if (client) { | ||
if (closeClient === true) { | ||
fastify.addHook('onClose', close) | ||
} | ||
} else { | ||
try { | ||
@@ -48,0 +56,0 @@ if (url) { |
{ | ||
"name": "@fastify/redis", | ||
"version": "6.1.0", | ||
"version": "6.1.1", | ||
"description": "Plugin to share a common Redis connection across Fastify.", | ||
@@ -42,3 +42,3 @@ "main": "index.js", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.24.1", | ||
"tsd": "^0.28.0", | ||
"why-is-node-running": "^2.2.2" | ||
@@ -45,0 +45,0 @@ }, |
@@ -103,2 +103,8 @@ # @fastify/redis | ||
To automatically close the client connection, set clientClose to true. | ||
```js | ||
fastify.register(require('@fastify/redis'), { client, closeClient: true }) | ||
``` | ||
## Registering multiple Redis client instances | ||
@@ -105,0 +111,0 @@ |
@@ -245,2 +245,89 @@ 'use strict' | ||
test('If closeClient is enabled, close the client.', (t) => { | ||
t.plan(10) | ||
const fastify = Fastify() | ||
const Redis = require('ioredis') | ||
const redis = new Redis({ host: 'localhost', port: 6379 }) | ||
redis.set('key', 'value', (err) => { | ||
t.error(err) | ||
redis.get('key', (err, val) => { | ||
t.error(err) | ||
t.equal(val, 'value') | ||
fastify.register(fastifyRedis, { | ||
client: redis, | ||
closeClient: true | ||
}) | ||
fastify.ready((err) => { | ||
t.error(err) | ||
t.equal(fastify.redis, redis) | ||
fastify.redis.set('key2', 'value2', (err) => { | ||
t.error(err) | ||
fastify.redis.get('key2', (err, val) => { | ||
t.error(err) | ||
t.equal(val, 'value2') | ||
const originalQuit = fastify.redis.quit | ||
fastify.redis.quit = (callback) => { | ||
t.pass('redis client closed') | ||
originalQuit.call(fastify.redis, callback) | ||
} | ||
fastify.close(function (err) { | ||
t.error(err) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('If closeClient is enabled, close the client namespace.', (t) => { | ||
t.plan(10) | ||
const fastify = Fastify() | ||
const Redis = require('ioredis') | ||
const redis = new Redis({ host: 'localhost', port: 6379 }) | ||
redis.set('key', 'value', (err) => { | ||
t.error(err) | ||
redis.get('key', (err, val) => { | ||
t.error(err) | ||
t.equal(val, 'value') | ||
fastify.register(fastifyRedis, { | ||
client: redis, | ||
namespace: 'foo', | ||
closeClient: true | ||
}) | ||
fastify.ready((err) => { | ||
t.error(err) | ||
t.equal(fastify.redis.foo, redis) | ||
fastify.redis.foo.set('key2', 'value2', (err) => { | ||
t.error(err) | ||
fastify.redis.foo.get('key2', (err, val) => { | ||
t.error(err) | ||
t.equal(val, 'value2') | ||
const originalQuit = fastify.redis.foo.quit | ||
fastify.redis.foo.quit = (callback) => { | ||
t.pass('redis client closed') | ||
originalQuit.call(fastify.redis.foo, callback) | ||
} | ||
fastify.close(function (err) { | ||
t.error(err) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('fastify.redis.test should throw with duplicate connection namespaces', (t) => { | ||
@@ -247,0 +334,0 @@ t.plan(1) |
@@ -27,2 +27,5 @@ import { FastifyPluginCallback } from 'fastify'; | ||
namespace?: string; | ||
/** | ||
* @default false | ||
*/ | ||
closeClient?: boolean; | ||
@@ -29,0 +32,0 @@ } |
26580
582
235