npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-redis-box

v0.1.2

Published

The module makes it very easy to connect redis with the ability to use it as a cache, transport, graphql subscription.

Downloads

20

Readme

nestjs-redis-box

Connecting globally

Transferable options (Variant 1)

const options =
  {
    options: {
      host: '127.0.0.1',
      port: 6379,
      username: undefined,
      password: undefined,
    },
    isCache: true,
    isTransport: true,
    isGraphql: true,
  } as IORedis;

Transferable options (Variant 2)


const options = {
    options: {
      host: '127.0.0.1',
      port: 6379,
      username: undefined,
      password: undefined,
      retryStrategy: (times) => {
        const delay = 5000;
        if (times >= 12 * 5) {
          return new Error('REDIS CONNECTION TERMINATED');
        }
        return delay;
      },
    },
    isCache: true,
    isTransport: true,
    isGraphql: true,
  } as IORedis

You can read more about the options here https://docs.nestjs.com/microservices/redis#options

import { RedisModule } from 'nestjs-redis-box';

@Module({
  imports: [
    RedisModule.register(options),
    ...
  ]
})
export default class AppModule {}

Example of sending messages to a microservice with waiting for a response

@Resolver(() => NetworkModel)
export class NetworkResolver {
  constructor(private readonly serviceRedis: RedisService) {}

  @Query(() => NetworksModel, {
    description: '@public - Network list',
  })
  async networks(@CurrentCtx() { relations }): Promise<NetworksModel> {
    try {
      const result = await this.serviceRedis.sendPromise(
        MicroserviceEnum.MS_BLOCKCHAIN_networks,
        { payload, relations },
      );

      if (result.error) throw Error(result.error);
      return result;
    } catch (error) {
      throw new HttpException(error.message, 500);
    }
  }
}

Example of receiving a message in a microservice

@Controller('network')
export class NetworkControler {
  constructor(private readonly dataSource: DataSource) {}

  @MessagePattern(MicroserviceEnum.MS_BLOCKCHAIN_networks)
  async networks(@Payload() data: PayloadListDTO) {
    return await QueryService.list(
      this.dataSource.getRepository(NetworkModel),
      { ...data, select: ['id', 'name', 'symbol', 'fees'] },
    );
  }
}

Clarification:

  • sendPromise - sends a message while waiting for a response (async/await)
  • emit - sends an event without waiting for a response (pub/sub)

If you have a hybrid application and you want to not only send but also receive messages from microservices, then you need to add the following code to the main.ts file

import { RedisModule } from 'nestjs-redis-box';
const options = {} as RedisOptions;

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  ...
  app.connectMicroservice(
    RedisModule.getProviderOptions(options),
    {
      inheritAppConfig: true,
    },
  );
  ...

  await app.startAllMicroservices();
  await app.listen(configuration().platform.port);
}
bootstrap();

To use the caching mechanism in redis it is enough to perform a service connection

 constructor(
    private readonly redisCacheService: RedisCacheService,
    ...
  ) {}

And then save to cache or retrieve from cache.

  async getSession(email: string) {
    return await this.redisCacheService.get(`session:${email}`);
  }

  async setSession(email: string, data: any) {
    await this.redisCacheService.set(`session:${email}`, JSON.stringify(data));
  }

To use the Subscribtion mechanism in Graphql it is enough to perform a service connection

constructor(
  private readonly redisGraphqlService: RedisGraphqlService,
) {}

Send an event to graphql

await this.redisGraphqlService.publish('nameEvent', {...});

And add a resolver to Subscribtion

@Subscription(() => Model, {
    name: 'nameEvent',
    filter: ({ id }, { payload }) => id === payload.id,
    resolve: (data) => data,
  })
  @UseGuards(AuthGuardJwt)
  nameEvent(
    @Args('payload', { type: () => GetIdDTO, nullable: false }) _: GetIdDTO,
  ) {
    return this.redisGraphqlService.asyncIterator<Model>(
      'nameEvent',
    );
  }