@jasonsoft/nestjs-redis
Version:
Redis module for Nest framework (node.js)
114 lines (113 loc) • 4.53 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var RedisModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisModule = void 0;
const common_1 = require("@nestjs/common");
const ioredis_1 = __importDefault(require("ioredis"));
const redis_cache_helper_1 = require("./redis-cache.helper");
const redis_constants_1 = require("./redis.constants");
/**
* JasonSoft Redis Module
* Added by Jason.Song (成长的小猪) on 2023/03/16 23:02:18
*/
let RedisModule = exports.RedisModule = RedisModule_1 = class RedisModule {
static forRoot(options) {
return {
module: RedisModule_1,
global: true,
providers: [
{
provide: redis_constants_1.JASONSOFT_REDIS_OPTIONS,
useValue: options,
},
{
provide: redis_constants_1.JASONSOFT_REDIS,
useFactory: () => {
return this.createRedisClient(options);
},
},
redis_cache_helper_1.RedisCacheHelper,
],
exports: [redis_constants_1.JASONSOFT_REDIS, redis_cache_helper_1.RedisCacheHelper],
};
}
static createRedisClient(options) {
const { url } = options, redisOptions = __rest(options, ["url"]);
if (url) {
return new ioredis_1.default(url, redisOptions);
}
return new ioredis_1.default(options);
}
static forRootAsync(options) {
return {
module: RedisModule_1,
global: true,
imports: options.imports || [],
providers: [
...this.createAsyncProviders(options),
{
provide: redis_constants_1.JASONSOFT_REDIS,
useFactory: (options) => {
return this.createRedisClient(options);
},
inject: [redis_constants_1.JASONSOFT_REDIS_OPTIONS],
},
redis_cache_helper_1.RedisCacheHelper,
],
exports: [redis_constants_1.JASONSOFT_REDIS, redis_cache_helper_1.RedisCacheHelper],
};
}
static createAsyncProviders(options) {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}
const useClass = options.useClass;
return [
this.createAsyncOptionsProvider(options),
{
provide: useClass,
useClass,
},
];
}
static createAsyncOptionsProvider(asyncOptions) {
if (asyncOptions.useFactory) {
return {
provide: redis_constants_1.JASONSOFT_REDIS_OPTIONS,
useFactory: asyncOptions.useFactory,
inject: asyncOptions.inject || [],
};
}
return {
provide: redis_constants_1.JASONSOFT_REDIS_OPTIONS,
useFactory: async (optionsFactory) => optionsFactory.createRedisModuleOptions(),
inject: [
(asyncOptions.useClass ||
asyncOptions.useExisting),
],
};
}
};
exports.RedisModule = RedisModule = RedisModule_1 = __decorate([
(0, common_1.Module)({})
], RedisModule);