From 73203d71f8ea0ead8b40defbaf1730dc72e3e27e Mon Sep 17 00:00:00 2001 From: zzy Date: Thu, 6 Dec 2018 11:10:54 +0800 Subject: [PATCH] add async function --- README.md | 17 +++++++++++++++++ dist/redis-client.provider.d.ts | 7 ++++++- dist/redis-client.provider.js | 5 +++++ dist/redis.interface.d.ts | 5 +++++ dist/redis.module.d.ts | 3 ++- dist/redis.module.js | 11 +++++++++++ lib/redis-client.provider.ts | 11 +++++++++-- lib/redis.interface.ts | 10 ++++++++-- lib/redis.module.ts | 18 +++++++++++++++--- 9 files changed, 78 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 931f5bd..7141730 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,23 @@ import { RedisModule} from 'nestjs-redis' }) export class AppModule {} ``` +With Async +```typescript +import { Module } from '@nestjs/common'; +import { RedisModule} from 'nestjs-redis' + +@Module({ + imports: [ + RedisModuleforRootAsync({ + useFactory: (configService: ConfigService) => configService.get('redis'), // or use async method + //useFactory: async (configService: ConfigService) => configService.get('redis'), + inject:[ConfigService] + }), + ], +}) +export class AppModule {} +``` + Options ```typescript interface RedisOptions { diff --git a/dist/redis-client.provider.d.ts b/dist/redis-client.provider.d.ts index 24182ac..d9e18b9 100644 --- a/dist/redis-client.provider.d.ts +++ b/dist/redis-client.provider.d.ts @@ -1,7 +1,12 @@ import * as Redis from "ioredis"; -import { RedisModuleOptions } from "./redis.interface"; +import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface"; export declare const createClient: () => { provide: symbol; useFactory: (options: RedisModuleOptions) => Redis.Redis; inject: symbol[]; }; +export declare const createAsyncClientOptions: (options: RedisModuleAsyncOptions) => { + provide: symbol; + useFactory: (...args: any[]) => RedisModuleOptions | Promise; + inject: any[]; +}; diff --git a/dist/redis-client.provider.js b/dist/redis-client.provider.js index 2b3f4ae..45edaf3 100644 --- a/dist/redis-client.provider.js +++ b/dist/redis-client.provider.js @@ -9,3 +9,8 @@ exports.createClient = () => ({ }, inject: [redis_constants_1.REDIS_MODULE_OPTIONS] }); +exports.createAsyncClientOptions = (options) => ({ + provide: redis_constants_1.REDIS_MODULE_OPTIONS, + useFactory: options.useFactory, + inject: options.inject +}); diff --git a/dist/redis.interface.d.ts b/dist/redis.interface.d.ts index a5ef634..e58ae98 100644 --- a/dist/redis.interface.d.ts +++ b/dist/redis.interface.d.ts @@ -1,3 +1,8 @@ +import { ModuleMetadata } from '@nestjs/common/interfaces'; import { RedisOptions } from 'ioredis'; export interface RedisModuleOptions extends RedisOptions { } +export interface RedisModuleAsyncOptions extends Pick { + useFactory?: (...args: any[]) => RedisModuleOptions | Promise; + inject?: any[]; +} diff --git a/dist/redis.module.d.ts b/dist/redis.module.d.ts index 404bb9e..7cd012a 100644 --- a/dist/redis.module.d.ts +++ b/dist/redis.module.d.ts @@ -1,5 +1,6 @@ import { DynamicModule } from '@nestjs/common'; -import { RedisModuleOptions } from './redis.interface'; +import { RedisModuleOptions, RedisModuleAsyncOptions } from './redis.interface'; export declare class RedisModule { static register(options: RedisModuleOptions): DynamicModule; + static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule; } diff --git a/dist/redis.module.js b/dist/redis.module.js index d94b704..f1d79e3 100644 --- a/dist/redis.module.js +++ b/dist/redis.module.js @@ -22,6 +22,17 @@ let RedisModule = RedisModule_1 = class RedisModule { exports: [redis_service_1.RedisService] }; } + static forRootAsync(options) { + return { + module: RedisModule_1, + imports: options.imports, + providers: [ + redis_client_provider_1.createClient(), + redis_client_provider_1.createAsyncClientOptions(options), + ], + exports: [redis_service_1.RedisService] + }; + } }; RedisModule = RedisModule_1 = __decorate([ common_1.Global(), diff --git a/lib/redis-client.provider.ts b/lib/redis-client.provider.ts index f1ddad7..e49e1c8 100644 --- a/lib/redis-client.provider.ts +++ b/lib/redis-client.provider.ts @@ -1,6 +1,6 @@ import * as Redis from "ioredis" import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from './redis.constants'; -import { RedisModuleOptions } from "./redis.interface"; +import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface"; export const createClient = () => ({ provide: REDIS_CLIENT, @@ -8,4 +8,11 @@ export const createClient = () => ({ return new Redis(options) }, inject:[REDIS_MODULE_OPTIONS] -}) \ No newline at end of file +}) + +export const createAsyncClientOptions = (options:RedisModuleAsyncOptions) => ({ + provide: REDIS_MODULE_OPTIONS, + useFactory: options.useFactory, + inject: options.inject +}) + \ No newline at end of file diff --git a/lib/redis.interface.ts b/lib/redis.interface.ts index 95f4937..4bf1300 100644 --- a/lib/redis.interface.ts +++ b/lib/redis.interface.ts @@ -1,3 +1,9 @@ -import { RedisOptions } from 'ioredis' +import { ModuleMetadata } from '@nestjs/common/interfaces'; +import { RedisOptions } from 'ioredis'; -export interface RedisModuleOptions extends RedisOptions {} \ No newline at end of file +export interface RedisModuleOptions extends RedisOptions {} + +export interface RedisModuleAsyncOptions extends Pick { + useFactory?: (...args: any[]) => RedisModuleOptions | Promise, + inject?: any[]; +} \ No newline at end of file diff --git a/lib/redis.module.ts b/lib/redis.module.ts index ccea2cd..8922bc6 100644 --- a/lib/redis.module.ts +++ b/lib/redis.module.ts @@ -1,8 +1,8 @@ import { DynamicModule, Module, Global } from '@nestjs/common'; -import { RedisModuleOptions } from './redis.interface'; +import { RedisModuleOptions, RedisModuleAsyncOptions } from './redis.interface'; import { REDIS_MODULE_OPTIONS } from './redis.constants'; import { RedisService } from './redis.service'; -import { createClient } from './redis-client.provider'; +import { createClient, createAsyncClientOptions } from './redis-client.provider'; @Global() @Module({ @@ -15,7 +15,19 @@ export class RedisModule { module: RedisModule, providers: [ createClient(), - { provide: REDIS_MODULE_OPTIONS,useValue:options} + { provide: REDIS_MODULE_OPTIONS, useValue:options} + ], + exports: [RedisService] + } + } + + static forRootAsync(options: RedisModuleAsyncOptions) : DynamicModule { + return { + module: RedisModule, + imports: options.imports, + providers: [ + createClient(), + createAsyncClientOptions(options), ], exports: [RedisService] }