-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from mentatxx/master
Fixed usage with module imports
- Loading branch information
Showing
11 changed files
with
154 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { DynamicModule } from '@nestjs/common'; | ||
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface'; | ||
export declare class RedisCoreModule { | ||
static register(options: RedisModuleOptions | RedisModuleOptions[]): DynamicModule; | ||
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use strict"; | ||
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; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var RedisCoreModule_1; | ||
const common_1 = require("@nestjs/common"); | ||
const redis_client_provider_1 = require("./redis-client.provider"); | ||
const redis_constants_1 = require("./redis.constants"); | ||
const redis_service_1 = require("./redis.service"); | ||
let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule { | ||
static register(options) { | ||
return { | ||
module: RedisCoreModule_1, | ||
providers: [ | ||
redis_client_provider_1.createClient(), | ||
{ provide: redis_constants_1.REDIS_MODULE_OPTIONS, useValue: options }, | ||
], | ||
exports: [redis_service_1.RedisService], | ||
}; | ||
} | ||
static forRootAsync(options) { | ||
return { | ||
module: RedisCoreModule_1, | ||
imports: options.imports, | ||
providers: [redis_client_provider_1.createClient(), redis_client_provider_1.createAsyncClientOptions(options)], | ||
exports: [redis_service_1.RedisService], | ||
}; | ||
} | ||
}; | ||
RedisCoreModule = RedisCoreModule_1 = __decorate([ | ||
common_1.Global(), | ||
common_1.Module({ | ||
providers: [redis_service_1.RedisService], | ||
exports: [redis_service_1.RedisService], | ||
}) | ||
], RedisCoreModule); | ||
exports.RedisCoreModule = RedisCoreModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,49 @@ | ||
import * as Redis from "ioredis" | ||
import * as Redis from 'ioredis'; | ||
import * as uuid from 'uuid'; | ||
|
||
import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from './redis.constants'; | ||
import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface"; | ||
import * as uuid from 'uuid' | ||
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface'; | ||
|
||
export class RedisClientError extends Error {} | ||
export interface RedisClient { | ||
defaultKey:string, | ||
clients: Map<string, Redis.Redis>, | ||
size:number | ||
defaultKey: string; | ||
clients: Map<string, Redis.Redis>; | ||
size: number; | ||
} | ||
|
||
export const createClient = () => ({ | ||
provide: REDIS_CLIENT, | ||
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`) | ||
useFactory: (options: RedisModuleOptions | RedisModuleOptions[]) => { | ||
const clients = new Map<string, Redis.Redis>(); | ||
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`); | ||
} | ||
clients.set(o.name, new Redis(o)) | ||
clients.set(o.name, new Redis(o)); | ||
} else { | ||
if(clients.has(defaultKey)) { | ||
throw new RedisClientError('default client is exists') | ||
if (clients.has(defaultKey)) { | ||
throw new RedisClientError('default client is exists'); | ||
} | ||
clients.set(defaultKey, new Redis(o)) | ||
clients.set(defaultKey, new Redis(o)); | ||
} | ||
} | ||
} else { | ||
clients.set(defaultKey,new Redis(options)) | ||
clients.set(defaultKey, new Redis(options)); | ||
} | ||
return { | ||
defaultKey,clients,size:clients.size | ||
} | ||
defaultKey, | ||
clients, | ||
size: clients.size, | ||
}; | ||
}, | ||
inject:[REDIS_MODULE_OPTIONS] | ||
}) | ||
inject: [REDIS_MODULE_OPTIONS], | ||
}); | ||
|
||
export const createAsyncClientOptions = (options:RedisModuleAsyncOptions) => ({ | ||
export const createAsyncClientOptions = (options: RedisModuleAsyncOptions) => ({ | ||
provide: REDIS_MODULE_OPTIONS, | ||
useFactory: options.useFactory, | ||
inject: options.inject | ||
}) | ||
|
||
inject: options.inject, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { DynamicModule, Global, Module } from '@nestjs/common'; | ||
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface'; | ||
import { | ||
createAsyncClientOptions, | ||
createClient, | ||
} from './redis-client.provider'; | ||
|
||
import { REDIS_MODULE_OPTIONS } from './redis.constants'; | ||
import { RedisService } from './redis.service'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [RedisService], | ||
exports: [RedisService], | ||
}) | ||
export class RedisCoreModule { | ||
static register( | ||
options: RedisModuleOptions | RedisModuleOptions[], | ||
): DynamicModule { | ||
return { | ||
module: RedisCoreModule, | ||
providers: [ | ||
createClient(), | ||
{ provide: REDIS_MODULE_OPTIONS, useValue: options }, | ||
], | ||
exports: [RedisService], | ||
}; | ||
} | ||
|
||
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule { | ||
return { | ||
module: RedisCoreModule, | ||
imports: options.imports, | ||
providers: [createClient(), createAsyncClientOptions(options)], | ||
exports: [RedisService], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,23 @@ | ||
import { DynamicModule, Module, Global } from '@nestjs/common'; | ||
import { RedisModuleOptions, RedisModuleAsyncOptions } from './redis.interface'; | ||
import { REDIS_MODULE_OPTIONS } from './redis.constants'; | ||
import { RedisService } from './redis.service'; | ||
import { createClient, createAsyncClientOptions } from './redis-client.provider'; | ||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface'; | ||
|
||
@Global() | ||
@Module({ | ||
providers:[RedisService], | ||
exports:[RedisService] | ||
}) | ||
import { RedisCoreModule } from './redis-core.module'; | ||
|
||
@Module({}) | ||
export class RedisModule { | ||
static register(options:RedisModuleOptions|RedisModuleOptions[]): DynamicModule { | ||
static register( | ||
options: RedisModuleOptions | RedisModuleOptions[], | ||
): DynamicModule { | ||
return { | ||
module: RedisModule, | ||
providers: [ | ||
createClient(), | ||
{ provide: REDIS_MODULE_OPTIONS, useValue:options} | ||
], | ||
exports: [RedisService] | ||
} | ||
imports: [RedisCoreModule.register(options)], | ||
}; | ||
} | ||
|
||
static forRootAsync(options: RedisModuleAsyncOptions) : DynamicModule { | ||
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule { | ||
return { | ||
module: RedisModule, | ||
imports: options.imports, | ||
providers: [ | ||
createClient(), | ||
createAsyncClientOptions(options), | ||
], | ||
exports: [RedisService] | ||
} | ||
imports: [RedisCoreModule.forRootAsync(options)], | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters