Skip to content

Commit

Permalink
feat: implement forFeature method
Browse files Browse the repository at this point in the history
  • Loading branch information
bassochette committed Feb 25, 2022
1 parent c6c0f3e commit ccc730e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
lib
coverage
dist
docs
2 changes: 1 addition & 1 deletion src/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RedisModule, REDIS_CLIENT, RedisClientType } from "./main";
import { RedisModule, REDIS_CLIENT } from "./main";

describe("main exports", () => {
it("is defined", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/redis/redis.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient } from "redis";

export const REDIS_CLIENT = "RedisClient";
export type RedisClientType = ReturnType<typeof createClient>;
export type RedisClient = ReturnType<typeof createClient>;
16 changes: 16 additions & 0 deletions src/redis/redis.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ export class RedisModule {
};
}

public static forFeature(): DynamicModule {
return {
module: RedisModule,
providers: [
RedisProvider,
{
provide: REDIS_CLIENT,
useFactory: async (redisProvider: RedisProvider) =>
await redisProvider.getClient(),
inject: [RedisProvider],
},
],
exports: [REDIS_CLIENT],
};
}

private static createConfigProviders(
options: RedisModuleAsyncOptions
): Provider {
Expand Down
6 changes: 3 additions & 3 deletions src/redis/redis.provider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Inject, Injectable, Logger } from "@nestjs/common";
import { REDIS_OPTIONS, RedisConnectionOptions } from "./redis.config";
import { createClient } from "redis";
import { REDIS_CLIENT, RedisClientType } from "./redis.client";
import { REDIS_CLIENT, RedisClient } from "./redis.client";

@Injectable()
export class RedisProvider {
client: RedisClientType;
client: RedisClient;

constructor(
@Inject(REDIS_OPTIONS) private readonly options: RedisConnectionOptions
) {}

async getClient(): Promise<RedisClientType> {
async getClient(): Promise<RedisClient> {
if (this.client) return this.client;
this.client = createClient(this.options);

Expand Down

0 comments on commit ccc730e

Please sign in to comment.