提交 100b894f authored 作者: zzy's avatar zzy

1.2.6

上级 e53907fc
import * as Redis from 'ioredis';
import { Provider } from '@nestjs/common';
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface';
export declare class RedisClientError extends Error {
}
......@@ -7,15 +8,7 @@ export interface RedisClient {
clients: Map<string, Redis.Redis>;
size: number;
}
export declare const createClient: () => {
provide: symbol;
useFactory: (options: RedisModuleOptions | RedisModuleOptions[]) => {
defaultKey: string;
clients: Map<string, Redis.Redis>;
size: number;
};
inject: symbol[];
};
export declare const createClient: () => Provider<any>;
export declare const createAsyncClientOptions: (options: RedisModuleAsyncOptions) => {
provide: symbol;
useFactory: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions> | RedisModuleOptions[] | Promise<RedisModuleOptions[]>;
......
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Redis = require("ioredis");
const uuid = require("uuid");
......@@ -6,51 +23,39 @@ const redis_constants_1 = require("./redis.constants");
class RedisClientError extends Error {
}
exports.RedisClientError = RedisClientError;
function getClient(options) {
return __awaiter(this, void 0, void 0, function* () {
const { onClientReady, url } = options, opt = __rest(options, ["onClientReady", "url"]);
const client = url ? new Redis(url) : new Redis(opt);
if (onClientReady) {
onClientReady(client);
}
return client;
});
}
exports.createClient = () => ({
provide: redis_constants_1.REDIS_CLIENT,
useFactory: (options) => {
useFactory: (options) => __awaiter(this, void 0, void 0, function* () {
const clients = new Map();
const defaultKey = uuid();
if (Array.isArray(options)) {
for (const o of options) {
if (o.name) {
if (clients.has(o.name)) {
throw new RedisClientError(`client ${o.name} is exists`);
}
if (o.url) {
clients.set(o.name, new Redis(o.url));
}
else {
clients.set(o.name, new Redis(o));
}
}
else {
if (clients.has(defaultKey)) {
throw new RedisClientError('default client is exists');
yield Promise.all(options.map((o) => __awaiter(this, void 0, void 0, function* () {
const key = o.name || defaultKey;
if (clients.has(key)) {
throw new RedisClientError(`${o.name || 'default'} client is exists`);
}
if (o.url) {
clients.set(defaultKey, new Redis(o.url));
clients.set(key, yield getClient(o));
})));
}
else {
clients.set(defaultKey, new Redis(o));
}
}
}
}
else {
if (options.url) {
clients.set(defaultKey, new Redis(options.url));
}
else {
clients.set(defaultKey, new Redis(options));
}
clients.set(defaultKey, yield getClient(options));
}
return {
defaultKey,
clients,
size: clients.size,
};
},
}),
inject: [redis_constants_1.REDIS_MODULE_OPTIONS],
});
exports.createAsyncClientOptions = (options) => ({
......
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';
import { Redis, RedisOptions } from 'ioredis';
export interface RedisModuleOptions extends RedisOptions {
name?: string;
url?: string;
onClientReady?(client: Redis): Promise<void>;
}
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>;
......
import * as Redis from "ioredis";
import { RedisClient } from "./redis-client.provider";
import * as Redis from 'ioredis';
import { RedisClient } from './redis-client.provider';
export declare class RedisService {
private readonly redisClient;
constructor(redisClient: RedisClient);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论