Skip to content

Commit

Permalink
feat: add aliases: RedisService, ClusterService (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliaots committed Dec 9, 2021
1 parent 281b483 commit f23fd45
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- Support health check
- Support multiple clients
- Inject a redis/cluster client via `@InjectRedis()` and `@InjectCluster()`
- Get a redis/cluster client via `RedisManager` and `ClusterManager`
- Get a redis/cluster client via `RedisService` and `ClusterService`

## Documentation

Expand Down
22 changes: 11 additions & 11 deletions docs/latest/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class AppModule {}

> HINT: The `ClusterModule` is a [global module](https://docs.nestjs.com/modules#global-modules). Once defined, this module is available everywhere.
**Now** we can use cluster in two ways.
**Now**, we can use cluster in two ways.

via decorator:

Expand All @@ -36,36 +36,36 @@ import { Cluster } from 'ioredis';
@Injectable()
export class AppService {
constructor(
@InjectCluster() private readonly defaultClusterClient: Cluster
@InjectCluster() private readonly cluster: Cluster
// or
// @InjectCluster(DEFAULT_CLUSTER_NAMESPACE) private readonly defaultClusterClient: Cluster
// @InjectCluster(DEFAULT_CLUSTER_NAMESPACE) private readonly cluster: Cluster
) {}

async ping(): Promise<string> {
return await this.defaultClusterClient.ping();
return await this.cluster.ping();
}
}
```

via manager:
via service:

```TypeScript
import { Injectable } from '@nestjs/common';
import { ClusterManager, DEFAULT_CLUSTER_NAMESPACE } from '@liaoliaots/nestjs-redis';
import { ClusterService, DEFAULT_CLUSTER_NAMESPACE } from '@liaoliaots/nestjs-redis';
import { Cluster } from 'ioredis';

@Injectable()
export class AppService {
private readonly defaultClusterClient: Cluster;
private readonly cluster: Cluster;

constructor(private readonly clusterManager: ClusterManager) {
this.defaultClusterClient = this.clusterManager.getClient();
constructor(private readonly clusterService: ClusterService) {
this.cluster = this.clusterService.getClient();
// or
// this.defaultClusterClient = this.clusterManager.getClient(DEFAULT_CLUSTER_NAMESPACE);
// this.cluster = this.clusterService.getClient(DEFAULT_CLUSTER_NAMESPACE);
}

async ping(): Promise<string> {
return await this.defaultClusterClient.ping();
return await this.cluster.ping();
}
}
```
Expand Down
39 changes: 16 additions & 23 deletions docs/latest/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class AppModule {}

> HINT: The `RedisModule` is a [global module](https://docs.nestjs.com/modules#global-modules). Once defined, this module is available everywhere.
**Now** we can use redis in two ways.
**Now**, we can use redis in two ways.

via decorator:

Expand All @@ -37,36 +37,36 @@ import { Redis } from 'ioredis';
@Injectable()
export class AppService {
constructor(
@InjectRedis() private readonly defaultRedisClient: Redis
@InjectRedis() private readonly redis: Redis
// or
// @InjectRedis(DEFAULT_REDIS_NAMESPACE) private readonly defaultRedisClient: Redis
// @InjectRedis(DEFAULT_REDIS_NAMESPACE) private readonly redis: Redis
) {}

async ping(): Promise<string> {
return await this.defaultRedisClient.ping();
return await this.redis.ping();
}
}
```

via manager:
via service:

```TypeScript
import { Injectable } from '@nestjs/common';
import { RedisManager, DEFAULT_REDIS_NAMESPACE } from '@liaoliaots/nestjs-redis';
import { RedisService, DEFAULT_REDIS_NAMESPACE } from '@liaoliaots/nestjs-redis';
import { Redis } from 'ioredis';

@Injectable()
export class AppService {
private readonly defaultRedisClient: Redis;
private readonly redis: Redis;

constructor(private readonly redisManager: RedisManager) {
this.defaultRedisClient = this.redisManager.getClient();
constructor(private readonly redisService: RedisService) {
this.redis = this.redisService.getClient();
// or
// this.defaultRedisClient = this.redisManager.getClient(DEFAULT_REDIS_NAMESPACE);
// this.redis = this.redisService.getClient(DEFAULT_REDIS_NAMESPACE);
}

async ping(): Promise<string> {
return await this.defaultRedisClient.ping();
return await this.redis.ping();
}
}
```
Expand All @@ -79,9 +79,8 @@ For example, use with `@nestjs/throttler` and `nestjs-throttler-storage-redis`:

```TypeScript
import { Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { RedisModule, RedisManager } from '@liaoliaots/nestjs-redis';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { RedisModule, RedisService } from '@liaoliaots/nestjs-redis';
import { ThrottlerModule } from '@nestjs/throttler';
import { ThrottlerStorageRedisService } from 'nestjs-throttler-storage-redis';

@Module({
Expand All @@ -97,18 +96,12 @@ import { ThrottlerStorageRedisService } from 'nestjs-throttler-storage-redis';
}
}),
ThrottlerModule.forRootAsync({
useFactory(redisManager: RedisManager) {
const redis = redisManager.getClient('default');
useFactory(redisService: RedisService) {
const redis = redisService.getClient('default');
return { ttl: 60, limit: 10, storage: new ThrottlerStorageRedisService(redis) };
},
inject: [RedisManager]
inject: [RedisService]
})
],
providers: [
{
provide: APP_GUARD,
useClass: ThrottlerGuard
}
]
})
export class AppModule {}
Expand Down
4 changes: 2 additions & 2 deletions lib/cluster/common/cluster.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const displayReadyLog = (clients: ClusterClients): void => {
});
};

export const quitClients = async (
export const quitClients = (
clients: ClusterClients
): Promise<[PromiseResult<ClientNamespace>, PromiseResult<'OK'>][]> => {
const promises: Promise<[PromiseResult<ClientNamespace>, PromiseResult<'OK'>]>[] = [];
Expand All @@ -36,5 +36,5 @@ export const quitClients = async (
client.disconnect();
});

return await Promise.all(promises);
return Promise.all(promises);
};
2 changes: 1 addition & 1 deletion lib/cluster/common/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as allExports from '.';

test('should have 6 exports', () => {
test('there should be 6 exports', () => {
expect(Object.keys(allExports)).toHaveLength(6);
});

Expand Down
2 changes: 1 addition & 1 deletion lib/health/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as allExports from '.';

test('should have 2 exports', () => {
test('there should be 2 exports', () => {
expect(Object.keys(allExports)).toHaveLength(2);
});

Expand Down
4 changes: 2 additions & 2 deletions lib/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as allExports from '.';

test('should have 12 exports', () => {
expect(Object.keys(allExports)).toHaveLength(12);
test('there should be 14 exports', () => {
expect(Object.keys(allExports)).toHaveLength(14);
});

test('each of exports should be defined', () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export { RedisModule } from './redis/redis.module';
export { DEFAULT_REDIS_NAMESPACE, RedisStatus } from './redis/redis.constants';
export { RedisManager } from './redis/redis-manager';
export { RedisManager, RedisManager as RedisService } from './redis/redis-manager';
export { InjectRedis, getRedisToken } from './redis/common';
export { ClusterModule } from './cluster/cluster.module';
export { DEFAULT_CLUSTER_NAMESPACE, ClusterStatus } from './cluster/cluster.constants';
export { ClusterManager } from './cluster/cluster-manager';
export { ClusterManager, ClusterManager as ClusterService } from './cluster/cluster-manager';
export { InjectCluster, getClusterToken } from './cluster/common';

// * Types & Interfaces
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/common/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as allExports from '.';

test('should have 6 exports', () => {
test('there should be 6 exports', () => {
expect(Object.keys(allExports)).toHaveLength(6);
});

Expand Down
4 changes: 2 additions & 2 deletions lib/redis/common/redis.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const displayReadyLog = (clients: RedisClients): void => {
});
};

export const quitClients = async (
export const quitClients = (
clients: RedisClients
): Promise<[PromiseResult<ClientNamespace>, PromiseResult<'OK'>][]> => {
const promises: Promise<[PromiseResult<ClientNamespace>, PromiseResult<'OK'>]>[] = [];
Expand All @@ -36,5 +36,5 @@ export const quitClients = async (
client.disconnect();
});

return await Promise.all(promises);
return Promise.all(promises);
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@liaoliaots/nestjs-redis",
"version": "5.1.1",
"version": "5.2.0",
"description": "Redis(ioredis) module for NestJS framework",
"author": "LiaoLiao",
"main": "dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions test/cluster/src/controllers/manager.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Controller, Get } from '@nestjs/common';
import { ClusterManager } from '@/.';
import { ClusterService } from '@/.';

@Controller('manager')
export class ManagerController {
constructor(private readonly clusterManager: ClusterManager) {}
constructor(private readonly clusterManager: ClusterService) {}

@Get()
async pingDefault(): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions test/redis/src/controllers/manager.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Controller, Get } from '@nestjs/common';
import { RedisManager } from '@/.';
import { RedisService } from '@/.';

@Controller('manager')
export class ManagerController {
constructor(private readonly redisManager: RedisManager) {}
constructor(private readonly redisManager: RedisService) {}

@Get()
async pingDefault(): Promise<string> {
Expand Down

0 comments on commit f23fd45

Please sign in to comment.