Skip to content

Commit

Permalink
feat!: default redis client is no longer created by default (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliaots committed Oct 15, 2021
1 parent dd5edd6 commit 0a9da80
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 73 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ _For the legacy V2 or V3@next documentation, [click here](docs/v2/README.md)._
- [Health Checks](docs/v3/health-checks.md)
- [Examples](docs/v3/examples.md)
- [Redis](docs/v3/examples.md#redis)
- [Default](docs/v3/examples.md#default)
- [Sentinel](docs/v3/examples.md#sentinel)
- [Cluster](docs/v3/examples.md#cluster)
- [Multiple Clients](docs/v3/examples.md#multiple-clients)
Expand Down
50 changes: 0 additions & 50 deletions docs/v3/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,6 @@

## Redis

### Default

If the redis server does **not** have a password, the host is **127.0.0.1** and the port is **6379**:

```TypeScript
import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';

@Module({
imports: [RedisModule.forRoot()]
})
export class AppModule {}
```

... or

```TypeScript
import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';

@Module({
imports: [RedisModule.forRoot({ closeClient: true })]
})
export class AppModule {}
```

... or

```TypeScript
import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';

@Module({
imports: [RedisModule.forRoot({ closeClient: true, config: { namespace: 'default' } })]
})
export class AppModule {}
```

... or

```TypeScript
import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';

@Module({
imports: [RedisModule.forRoot({ config: { host: '127.0.0.1', port: 6379 } })]
})
export class AppModule {}
```

### Sentinel

| name | ip | port | password |
Expand Down
12 changes: 6 additions & 6 deletions docs/v3/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ export class AppModule {}

### RedisModuleOptions

| Name | Type | Default value | Description |
| ------------- | ------------------------------------ | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| closeClient | boolean | false | If `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 | object | undefined | The common options for each client. |
| readyLog | boolean | false | If `true`, will show a message when the client is ready. |
| config | `ClientOptions` or `ClientOptions`[] | { host: 'localhost', port: 6379 } | Specify single or multiple clients. |
| Name | Type | Default value | Description |
| ------------- | ------------------------------------ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| closeClient | boolean | false | If `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 | object | undefined | The common options for each client. |
| readyLog | boolean | false | If `true`, will show a message when the client is ready. |
| config | `ClientOptions` or `ClientOptions`[] | undefined | Specify single or multiple clients. |

### ClientOptions

Expand Down
16 changes: 16 additions & 0 deletions lib/cluster/cluster.providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ describe('clusterClientsProvider', () => {
});
});

describe('without options', () => {
let clients: ClusterClients;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [{ provide: CLUSTER_OPTIONS, useValue: {} }, clusterClientsProvider, ClusterManager]
}).compile();

clients = module.get<ClusterClients>(CLUSTER_CLIENTS);
});

test('should have 1 member', () => {
expect(clients.size).toBe(0);
});
});

describe('displayReadyLog', () => {
beforeEach(() => {
(displayReadyLog as jest.MockedFunction<typeof displayReadyLog>).mockReset();
Expand Down
2 changes: 1 addition & 1 deletion lib/cluster/cluster.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const clusterClientsProvider: FactoryProvider<ClusterClients> = {
options.config.forEach(item =>
clients.set(item.namespace ?? DEFAULT_CLUSTER_NAMESPACE, createClient(item))
);
} /* single */ else {
} else if (options.config /* single */) {
clients.set(options.config.namespace ?? DEFAULT_CLUSTER_NAMESPACE, createClient(options.config));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/redis/default-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('defaultRedisModuleOptions', () => {
test('should validate the defaultRedisModuleOptions', () => {
expect(defaultRedisModuleOptions.closeClient).toBe(false);
expect(defaultRedisModuleOptions.readyLog).toBe(false);
expect(defaultRedisModuleOptions.config).toEqual({ host: 'localhost', port: 6379 });
expect(defaultRedisModuleOptions.config).toBeUndefined();
expect(defaultRedisModuleOptions.commonOptions).toBeUndefined();
});
});
3 changes: 1 addition & 2 deletions lib/redis/default-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { RedisModuleOptions } from './interfaces';
export const defaultRedisModuleOptions: RedisModuleOptions = {
closeClient: false,
readyLog: false,
// ! https://github.com/luin/ioredis/blob/master/lib/redis/RedisOptions.ts#L35
config: { host: 'localhost', port: 6379 },
config: undefined,
commonOptions: undefined
};
9 changes: 1 addition & 8 deletions lib/redis/redis.providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,17 @@ describe('redisClientsProvider', () => {

describe('without options', () => {
let clients: RedisClients;
let manager: RedisManager;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [{ provide: REDIS_OPTIONS, useValue: {} }, redisClientsProvider, RedisManager]
}).compile();

clients = module.get<RedisClients>(REDIS_CLIENTS);
manager = module.get<RedisManager>(RedisManager);
});

test('should have 1 member', () => {
expect(clients.size).toBe(1);
});

test('should work correctly', () => {
const client = manager.getClient(DEFAULT_REDIS_NAMESPACE);
expect(client).toBeInstanceOf(IORedis);
expect(clients.size).toBe(0);
});
});

Expand Down
2 changes: 1 addition & 1 deletion lib/redis/redis.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const redisClientsProvider: FactoryProvider<RedisClients> = {
);
} else if (options.config /* single */) {
clients.set(options.config.namespace ?? DEFAULT_REDIS_NAMESPACE, createClient(options.config));
} else clients.set(DEFAULT_REDIS_NAMESPACE, createClient({}));
}

if (options.readyLog) displayReadyLog(clients);

Expand Down
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": "4.1.3",
"version": "5.0.0",
"description": "Redis(ioredis) module for NestJS framework",
"author": "LiaoLiao",
"main": "dist/index.js",
Expand Down

0 comments on commit 0a9da80

Please sign in to comment.