Skip to content

Commit

Permalink
feat(redis-client.provider.ts): add multi client
Browse files Browse the repository at this point in the history
service can can use multi client
  • Loading branch information
skunight committed Dec 22, 2018
1 parent 0626744 commit b71806e
Show file tree
Hide file tree
Showing 13 changed files with 3,311 additions and 120 deletions.
15 changes: 13 additions & 2 deletions dist/redis-client.provider.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import * as Redis from "ioredis";
import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface";
export declare class RedisClientError extends Error {
}
export interface RedisClient {
defaultKey: string;
clients: Map<string, Redis.Redis>;
size: number;
}
export declare const createClient: () => {
provide: symbol;
useFactory: (options: RedisModuleOptions) => Redis.Redis;
useFactory: (options: RedisModuleOptions | RedisModuleOptions[]) => {
defaultKey: string;
clients: Map<string, Redis.Redis>;
size: number;
};
inject: symbol[];
};
export declare const createAsyncClientOptions: (options: RedisModuleAsyncOptions) => {
provide: symbol;
useFactory: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>;
useFactory: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions> | RedisModuleOptions[] | Promise<RedisModuleOptions[]>;
inject: any[];
};
29 changes: 28 additions & 1 deletion dist/redis-client.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,37 @@
Object.defineProperty(exports, "__esModule", { value: true });
const Redis = require("ioredis");
const redis_constants_1 = require("./redis.constants");
const uuid = require("uuid");
class RedisClientError extends Error {
}
exports.RedisClientError = RedisClientError;
exports.createClient = () => ({
provide: redis_constants_1.REDIS_CLIENT,
useFactory: (options) => {
return new Redis(options);
const clients = new Map();
const defaultKey = uuid();
if (Array.isArray(options)) {
for (let o of options) {
if (o.name) {
if (clients.has(o.name)) {
throw new RedisClientError(`client ${o.name} is exists`);
}
clients.set(o.name, new Redis(o));
}
else {
if (clients.has(defaultKey)) {
throw new RedisClientError('default client is exists');
}
clients.set(defaultKey, new Redis(o));
}
}
}
else {
clients.set(defaultKey, new Redis(options));
}
return {
defaultKey, clients, size: clients.size
};
},
inject: [redis_constants_1.REDIS_MODULE_OPTIONS]
});
Expand Down
3 changes: 2 additions & 1 deletion dist/redis.interface.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';
export interface RedisModuleOptions extends RedisOptions {
name?: string;
}
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>;
useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>;
inject?: any[];
}
2 changes: 1 addition & 1 deletion dist/redis.module.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DynamicModule } from '@nestjs/common';
import { RedisModuleOptions, RedisModuleAsyncOptions } from './redis.interface';
export declare class RedisModule {
static register(options: RedisModuleOptions): DynamicModule;
static register(options: RedisModuleOptions | RedisModuleOptions[]): DynamicModule;
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule;
}
7 changes: 4 additions & 3 deletions dist/redis.service.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Redis from "ioredis";
import { RedisClient } from "./redis-client.provider";
export declare class RedisService {
private readonly client;
constructor(client: Redis.Redis);
getClient(): Redis.Redis;
private readonly redisClient;
constructor(redisClient: RedisClient);
getClient(name?: string): Redis.Redis;
}
16 changes: 11 additions & 5 deletions dist/redis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const redis_constants_1 = require("./redis.constants");
const Redis = require("ioredis");
const redis_client_provider_1 = require("./redis-client.provider");
let RedisService = class RedisService {
constructor(client) {
this.client = client;
constructor(redisClient) {
this.redisClient = redisClient;
}
getClient() {
return this.client;
getClient(name) {
if (!name) {
name = this.redisClient.defaultKey;
}
if (!this.redisClient.clients.has(name)) {
throw new redis_client_provider_1.RedisClientError(`client ${name} is not exists`);
}
return this.redisClient.clients.get(name);
}
};
RedisService = __decorate([
Expand Down
33 changes: 31 additions & 2 deletions lib/redis-client.provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import * as Redis from "ioredis"
import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from './redis.constants';
import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface";
import * as uuid from 'uuid'

export class RedisClientError extends Error {}
export interface RedisClient {
defaultKey:string,
clients: Map<string, Redis.Redis>,
size:number
}

export const createClient = () => ({
provide: REDIS_CLIENT,
useFactory:(options: RedisModuleOptions) => {
return new Redis(options)
useFactory:(options: RedisModuleOptions | RedisModuleOptions[]) => {
const clients = new Map<string,Redis.Redis>()
const defaultKey = uuid()
if(Array.isArray(options)) {
for(let o of options) {
if(o.name) {
if(clients.has(o.name)) {
throw new RedisClientError(`client ${o.name} is exists`)
}
clients.set(o.name, new Redis(o))
} else {
if(clients.has(defaultKey)) {
throw new RedisClientError('default client is exists')
}
clients.set(defaultKey, new Redis(o))
}
}
} else {
clients.set(defaultKey,new Redis(options))
}
return {
defaultKey,clients,size:clients.size
}
},
inject:[REDIS_MODULE_OPTIONS]
})
Expand Down
6 changes: 4 additions & 2 deletions lib/redis.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';

export interface RedisModuleOptions extends RedisOptions {}
export interface RedisModuleOptions extends RedisOptions {
name?: string
}

export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>,
useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>,
inject?: any[];
}
2 changes: 1 addition & 1 deletion lib/redis.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createClient, createAsyncClientOptions } from './redis-client.provider'
exports:[RedisService]
})
export class RedisModule {
static register(options:RedisModuleOptions): DynamicModule {
static register(options:RedisModuleOptions|RedisModuleOptions[]): DynamicModule {
return {
module: RedisModule,
providers: [
Expand Down
15 changes: 11 additions & 4 deletions lib/redis.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Injectable, Inject } from "@nestjs/common";
import { REDIS_CLIENT } from './redis.constants';
import * as Redis from "ioredis";
import { RedisClient, RedisClientError } from "./redis-client.provider";

@Injectable()
export class RedisService {
constructor(
@Inject(REDIS_CLIENT) private readonly client: Redis.Redis
) {}
@Inject(REDIS_CLIENT) private readonly redisClient: RedisClient
) { }

getClient() : Redis.Redis {
return this.client
getClient(name?: string): Redis.Redis {
if (!name) {
name = this.redisClient.defaultKey
}
if (!this.redisClient.clients.has(name)) {
throw new RedisClientError(`client ${name} is not exists`)
}
return this.redisClient.clients.get(name)
}
}
23 changes: 0 additions & 23 deletions package-lock.json

This file was deleted.

21 changes: 18 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,37 @@
"build": "rm -rf dist && tsc -p tsconfig.json",
"precommit": "lint-staged",
"prepublish:npm": "yarn run build",
"publish:npm": "yarn publish --access public"
"publish:npm": "yarn publish --access public",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^5.1.0",
"ioredis": "^4.2.0",
"rxjs": "^6.2.2"
"rxjs": "^6.2.2",
"uuid": "^3.3.2"
},
"devDependencies": {
"@nestjs/testing": "^5.5.0",
"@types/ioredis": "^4.0.4",
"@types/node": "^10.7.1",
"@types/uuid": "^3.4.4",
"cz-conventional-changelog": "^2.1.0",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
"typescript": "^2.4.2"
},
"keywords": [
"nestjs",
"nest",
"redis",
"nestjs-redis"
]
],
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
Loading

0 comments on commit b71806e

Please sign in to comment.