diff --git a/README.md b/README.md index 1bef9caf..c4325d09 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/docs/v3/examples.md b/docs/v3/examples.md index e3a0c46b..ba5784d6 100644 --- a/docs/v3/examples.md +++ b/docs/v3/examples.md @@ -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 | diff --git a/docs/v3/redis.md b/docs/v3/redis.md index 0eac082f..0ad93fa0 100644 --- a/docs/v3/redis.md +++ b/docs/v3/redis.md @@ -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 diff --git a/lib/cluster/cluster.providers.spec.ts b/lib/cluster/cluster.providers.spec.ts index de452ab3..a6067091 100644 --- a/lib/cluster/cluster.providers.spec.ts +++ b/lib/cluster/cluster.providers.spec.ts @@ -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(CLUSTER_CLIENTS); + }); + + test('should have 1 member', () => { + expect(clients.size).toBe(0); + }); + }); + describe('displayReadyLog', () => { beforeEach(() => { (displayReadyLog as jest.MockedFunction).mockReset(); diff --git a/lib/cluster/cluster.providers.ts b/lib/cluster/cluster.providers.ts index bbc094dc..534f4c79 100644 --- a/lib/cluster/cluster.providers.ts +++ b/lib/cluster/cluster.providers.ts @@ -90,7 +90,7 @@ export const clusterClientsProvider: FactoryProvider = { 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)); } diff --git a/lib/redis/default-options.spec.ts b/lib/redis/default-options.spec.ts index 3bba7ea3..3db87a15 100644 --- a/lib/redis/default-options.spec.ts +++ b/lib/redis/default-options.spec.ts @@ -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(); }); }); diff --git a/lib/redis/default-options.ts b/lib/redis/default-options.ts index d4b61b75..c3bdd7e7 100644 --- a/lib/redis/default-options.ts +++ b/lib/redis/default-options.ts @@ -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 }; diff --git a/lib/redis/redis.providers.spec.ts b/lib/redis/redis.providers.spec.ts index 112da42d..dcdbd6cb 100644 --- a/lib/redis/redis.providers.spec.ts +++ b/lib/redis/redis.providers.spec.ts @@ -215,7 +215,6 @@ describe('redisClientsProvider', () => { describe('without options', () => { let clients: RedisClients; - let manager: RedisManager; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -223,16 +222,10 @@ describe('redisClientsProvider', () => { }).compile(); clients = module.get(REDIS_CLIENTS); - manager = module.get(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); }); }); diff --git a/lib/redis/redis.providers.ts b/lib/redis/redis.providers.ts index 98157f54..802d42ec 100644 --- a/lib/redis/redis.providers.ts +++ b/lib/redis/redis.providers.ts @@ -90,7 +90,7 @@ export const redisClientsProvider: FactoryProvider = { ); } 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); diff --git a/package-lock.json b/package-lock.json index 1f9ed46b..06b9f7d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@liaoliaots/nestjs-redis", - "version": "4.1.3", + "version": "5.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@liaoliaots/nestjs-redis", - "version": "4.1.3", + "version": "5.0.0", "license": "MIT", "dependencies": { "redis-errors": "1.2.0", diff --git a/package.json b/package.json index a80eb05d..c873ce73 100644 --- a/package.json +++ b/package.json @@ -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",