Skip to content

Commit

Permalink
feat!: flat options in ClusterClientOptions (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliaots committed Mar 11, 2022
1 parent 15e3402 commit f60ecb0
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 34 deletions.
6 changes: 3 additions & 3 deletions lib/cluster/common/cluster.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe('createClient', () => {
test('should create a client with options', () => {
const options: ClusterClientOptions = {
nodes: [{ host: '127.0.0.1', port: 16380 }],
options: { redisOptions: { password: '' } }
redisOptions: { password: '' }
};
const client = createClient(options);
expect(MockCluster).toHaveBeenCalledTimes(1);
expect(MockCluster).toHaveBeenCalledWith(options.nodes, options.options);
expect(MockCluster).toHaveBeenCalledWith(options.nodes, { redisOptions: { password: '' } });
expect(client).toBeInstanceOf(IORedis.Cluster);
});

Expand All @@ -25,7 +25,7 @@ describe('createClient', () => {

const client = createClient({ nodes: [], onClientCreated: mockOnClientCreated });
expect(MockCluster).toHaveBeenCalledTimes(1);
expect(MockCluster).toHaveBeenCalledWith([], undefined);
expect(MockCluster).toHaveBeenCalledWith([], {});
expect(client).toBeInstanceOf(IORedis.Cluster);
expect(mockOnClientCreated).toHaveBeenCalledTimes(1);
expect(mockOnClientCreated).toHaveBeenCalledWith(client);
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 @@ -8,9 +8,9 @@ import { CONNECTED_SUCCESSFULLY } from '@/messages';
import { logger } from '../cluster-logger';

export const createClient = (clientOptions: ClusterClientOptions): Cluster => {
const { nodes, options, onClientCreated } = clientOptions;
const { nodes, onClientCreated, ...clusterOptions } = clientOptions;

const client = new IORedis.Cluster(nodes, options);
const client = new IORedis.Cluster(nodes, clusterOptions);
if (onClientCreated) onClientCreated(client);

return client;
Expand Down
23 changes: 11 additions & 12 deletions lib/cluster/interfaces/cluster-module-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import { Type, ModuleMetadata, Provider } from '@nestjs/common';
import { ClusterNode, ClusterOptions, Cluster } from 'ioredis';
import { ClientNamespace } from '@/interfaces';

export interface ClusterClientOptions {
export interface ClusterClientOptions extends ClusterOptions {
/**
* The name of the client, and must be unique.
* Client name. If client name is not given then it will be called "default".
* Different clients must have different names.
*/
namespace?: ClientNamespace;

/**
* A list of nodes of the cluster.
* List of cluster nodes.
*/
nodes: ClusterNode[];

/**
* The options of the cluster.
*/
options?: ClusterOptions;
/**
* This function will be executed as soon as the client is created.
* Function to be executed as soon as the client is created.
*
* @param client - The new client
*/
Expand All @@ -26,16 +25,16 @@ export interface ClusterClientOptions {
export interface ClusterModuleOptions {
/**
* If `true`, all clients will be closed automatically on nestjs application shutdown.
*
* Default: false
* Default value is true.
*/
closeClient?: boolean;

/**
* If `true`, will show a message when the client is ready.
*
* Default: false
* Default value is false.
*/
readyLog?: boolean;

/**
* Specify single or multiple clients.
*/
Expand Down
11 changes: 7 additions & 4 deletions lib/health/indicators/redis-check-settings.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ import { Redis, Cluster } from 'ioredis';
export type RedisCheckSettings =
| {
/**
* Server type. You must specify what Redis server type you use. Possible values are `"redis"`, `"cluster"`. This option is `required`.
* Server type. You must specify what Redis server type you use. Possible values are "redis", "cluster". This option is required.
*/
type: 'redis';

/**
* The client which the health check should get executed.
*/
client: Redis;

/**
* The amount of time the check should require in ms.
*
* Default: `1000`
* Default value is 1000 which is equivalent to 1 second.
*/
timeout?: number;

/**
* The maximum amount of memory that the Redis server expects to use in bytes.
*/
memoryThreshold?: number;
}
| {
/**
* Server type. You must specify what Redis server type you use. Possible values are `"redis"`, `"cluster"`. This option is `required`.
* Server type. You must specify what Redis server type you use. Possible values are "redis", "cluster". This option is required.
*/
type: 'cluster';

/**
* The client which the health check should get executed.
*/
Expand Down
20 changes: 12 additions & 8 deletions lib/redis/interfaces/redis-module-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { ClientNamespace } from '@/interfaces';

export interface RedisClientOptions extends RedisOptions {
/**
* The name of the client, and must be unique.
* Client name. If client name is not given then it will be called "default".
* Different clients must have different names.
*/
namespace?: ClientNamespace;

/**
* The URL specifies connection options.
* URL used to specify connection options.
*
* - redis:// https://www.iana.org/assignments/uri-schemes/prov/redis
* - rediss:// https://www.iana.org/assignments/uri-schemes/prov/rediss
*/
url?: string;

/**
* This function will be executed as soon as the client is created.
* Function to be executed as soon as the client is created.
*
* @param client - The new client
*/
Expand All @@ -25,20 +28,21 @@ export interface RedisClientOptions extends RedisOptions {
export interface RedisModuleOptions {
/**
* If `true`, all clients will be closed automatically on nestjs application shutdown.
*
* Default: false
* Default value is true.
*/
closeClient?: boolean;

/**
* The common options for each client.
* Common options for each client.
*/
commonOptions?: RedisOptions;

/**
* If `true`, will show a message when the client is ready.
*
* Default: false
* Default value is false.
*/
readyLog?: boolean;

/**
* Specify single or multiple 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": "7.0.0-rc.1",
"version": "7.0.0-alpha.3",
"description": "Redis(ioredis) module for NestJS framework",
"author": "LiaoLiao",
"main": "dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions test/cluster/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { ManagerController } from './controllers/manager.controller';
config: [
{
nodes: [{ host: '127.0.0.1', port: 16380 }],
options: { redisOptions: { password: 'cluster1' } }
redisOptions: { password: 'cluster1' }
},
{
namespace: 'client1',
nodes: [{ host: '127.0.0.1', port: 16480 }],
options: { redisOptions: { password: 'cluster2' } }
redisOptions: { password: 'cluster2' }
}
]
};
Expand Down

0 comments on commit f60ecb0

Please sign in to comment.