0.0.1 ā€¢ Published 4 years ago

@10xcompany/nest-redis v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

NPM version Downloads Commitizen friendly

Description

The Redis module for NestJS.

šŸš€ Quick Start

First install the module via yarn or npm and do not forget to install the database driver as well:

$ yarn add @10xcompany/nest-redis ioredis

or

$ npm i -s @10xcompany/nest-redis ioredis

Once the installation process is completed, we can import the RedisModule into the required module or in global scope in AppModule.

import {RedisModule} from '@10xcompany/nest-redis';

@Module({
  imports: [
    RedisModule.forRoot({
      name: 'foo',
      host: 'localhost',
      port: 6379,
      password: 'neo4j',
      db: 'neo4j',
    }),
  ],
  // ...
})
export class CustomModule {}

Or, if you want to use it in global:

import {RedisModule} from '@10xcompany/nest-redis';

@Module({
  imports: [
    Neo4jModule.forRoot({
      name: 'foo',
      host: 'localhost',
      port: 6379,
      password: 'neo4j',
      db: 'neo4j',
    }),
  ],
  // ...
})
export class AppModule {}

Or, if you're using the Async provider:

import {RedisModule} from '@10xcompany/nest-redis';

@Module({
  imports: [
    RedisModule.forRootAsync({
      // ...
      useFactory: () => ({
        name: 'foo',
        host: 'localhost',
        port: 6379,
        password: 'neo4j',
        db: 'neo4j',
      }),
    }),
  ],
  // ...
})
export class CustomModule {}

The forRoot()/forRootAsync() method accepts the same configuration object as new Redis() from the ioredis package.

Afterward, the RedisService will be available to inject across entire project or the module which the RedisModule is used.

import {RedisService} from '@10xcompany/nest-redis';

@Injectable()
export class MyService {
  constructor(private readonly redisService: RedisService) {}
}

šŸ“š Example usage

@Injectable()
export class ExampleService {
  constructor(private readonly redisService: RedisService) {}

  async create(): Promise<void> {
    await this.redisService.getClient().set('some-id', 'some-value', 'EX');
  }

  async delete(): Promise<void> {
    await this.redisService.getClient().del('some-id');
  }

  async get(id: string): Promise<QueryResult> {
    await this.redisService.getClient().get('some-id');
  }
}

RedisService methods

The RedisService exposes few methods for several use case:

  • getClient(): Redis: method to expose the ioredis instance.

  • getClients(): Map<string, Redis>: method to get all redis instance

šŸ¤ Contributing

Contributions, issues and feature requests are welcome. Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.

Authors

šŸ‘¤ Quadri Adekunle

See also the list of contributors who participated in this project.

Show Your Support

Please ā­ļø this repository if this project helped you!

šŸ“ License

Copyright Ā© 2020.

This project is licensed under the MIT License - see the LICENSE file for details.