Socket
Book a DemoInstallSign in
Socket

nest-rest-framework-redis

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nest-rest-framework-redis

Nest Redis Librarry

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Nest Logo Nest Logo

CircleCI

Description

Nest-Rest-Framework-Redis manages your REDIS CRUD for you. Need a super fast Key-Value store for some of your data, but don't want to sacrficie REST conventions? Try our framework

Installation

$ npm install --save nest-rest-framework nest-rest-framework-redis

Usage

See the example Directory.

Like All Nest Rest Framework implementations, you start with a model.

export class ExampleModel {
  id: string;
  firstName: string;
  lastName: string;
  age: number;
}

Then you define a ViewSet that Extends RedisViewSet. The Class should be injectible. The redisRepository injected will be provided by a Global Export from the NestRestFrameworkRedisModule. The second argument is the type you want to save. The third argument is the property inside that object represeting the Key, redis is a key/value store. This can also be a function which return a string. The final argument is a prefix to use for the Type. If you want to colocate multiple types in one redis cluster then using a prefix specific to each type helps

import { Injectable } from '@nestjs/common';
import { RedisViewSet, RedisRepository } from 'nest-rest-framework-redis';
import { ExampleModel } from './example-model';

@Injectable()
export class ExampleViewSet extends RedisViewSet<ExampleModel> {
  constructor(readonly redisRepository: RedisRepository) {
    super(
      redisRepository, // The Redis Repository
      ExampleModel, // The Example Model you want to svae
      'id', // The property of ExampleModel representing the primaryKey
      'example_', // The Prefix for distinguishing keys of different data types in Redis.
    );
  }
}

Then you define a controller that injects that ViewSet. It needs to be decorated as a controller. You can review the other options which can be injected into the RestController. They essentially allow you to do any business logic necessary at all points of the request lifecycle.

import { Controller } from '@nestjs/common';
import { RestController } from 'nest-rest-framework';
import { ExampleModel } from './example-model';
import { ExampleViewSet } from './example-view-set';

@Controller('example')
export class ExampleController extends RestController<
  string,
  ExampleModel,
  ExampleModel,
  ExampleModel
> {
  constructor(readonly exampleViewSet: ExampleViewSet) {
    super({
      viewset: exampleViewSet,
    });
  }
}

Finally import NestRestFrameworkRedisModule in your app module. It will provide the RedisRepository you need. The forRoot method takes ClientOps arguments from the node redis lib. The second argument is whether you want to run a local in-memroy mock-redis server for testing. We found the library very, very useful.

import { Module } from '@nestjs/common';
import { ExampleController } from './example.controller';
import { ExampleViewSet } from './example-view-set';
import { NestRestFrameworkRedisModule } from 'nest-rest-framework-redis';

@Module({
  imports: [
    NestRestFrameworkRedisModule.forRoot(
      {
        host: 'localhost',
        port: 66666,
        password: 'fakepassword',
      },
      true,
    ),
  ],
  controllers: [ExampleController],
  providers: [ExampleViewSet],
})
export class ExampleModule {}

Stay in touch

  • Author - Benjamin Main

License

Nest-Rest-Framework-Redis is MIT licensed.

Keywords

nest

FAQs

Package last updated on 21 Feb 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.