提交 b71806e0 authored 作者: zzy's avatar zzy

feat(redis-client.provider.ts): add multi client

service can can use multi client
上级 0626744f
import * as Redis from "ioredis"; import * as Redis from "ioredis";
import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface"; 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: () => { export declare const createClient: () => {
provide: symbol; provide: symbol;
useFactory: (options: RedisModuleOptions) => Redis.Redis; useFactory: (options: RedisModuleOptions | RedisModuleOptions[]) => {
defaultKey: string;
clients: Map<string, Redis.Redis>;
size: number;
};
inject: symbol[]; inject: symbol[];
}; };
export declare const createAsyncClientOptions: (options: RedisModuleAsyncOptions) => { export declare const createAsyncClientOptions: (options: RedisModuleAsyncOptions) => {
provide: symbol; provide: symbol;
useFactory: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>; useFactory: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions> | RedisModuleOptions[] | Promise<RedisModuleOptions[]>;
inject: any[]; inject: any[];
}; };
...@@ -2,10 +2,37 @@ ...@@ -2,10 +2,37 @@
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const Redis = require("ioredis"); const Redis = require("ioredis");
const redis_constants_1 = require("./redis.constants"); const redis_constants_1 = require("./redis.constants");
const uuid = require("uuid");
class RedisClientError extends Error {
}
exports.RedisClientError = RedisClientError;
exports.createClient = () => ({ exports.createClient = () => ({
provide: redis_constants_1.REDIS_CLIENT, provide: redis_constants_1.REDIS_CLIENT,
useFactory: (options) => { 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] inject: [redis_constants_1.REDIS_MODULE_OPTIONS]
}); });
......
import { ModuleMetadata } from '@nestjs/common/interfaces'; import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis'; import { RedisOptions } from 'ioredis';
export interface RedisModuleOptions extends RedisOptions { export interface RedisModuleOptions extends RedisOptions {
name?: string;
} }
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> { export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>; useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>;
inject?: any[]; inject?: any[];
} }
import { DynamicModule } from '@nestjs/common'; import { DynamicModule } from '@nestjs/common';
import { RedisModuleOptions, RedisModuleAsyncOptions } from './redis.interface'; import { RedisModuleOptions, RedisModuleAsyncOptions } from './redis.interface';
export declare class RedisModule { export declare class RedisModule {
static register(options: RedisModuleOptions): DynamicModule; static register(options: RedisModuleOptions | RedisModuleOptions[]): DynamicModule;
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule; static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule;
} }
import * as Redis from "ioredis"; import * as Redis from "ioredis";
import { RedisClient } from "./redis-client.provider";
export declare class RedisService { export declare class RedisService {
private readonly client; private readonly redisClient;
constructor(client: Redis.Redis); constructor(redisClient: RedisClient);
getClient(): Redis.Redis; getClient(name?: string): Redis.Redis;
} }
...@@ -14,13 +14,19 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { ...@@ -14,13 +14,19 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common"); const common_1 = require("@nestjs/common");
const redis_constants_1 = require("./redis.constants"); const redis_constants_1 = require("./redis.constants");
const Redis = require("ioredis"); const redis_client_provider_1 = require("./redis-client.provider");
let RedisService = class RedisService { let RedisService = class RedisService {
constructor(client) { constructor(redisClient) {
this.client = client; this.redisClient = redisClient;
} }
getClient() { getClient(name) {
return this.client; 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([ RedisService = __decorate([
......
import * as Redis from "ioredis" import * as Redis from "ioredis"
import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from './redis.constants'; import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from './redis.constants';
import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface"; 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 = () => ({ export const createClient = () => ({
provide: REDIS_CLIENT, provide: REDIS_CLIENT,
useFactory:(options: RedisModuleOptions) => { useFactory:(options: RedisModuleOptions | RedisModuleOptions[]) => {
return new Redis(options) 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] inject:[REDIS_MODULE_OPTIONS]
}) })
......
import { ModuleMetadata } from '@nestjs/common/interfaces'; import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis'; import { RedisOptions } from 'ioredis';
export interface RedisModuleOptions extends RedisOptions {} export interface RedisModuleOptions extends RedisOptions {
name?: string
}
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> { export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>, useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>,
inject?: any[]; inject?: any[];
} }
\ No newline at end of file
...@@ -10,7 +10,7 @@ import { createClient, createAsyncClientOptions } from './redis-client.provider' ...@@ -10,7 +10,7 @@ import { createClient, createAsyncClientOptions } from './redis-client.provider'
exports:[RedisService] exports:[RedisService]
}) })
export class RedisModule { export class RedisModule {
static register(options:RedisModuleOptions): DynamicModule { static register(options:RedisModuleOptions|RedisModuleOptions[]): DynamicModule {
return { return {
module: RedisModule, module: RedisModule,
providers: [ providers: [
......
import { Injectable, Inject } from "@nestjs/common"; import { Injectable, Inject } from "@nestjs/common";
import { REDIS_CLIENT } from './redis.constants'; import { REDIS_CLIENT } from './redis.constants';
import * as Redis from "ioredis"; import * as Redis from "ioredis";
import { RedisClient, RedisClientError } from "./redis-client.provider";
@Injectable() @Injectable()
export class RedisService { export class RedisService {
constructor( constructor(
@Inject(REDIS_CLIENT) private readonly client: Redis.Redis @Inject(REDIS_CLIENT) private readonly redisClient: RedisClient
) {} ) { }
getClient() : Redis.Redis { getClient(name?: string): Redis.Redis {
return this.client 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)
} }
} }
\ No newline at end of file
{
"name": "nestjs-redis",
"version": "0.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/ioredis": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.0.4.tgz",
"integrity": "sha512-QdJTMFrmKkphjoGIxItTMhP++8/6INLbgSIxB8kd9N+3OuiuiaZ2knt+OR4gFQrloac/ctwaQA1PCzISmD9afQ==",
"dev": true,
"requires": {
"@types/node": "10.12.12"
}
},
"@types/node": {
"version": "10.12.12",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.12.tgz",
"integrity": "sha512-Pr+6JRiKkfsFvmU/LK68oBRCQeEg36TyAbPhc2xpez24OOZZCuoIhWGTd39VZy6nGafSbxzGouFPTFD/rR1A0A==",
"dev": true
}
}
}
...@@ -12,16 +12,26 @@ ...@@ -12,16 +12,26 @@
"build": "rm -rf dist && tsc -p tsconfig.json", "build": "rm -rf dist && tsc -p tsconfig.json",
"precommit": "lint-staged", "precommit": "lint-staged",
"prepublish:npm": "yarn run build", "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": { "dependencies": {
"@nestjs/common": "^5.1.0", "@nestjs/common": "^5.1.0",
"ioredis": "^4.2.0", "ioredis": "^4.2.0",
"rxjs": "^6.2.2" "rxjs": "^6.2.2",
"uuid": "^3.3.2"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/testing": "^5.5.0",
"@types/ioredis": "^4.0.4", "@types/ioredis": "^4.0.4",
"@types/node": "^10.7.1", "@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" "typescript": "^2.4.2"
}, },
"keywords": [ "keywords": [
...@@ -29,5 +39,10 @@ ...@@ -29,5 +39,10 @@
"nest", "nest",
"redis", "redis",
"nestjs-redis" "nestjs-redis"
] ],
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
} }
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论