https://github.com/mobizerg/nest-ioredis
Ioredis integration module for nestjs framework
https://github.com/mobizerg/nest-ioredis
ioredis nestjs typescript
Last synced: about 1 month ago
JSON representation
Ioredis integration module for nestjs framework
- Host: GitHub
- URL: https://github.com/mobizerg/nest-ioredis
- Owner: mobizerg
- License: other
- Created: 2019-05-24T07:22:56.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-17T12:08:09.000Z (over 6 years ago)
- Last Synced: 2025-06-10T17:11:19.260Z (4 months ago)
- Topics: ioredis, nestjs, typescript
- Language: TypeScript
- Size: 8.79 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
A Ioredis integration module for Nest.js framework.### Installation
**Yarn**
```bash
yarn add @mobizerg/nest-ioredis ioredis
yarn add @types/ioredis --dev
```**NPM**
```bash
npm install @mobizerg/nest-ioredis ioredis --save
npm install @types/ioredis --save-dev
```### Description
Redis integration module for [Nest.js](https://github.com/nestjs/nest) based on the [Ioredis](https://github.com/luin/ioredis) package.### Usage
Import the **IoredisModule** in `app.module.ts`
```typescript
import { Module } from '@nestjs/common';
import { IoredisModule } from '@mobizerg/nest-ioredis';@Module({
imports: [
IoredisModule.register(options);
],
})
export class AppModule {}
```
With Async
```typescript
import { Module } from '@nestjs/common';
import { IoredisModule } from '@mobizerg/nest-ioredis';@Module({
imports: [
IoredisModule.registerAsync({
imports: [ConfigModule],
useExisting: IoredisConfigService,
}),
],
})
export class AppModule {}
```Example config file (async)
```typescript
import { Injectable } from '@nestjs/common';
import { ConfigService } from './config.service';
import { IoredisModuleOptions, IoredisOptionsFactory } from '@mobizerg/nest-ioredis';@Injectable()
export class IoredisConfigService implements IoredisOptionsFactory {constructor(private readonly config: ConfigService) {}
createIoredisOptions(name?: string): IoredisModuleOptions {
return {
name,
host: this.config.redisHost,
port: this.config.redisPort,
db: this.config.redisDatabase,
keyPrefix: this.config.redisPrefix,
};
}
}
```Importing inside services
```typescript
import { Injectable } from '@nestjs/common';
import { InjectRedis } from '@mobizerg/nest-ioredis';
import { Redis } from 'ioredis';
import { serialize } from 'class-transformer';@Injectable()
export class IoredisService {constructor(@InjectRedis(REDIS_CLIENT_NAME)
private readonly client: Redis) {}async publish(event: string, payload: T): Promise {
return await this.client.publish(event, serialize(payload));
}
}
```### License
MIT