
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
@mich4l/nestjs-redis
Advanced tools
Redis module based on popular npm library ioredis
.
onReady
, beforeShutdown
)# pnpm:
pnpm add @mich4l/nestjs-redis ioredis
# npm:
npm install --save @mich4l/nestjs-redis ioredis
# yarn:
yarn add @mich4l/nestjs-redis ioredis
import { Module } from '@nestjs/common';
import { RedisModule } from '@mich4l/nestjs-redis';
@Module({
imports: [
RedisModule.forRoot({
host: 'localhost',
port: 6379,
})
]
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { RedisModule } from '@mich4l/nestjs-redis';
@Module({
imports: [
RedisModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
connectUrl: config.get<string>('REDIS_URL')
})
})
]
})
export class AppModule {}
import { Module } from '@nestjs/common';
import {
RedisOptionsFactory,
IORedisOptions,
RedisModule,
} from '@mich4l/nestjs-redis';
@Injectable()
export class ConfigService implements RedisOptionsFactory {
createRedisOptions(): IORedisOptions {
return {
connectUrl: 'redis://localhost:6379',
};
}
}
@Module({
imports: [
RedisModule.forRootAsync({
useClass: ConfigService,
})
]
})
export class AppModule {}
@Module({
imports: [
RedisModule.forRootAsync({
imports: [ConfigModule],
useExisting: ConfigService,
})
]
})
export class AppModule {}
Redis configuration
app.module.ts
@Module({
imports: [
RedisModule.forRoot({
name: 'conn1',
host: 'localhost',
port: 6379,
}),
ExampleModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
Injecting Redis client to service
example.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { InjectRedis } from '@mich4l/nestjs-redis';
import { Redis } from 'ioredis';
@Injectable()
export class ExampleService {
constructor(
@InjectRedis('conn1')
private readonly redis: Redis,
) {}
async getAllPosts() {
const key = 'posts';
const result = await this.redis.get(key);
return JSON.parse(result);
}
}
Note:
Every connection requires unique name!
@Module({
imports: [
RedisModule.forRoot({
name: 'conn1',
host: 'localhost',
port: 6379,
}),
RedisModule.forRoot({
name: 'conn2',
host: 'localhost',
port: 2137,
})
],
controllers: [],
providers: [],
})
export class AppModule {}
Example with Nest.js config module:
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
validationSchema: ConfigSchema,
}),
RedisModule.forRootAsync({
name: 'my-redis',
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
host: config.get('REDIS_HOST'),
port: config.get('REDIS_PORT'),
}),
}),
]
})
export class AppModule {}
Example with Nest.js Throttler and Redis storage
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
validationSchema: ConfigSchema,
}),
RedisModule.forRootAsync({
name: 'my-redis',
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
host: config.get('REDIS_HOST'),
port: config.get('REDIS_PORT'),
}),
}),
ThrottlerModule.forRootAsync({
inject: [injectRedisToken('my-redis')],
useFactory: (redis: Redis) => ({
storage: new ThrottlerStorageRedisService(redis),
})
})
]
})
export class AppModule {}
Set option isGlobal
to false
to change it.
@Module({
imports: [
RedisModule.forRoot({
isGlobal: false,
host: 'localhost',
port: 6379,
})
]
})
export class AppModule {}
You can use lifecycle hooks to handle errors.
@Module({
imports: [
RedisModule.forRoot({
name: 'conn1',
host: 'localhost',
port: 6379,
onReady: (client: Redis) => {
client.on('error', handleError);
},
beforeShutdown: async (client: Redis) => {
await client.flushall();
}
}),
],
})
export class AppModule {}
FAQs
Redis Nest.js connector module
The npm package @mich4l/nestjs-redis receives a total of 18 weekly downloads. As such, @mich4l/nestjs-redis popularity was classified as not popular.
We found that @mich4l/nestjs-redis demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.