Skip to content

Commit

Permalink
#17 add url for redisOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
skunight committed Jul 23, 2019
1 parent 53eda8f commit 6faeb43
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ export default {
password: process.env.REDIS_PASSWORD,
keyPrefix: process.env.REDIS_PRIFIX,
}
Or
export default {
url: 'redis://:[email protected]: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://:[email protected]:6380/4',
},
{
name:'test2',
Expand Down Expand Up @@ -101,6 +101,7 @@ interface RedisOptions {
* client name. default is a uuid, unique.
*/
name?: string;
url?: string;
port?: number;
host?: string;
/**
Expand Down
21 changes: 18 additions & 3 deletions dist/redis-client.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions dist/redis.interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>;
Expand Down
18 changes: 15 additions & 3 deletions lib/redis-client.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lib/redis.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6faeb43

Please sign in to comment.