📝Redis

This service is used to interact with the Redis database.

Imports

import { RedisService } from 'niro-health';

Environment Variables

Do you create variables an .env file.

name
description

REDIS_HOST

Host of the redis.

REDIS_PORT

Port of the redis.

REDIS_PASSWORD

Password of the redis.

Check the example file.

.env
REDIS_HOST=redis://localhost:6379
REDIS_PORT=6379
REDIS_PASSWORD=

Method of Use

To use this module, you need to inject it into the desired service.

import { Module } from '@nestjs/common';
import {
  AppHostService,
  PrismaService,
  ConfigurationService,
  ValidatorRegexpService,
  StringExService,
  DebugService,
} from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'IAppHostService',
      useClass: AppHostService,
    },
    {
      provide: 'IPrismaService',
      useClass: PrismaService,
    },
    {
      provide: 'IConfigurationService',
      useClass: ConfigurationService,
    },
    {
      provide: 'IValidatorRegexpService',
      useClass: ValidatorRegexpService,
    },
    {
      provide: 'IStringExService',
      useClass: StringExService,
    },
    {
      provide: 'IDebugService',
      useClass: DebugService,
    },
  ],
})
export class AppModule {}

Injecting the module into our service.

import { Inject, Injectable } from '@nestjs/common';
import type { IAppHostService, IRedisService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('IAppHostService')
    private readonly appHostService: IAppHostService,
    @Inject('IRedisService')
    private readonly redisService: IRedisService,
  ) {}

  async enableShutdownHooks() {
    await this.redisService.enableShutdownHooks(this.appHostService.app);
  }
}

Modifying our main.ts

main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from '@/app.module';
import { Logger } from '@nestjs/common';
import { AppHostModule, AppHostService } from 'niro-health';
import { AppModule } from './app.module';
import { AppService } from './app.service';

(async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: new Logger('NestApplication', {
      timestamp: true,
    }),
    cors: true,
  });
  app.select(AppHostModule).get(AppHostService).setApp(app);
  app.select(AppModule).get(AppService).enableShutdownHooks();
})();

With dependencies

You will need to inject the following interfaces.

Interface
Service

AppHostService

IConfigurationService

IValidatorRegexpService

IStringExService

IDebugService

Properties

Property
Scope
Description

app

public

This is instance of application.

_client

private

This is the major component of ioredis. Use it to connect to a standalone Redis server or Sentinels.

_redisConfigurationFalsy

private

This is used to check if the environment variables from Redis are missing.

get redisConfigurationFalsy

public

Returns if environment variables from Redis are missing.

Methods

Method
Scope
Description

keys

public

Get all keys that match a pattern.

save

public

It saves a key-value pair in the Redis database.

saveBuffer

public

It saves a key-value pair in the Redis database.

update

public

It update a key-value pair in the Redis database.

updateBuffer

public

It update a key-value pair in the Redis database.

findAll

public

It returns all the values in the Redis database.

findOne

public

It returns a value in the Redis database.

delete

public

It delete a key-value pair in the Redis database.

flushall

public

Delete all the keys of all the existing databases, not just the currently selected one.

shutdown

public

This method is used to shutdown application.

onApplicationShutdown

public

Application shutdown hook.

enableShutdownHooks

public

Enable shutdown hooks.

Last updated