From 6faeb435ff951df7f2f080074ef52f4df7177f8b Mon Sep 17 00:00:00 2001 From: zzy Date: Tue, 23 Jul 2019 11:12:31 +0800 Subject: [PATCH] #17 add url for redisOptions --- README.md | 11 ++++++----- dist/redis-client.provider.js | 21 ++++++++++++++++++--- dist/redis.interface.d.ts | 1 + lib/redis-client.provider.ts | 18 +++++++++++++++--- lib/redis.interface.ts | 3 ++- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4530533..f73fa28 100644 --- a/README.md +++ b/README.md @@ -56,17 +56,17 @@ export default { password: process.env.REDIS_PASSWORD, keyPrefix: process.env.REDIS_PRIFIX, } +Or +export default { + url: 'redis://:authpassword@127.0.0.1:6380/4', +} ``` With multi client ```typescript export default [ { name:'test1', - host: process.env.REDIS_HOST, - port: parseInt(process.env.REDIS_PORT), - db: parseInt(process.env.REDIS_DB), - password: process.env.REDIS_PASSWORD, - keyPrefix: process.env.REDIS_PRIFIX, + url: 'redis://:authpassword@127.0.0.1:6380/4', }, { name:'test2', @@ -101,6 +101,7 @@ interface RedisOptions { * client name. default is a uuid, unique. */ name?: string; + url?: string; port?: number; host?: string; /** diff --git a/dist/redis-client.provider.js b/dist/redis-client.provider.js index 7376b57..e7ee965 100644 --- a/dist/redis-client.provider.js +++ b/dist/redis-client.provider.js @@ -17,18 +17,33 @@ exports.createClient = () => ({ if (clients.has(o.name)) { throw new RedisClientError(`client ${o.name} is exists`); } - clients.set(o.name, new Redis(o)); + if (o.url) { + clients.set(o.name, new Redis(o.url)); + } + else { + clients.set(o.name, new Redis(o)); + } } else { if (clients.has(defaultKey)) { throw new RedisClientError('default client is exists'); } - clients.set(defaultKey, new Redis(o)); + if (o.url) { + clients.set(defaultKey, new Redis(o.url)); + } + else { + clients.set(defaultKey, new Redis(o)); + } } } } else { - clients.set(defaultKey, new Redis(options)); + if (options.url) { + clients.set(defaultKey, new Redis(options.url)); + } + else { + clients.set(defaultKey, new Redis(options)); + } } return { defaultKey, diff --git a/dist/redis.interface.d.ts b/dist/redis.interface.d.ts index 836516b..f00a6c5 100644 --- a/dist/redis.interface.d.ts +++ b/dist/redis.interface.d.ts @@ -2,6 +2,7 @@ import { ModuleMetadata } from '@nestjs/common/interfaces'; import { RedisOptions } from 'ioredis'; export interface RedisModuleOptions extends RedisOptions { name?: string; + url?: string; } export interface RedisModuleAsyncOptions extends Pick { useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise | Promise; diff --git a/lib/redis-client.provider.ts b/lib/redis-client.provider.ts index 0ba4332..e6b67b2 100644 --- a/lib/redis-client.provider.ts +++ b/lib/redis-client.provider.ts @@ -22,16 +22,28 @@ export const createClient = () => ({ if (clients.has(o.name)) { throw new RedisClientError(`client ${o.name} is exists`); } - clients.set(o.name, new Redis(o)); + if (o.url) { + clients.set(o.name, new Redis(o.url)); + } else { + clients.set(o.name, new Redis(o)); + } } else { if (clients.has(defaultKey)) { throw new RedisClientError('default client is exists'); } - clients.set(defaultKey, new Redis(o)); + if (o.url) { + clients.set(defaultKey, new Redis(o.url)); + } else { + clients.set(defaultKey, new Redis(o)); + } } } } else { - clients.set(defaultKey, new Redis(options)); + if (options.url) { + clients.set(defaultKey, new Redis(options.url)); + } else { + clients.set(defaultKey, new Redis(options)); + } } return { defaultKey, diff --git a/lib/redis.interface.ts b/lib/redis.interface.ts index 18177fc..15393ab 100644 --- a/lib/redis.interface.ts +++ b/lib/redis.interface.ts @@ -2,7 +2,8 @@ import { ModuleMetadata } from '@nestjs/common/interfaces'; import { RedisOptions } from 'ioredis'; export interface RedisModuleOptions extends RedisOptions { - name?: string; + name?: string + url?: string } export interface RedisModuleAsyncOptions