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

Useparagon cluster support #4

Merged
merged 9 commits into from
Nov 6, 2020
Prev Previous commit
Next Next commit
chagned "cluster" type back to "Redis" to prevent typescript errors
ishmaelthedestroyer committed Jul 2, 2020
commit 735df4ce4e9b80b929230990765fe3269400f674
4 changes: 2 additions & 2 deletions dist/cluster.interface.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { Cluster, ClusterOptions } from 'ioredis';
import { Redis, ClusterOptions } from 'ioredis';
export interface ClusterModuleOptions extends ClusterOptions {
name?: string;
nodes: (string | number | object)[];
onClusterReady?(cluster: Cluster): Promise<void>;
onClusterReady?(cluster: Redis): Promise<void>;
}
export interface ClusterModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => ClusterModuleOptions | ClusterModuleOptions[] | Promise<ClusterModuleOptions> | Promise<ClusterModuleOptions[]>;
4 changes: 2 additions & 2 deletions dist/cluster.provider.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Cluster } from 'ioredis';
import { Redis } from 'ioredis';
import { Provider } from '@nestjs/common';
import { ClusterModuleAsyncOptions, ClusterModuleOptions } from './cluster.interface';
export declare class RedisClusterError extends Error {
}
export interface ClusterProvider {
defaultKey: string;
clusters: Map<string, Cluster>;
clusters: Map<string, Redis>;
size: number;
}
export declare const createCluster: () => Provider;
6 changes: 3 additions & 3 deletions dist/cluster.service.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Cluster } from 'ioredis';
import { Redis } from 'ioredis';
import { ClusterProvider } from './cluster.provider';
export declare class ClusterService {
private readonly provider;
constructor(provider: ClusterProvider);
getCluster(name?: string): Cluster;
getClusters(): Map<string, Cluster>;
getCluster(name?: string): Redis;
getClusters(): Map<string, Redis>;
}
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -3,4 +3,4 @@ export * from './redis.module';
export * from './redis.interface';
export * from './cluster.service';
export * from './cluster.module';
export * from './redis.interface';
export * from './cluster.interface';
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
@@ -15,4 +15,4 @@ __exportStar(require("./redis.module"), exports);
__exportStar(require("./redis.interface"), exports);
__exportStar(require("./cluster.service"), exports);
__exportStar(require("./cluster.module"), exports);
__exportStar(require("./redis.interface"), exports);
__exportStar(require("./cluster.interface"), exports);
4 changes: 2 additions & 2 deletions lib/cluster-core.module.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import {
OnModuleDestroy,
} from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { Cluster } from 'ioredis';
import { Redis } from 'ioredis';
import {
ClusterModuleAsyncOptions,
ClusterModuleOptions,
@@ -60,7 +60,7 @@ export class ClusterCoreModule implements OnModuleDestroy {
defaultKey,
}: ClusterProvider) => options => {
const name = options.name || defaultKey;
const cluster: Cluster = clusters.get(name);
const cluster: Redis = clusters.get(name);

if (cluster && !options.keepAlive) {
cluster.disconnect();
4 changes: 2 additions & 2 deletions lib/cluster.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { Cluster, ClusterOptions } from 'ioredis';
import { Redis, ClusterOptions } from 'ioredis';

export interface ClusterModuleOptions extends ClusterOptions {
name?: string;
nodes: (string | number | object)[];
onClusterReady?(cluster: Cluster): Promise<void>;
onClusterReady?(cluster: Redis): Promise<void>;
}

export interface ClusterModuleAsyncOptions
10 changes: 5 additions & 5 deletions lib/cluster.provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cluster } from 'ioredis';
import { Cluster, Redis } from 'ioredis';
import * as uuid from 'uuid';
import { Provider } from '@nestjs/common';

@@ -11,13 +11,13 @@ import {
export class RedisClusterError extends Error {}
export interface ClusterProvider {
defaultKey: string;
clusters: Map<string, Cluster>;
clusters: Map<string, Redis>;
size: number;
}

async function getCluster(options: ClusterModuleOptions): Promise<Cluster> {
async function getCluster(options: ClusterModuleOptions): Promise<Redis> {
const { onClusterReady, nodes, ...opt } = options;
const cluster: Cluster = (new Cluster(nodes, opt) as unknown) as Cluster;
const cluster: Redis = new Cluster(nodes, opt);

if (onClusterReady) {
onClusterReady(cluster);
@@ -31,7 +31,7 @@ export const createCluster = (): Provider => ({
useFactory: async (
options: ClusterModuleOptions | ClusterModuleOptions[],
): Promise<ClusterProvider> => {
const clusters: Map<string, Cluster> = new Map<string, Cluster>();
const clusters: Map<string, Redis> = new Map<string, Redis>();
let defaultKey = uuid();

if (Array.isArray(options)) {
6 changes: 3 additions & 3 deletions lib/cluster.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Inject } from '@nestjs/common';
import { REDIS_CLUSTER } from './cluster.constants';
import { Cluster } from 'ioredis';
import { Redis } from 'ioredis';
import { ClusterProvider, RedisClusterError } from './cluster.provider';

@Injectable()
@@ -9,7 +9,7 @@ export class ClusterService {
@Inject(REDIS_CLUSTER) private readonly provider: ClusterProvider,
) {}

getCluster(name?: string): Cluster {
getCluster(name?: string): Redis {
if (!name) {
name = this.provider.defaultKey;
}
@@ -20,7 +20,7 @@ export class ClusterService {
return this.provider.clusters.get(name);
}

getClusters(): Map<string, Cluster> {
getClusters(): Map<string, Redis> {
return this.provider.clusters;
}
}