Skip to content

Commit

Permalink
change custom error handler to custom event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Polyakov committed Mar 12, 2020
1 parent 2ed76a9 commit a8d7ad2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ With custom error handler
```typescript
export default {
url: 'redis://:[email protected]:6380/4',
errorHandler: (err, client) => {},
onClientReady: (client) => {
client.on('error', (err) => {}
)},
}
```
With multi client
Expand Down
33 changes: 17 additions & 16 deletions lib/redis-client.provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Redis from 'ioredis';
import * as uuid from 'uuid';
import { Provider } from '@nestjs/common';

import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from './redis.constants';
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface';
Expand All @@ -11,33 +12,33 @@ export interface RedisClient {
size: number;
}

function getClient(options: RedisModuleOptions, key: string): Redis.Redis {
const { errorHandler, url, ...opt } = options;

async function getClient(options: RedisModuleOptions): Promise<Redis.Redis> {
const { onClientReady, url, ...opt } = options;
const client = url ? new Redis(url) : new Redis(opt);
if (errorHandler) {
client.on('error', err => errorHandler(err, client));
if (onClientReady) {
onClientReady(client)
}

return client;
}

export const createClient = () => ({
export const createClient = (): Provider => ({
provide: REDIS_CLIENT,
useFactory: (options: RedisModuleOptions | RedisModuleOptions[]) => {
useFactory: async (options: RedisModuleOptions | RedisModuleOptions[]): Promise<RedisClient> => {
const clients = new Map<string, Redis.Redis>();
const defaultKey = uuid();

if (Array.isArray(options)) {
for (const o of options) {
const key = o.name || defaultKey;
if (clients.has(key)) {
throw new RedisClientError(`client ${o.name} or default client is exists`);
}
clients.set(key, getClient(o, key));
}
await Promise.all(
options.map(async o => {
const key = o.name || defaultKey;
if (clients.has(key)) {
throw new RedisClientError(`${o.name || 'default'} client is exists`);
}
clients.set(key, await getClient(o));
}),
);
} else {
clients.set(defaultKey, getClient(options, defaultKey));
clients.set(defaultKey, await getClient(options));
}

return {
Expand Down
8 changes: 3 additions & 5 deletions lib/redis.interface.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';
import * as IORedis from 'ioredis';
import { Redis, RedisOptions } from 'ioredis';

export interface RedisModuleOptions extends RedisOptions {
name?: string;
url?: string;
errorHandler?(err, client: IORedis.Redis): void;
onClientReady?(client: Redis): Promise<void>;
}

export interface RedisModuleAsyncOptions
extends Pick<ModuleMetadata, 'imports'> {
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (
...args: any[]
) =>
Expand Down

0 comments on commit a8d7ad2

Please sign in to comment.