Skip to content

Commit

Permalink
add async function
Browse files Browse the repository at this point in the history
  • Loading branch information
skunight committed Dec 6, 2018
1 parent 2d8a4b6 commit 73203d7
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 9 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion dist/redis-client.provider.d.ts
Original file line number Diff line number Diff line change
@@ -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<RedisModuleOptions>;
inject: any[];
};
5 changes: 5 additions & 0 deletions dist/redis-client.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
5 changes: 5 additions & 0 deletions dist/redis.interface.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';
export interface RedisModuleOptions extends RedisOptions {
}
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>;
inject?: any[];
}
3 changes: 2 additions & 1 deletion dist/redis.module.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions dist/redis.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
11 changes: 9 additions & 2 deletions lib/redis-client.provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
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,
useFactory:(options: RedisModuleOptions) => {
return new Redis(options)
},
inject:[REDIS_MODULE_OPTIONS]
})
})

export const createAsyncClientOptions = (options:RedisModuleAsyncOptions) => ({
provide: REDIS_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject
})

10 changes: 8 additions & 2 deletions lib/redis.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { RedisOptions } from 'ioredis'
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';

export interface RedisModuleOptions extends RedisOptions {}
export interface RedisModuleOptions extends RedisOptions {}

export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>,
inject?: any[];
}
18 changes: 15 additions & 3 deletions lib/redis.module.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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]
}
Expand Down

0 comments on commit 73203d7

Please sign in to comment.