Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update #551

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 10 additions & 41 deletions docs/latest/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,24 @@ export class AppModule {}

**Now**, we can use redis in two ways.

via decorator:

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

@Injectable()
export class AppService {
constructor(
@InjectRedis() private readonly redis: Redis // or // @InjectRedis(DEFAULT_REDIS_NAMESPACE) private readonly redis: Redis
) {}

async set() {
return await this.redis.set('key', 'value', 'EX', 10);
}
}
```

via service:

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

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

constructor(private readonly redisService: RedisService) {
this.redis = this.redisService.getClient();
this.redis = this.redisService.getOrThrow();
// same as
// this.redis = this.redisService.getOrThrow(DEFAULT_REDIS);

// or
// this.redis = this.redisService.getClient(DEFAULT_REDIS_NAMESPACE);
// this.redis = this.redisService.getOrNil();
// same as
// this.redis = this.redisService.getOrNil(DEFAULT_REDIS);
}

async set() {
Expand Down Expand Up @@ -106,7 +90,7 @@ export class AppModule {}
| ------------- | ---------------------------------------------------------------------- | ----------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| closeClient | `boolean` | `true` | `false` | If set to `true`, all clients will be closed automatically on nestjs application shutdown. To use `closeClient`, you **must enable listeners** by calling `app.enableShutdownHooks()`. [Read more about the application shutdown.](https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown) |
| commonOptions | [RedisOptions](https://luin.github.io/ioredis/index.html#RedisOptions) | `undefined` | `false` | Common options to be passed to each client. |
| readyLog | `boolean` | `false` | `false` | If set to `true`, then ready logging will be displayed when the client is ready. |
| readyLog | `boolean` | `true` | `false` | If set to `true`, then ready logging will be displayed when the client is ready. |
| errorLog | `boolean` | `true` | `false` | If set to `true`, then errors that occurred while connecting will be displayed by the built-in logger. |
| config | `RedisClientOptions` \| `RedisClientOptions`[] | `undefined` | `false` | Used to specify single or multiple clients. |

Expand Down Expand Up @@ -497,18 +481,3 @@ export class AppModule {}
```

And there we go.

### Testing

This package exposes `getRedisToken()` function that returns an internal injection token based on the provided context. Using this token, you can provide a mock implementation of the redis instance using any of the standard custom provider techniques, including `useClass`, `useValue`, and `useFactory`.

```ts
import { Test, TestingModule } from '@nestjs/testing';
import { getRedisToken } from '@liaoliaots/nestjs-redis';

const module: TestingModule = await Test.createTestingModule({
providers: [{ provide: getRedisToken('namespace'), useValue: mockedInstance }, YourService]
}).compile();
```

A working example is available [here](/sample).
Loading