open-index/open-npm huggingface dataset:
npm package: @gaosong886/nestjs-redis
Description: A nestjs module provides a simple interface for working with ioredis.
Author: gaosong886
Latest version: 1.0.0


<h1 align="center">nestjs-redis</h1>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
</p>

## Description

A [nestjs](https://github.com/nestjs/nest) module provides a simple interface for working with [ioredis](https://github.com/redis/ioredis)

## Installation

```bash
$ npm i --save @gaosong886/nestjs-redis ioredis 
```

## Quick start

Import the `RedisModule`

```ts
import { Module } from '@nestjs/common';
import { RedisModule } from '@gaosong886/nestjs-redis';

@Module({
  imports: [
    RedisModule.forRoot({
      port: 6379,
      host: 'localhost',
      db: 0,
    }),
  ],
  ...
})
export class AppModule {}
```

Inject the `RedisClient` into your class using the `@InjectRedis()` decorator

```ts
import { Injectable } from '@nestjs/common';
import { InjectRedis, RedisClient } from '@gaosong886/nestjs-redis';

@Injectable()
export class MyService {
  constructor(
    @InjectRedis() private readonly redisClient: RedisClient,
  ) {}

  async getSomething(): Promise<string> {
    return await this.redisClient.get('myKey');
  }
}
```

## Cluster & Multi instance

Some projects may require multiple redis connections, which can also be achieved using this module. In this case, the `name` of the instance becomes a mandatory field.

```ts
import { Module } from '@nestjs/common';
import { RedisModule } from '@gaosong886/nestjs-redis';

@Module({
  imports: [
    RedisModule.forRoot({
      name: 'instance-1',
      port: 6379,
      host: '192.168.1.1',
      db: 0,
    }),

    // async passing options 
    RedisModule.forRootAsync({
      name: 'instance-2',
      useFactory: async () => {
        return {
          port: 6380,
          host: '192.168.1.2',
          db: 1,
        };
      },
    }),

    // cluster
    RedisModule.forRoot({
      name: 'cluster',
      nodes: [
        {
          port: 6380,
          host: '127.0.0.1',
        },
        {
          port: 6381,
          host: '127.0.0.1',
        },
      ],
      options: {
        scaleReads: 'slave',
      },
      // see more about cluster options
      // https://github.com/redis/ioredis/blob/main/README.md#cluster
    }),
  ],
  ...
})
export class AppModule {}
```
You can inject the `RedisClient` for a given instance `name`

```ts
import { Injectable } from '@nestjs/common';
import { InjectRedis, RedisClient } from '@gaosong886/nestjs-redis';

@Injectable()
export class MyService {
  constructor(
    @InjectRedis('instance-1') private readonly instance1: RedisClient,
    @InjectRedis('instance-2') private readonly instance2: RedisClient,
    @InjectRedis('cluster') private readonly cluster: RedisClient,
  ) {}

  ...
}
```
npm package: @gaosong886/nestjs-winston
Description: A nestjs module provides a simple interface for working with winston.
Author: gaosong886
Latest version: 1.0.0


<h1 align="center">nestjs-winston</h1>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
</p>

## Description

A [nestjs](https://github.com/nestjs/nest) module provides a simple interface for working with [winston](https://github.com/winstonjs/winston)

## Installation

```bash
$ npm i --save @gaosong886/nestjs-winston winston 
```

## Quick start

Import the `WinstonModule`

```ts
import { Module } from '@nestjs/common';
import { format, transports } from 'winston';
import { WinstonModule, WinstonModuleOptions } from '@gaosong886/nestjs-winston';
import 'winston-daily-rotate-file'; // Expand winston.transports with adding 'DailyRotateFile'

@Module({
  imports: [
    WinstonModule.forRootAsync({
      useFactory: async (): Promise<WinstonModuleOptions> => {
        const { combine, label, timestamp, printf } = format;

        // Create a transport to a rotating file
        // https://github.com/winstonjs/winston-daily-rotate-file
        const appTransport = new transports.DailyRotateFile({
          filename: 'logs/app-%DATE%.log',
          datePattern: 'YYYY-MM-DD',
          zippedArchive: true,
          maxSize: '20m',
          maxFiles: '14d',
        });

        const options: WinstonModuleOptions = {
          format: combine(
            label({ label: 'MyLabel' }),
            timestamp({ format: 'YYYY/MM/DD hh:mm:ss' }),
            printf(({ label, pid, timestamp, level, message, context }) => {
              return `[${label}] ${pid} - ${timestamp} ${level} [${context}] ${message}`;
            }),
          ),
          transports: [appTransport],
        };
        return options;
      },
    })
  ],
  ...
})
export class AppModule {}
```

Inject the `WinstonService` into your class

```ts
import { Injectable } from '@nestjs/common';
import { WinstonService } from 'src/common/winston/winston.service';

@Injectable()
export class MyService {
  constructor(private readonly winstonService: WinstonService) {
    this.winstonService.setContext(MyService.name);
  }

  doSomething() {
    this.winstonService.log('Hello');
    // [MyLabel] 7972 - 2023/11/25 04:28:57 info [MyService] Hello
  }
}

```