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

@sugoi/redis

v4.1.0

Published

sugoi framework redis support

Downloads

2

Readme

@Sugoi\redis

Sugoi logo

npm version Build Status codecov

Introduction

Sugoi is a minimal modular framework, which gives you the ability to use only what you need, fast. The sugoi framework redis module using the redisClient.

As all of the "Sugoi" modules, this module is stand alone and can act without other Sugoi modules.

Installation

npm install @sugoi/redis --save

Initialization

Initialization done by the static method CreateConnection of the RedisProvider

static CreateConnection(connectionConfig: IRedisConfig, connectionName: string): TRedisProvider;

Basic

const redisProvider = RedisProvider.CreateConnection({
    host: "127.0.0.1",
    port: 6379,
    isDefault: true
});
const resolvedClient = RedisProvider.getConnection();
expect(redisProvider).toBe(resolvedClient); // Should be truthy

Using @sugoi/server

@ServerModule({
    services: [
        {
            provide: RedisProvider.CreateConnection({
                             host: "127.0.0.1",
                             port: 6379,
                             isDefault: true
                      }),
            useName: "RedisService"
        }
    ]
})
export class BootstrapModule {
    constructor(@Inject('RedisService') private _redisService: TRedisProvider) {
    }
}

RedisProvider class

The RedisProvider class instance re-exports all of redis methods with promise handler for callback.

Example

await RedisProvider.GetConnection().set('test', '1');
await RedisProvider.GetConnection().get('test'); // Result will be '1'

Pub/Sub

Publish

Publish done by redis client publish method

publish(channel: string, value: string): Promise<number>;

Example

RedisProvider.GetConnection().publish("room-1", "data");

Subscribe

The subscription done by a single method that returns an observable object of type PubSubMessage

public getSubscriber<DataType>(byPattern: boolean, channelOrPattern: string): Observable<PubSubMessage<DataType>>

PubSubMessage class

{
    hasError: boolean;
    data: any;
    error: any;
    type: "data"|"close"|"error"|"init";
    channel: string;
    pattern?: string; // Only in case of pattern subscribe
}

Example

RedisProvider.GetConnection().getSubscriber(true, "room-*").subscribe(
message=>{
    // handle message
},
err=>{
    // handle error
});

Decorators

Another way to subscribe channel\pattern messages is using the following decorators

Subscribe to channel
OnRedisMessage<ResponseType = any>(channel: string,...middlewares: Array<(msg: PubSubMessage<ResponseType>) => TValid>);

Or

OnRedisMessage<ResponseType = any>(channel: string,connectionName?: string,...middlewares: Array<(msg: PubSubMessage<ResponseType>) => TValid>);
Example:
 @OnRedisMessage('decoratorTest', (msg) => {
    return msg.data > 10; // This middleware will act as filter,
                          // only messagea with data greater than 10 will continue to the next method

    })
    public static channelListener(data) {
        this.decoratorTestChannel = data
    }
Subscribe to pattern
OnRedisPMessage<ResponseType = any>(channel: string,...middlewares: Array<(msg: PubSubMessage<ResponseType>) => TValid>);

Or

OnRedisPMessage<ResponseType = any>(channel: string,connectionName?: string,...middlewares: Array<(msg: PubSubMessage<ResponseType>) => TValid>);
Example:
@OnRedisPMessage('decoratorTestPattern-*', null, (msg:PubSubMessage<number>) => {
    return msg.data > 10; // This middleware will act as filter,
                          // only messagea with data greater than 10 will continue to the next method
})
public static patternListener(msg) {
    this.decoratorTestPattern = msg;
}

Scripts

Running lua scripts can be done using the runScripts method

runScripts(...scripts: Array<ScriptResource>): Promise<any>

The ScriptResource provides an API for loading inline scripts and file scripts

Inline scripts

Using the ofScript method allows us to create a script from inline text

Example:

const inlineScriptResource = ScriptResource.OfScript('return redis.call("HSET",KEYS[1],ARGV[1],ARGV[2])')
                                            .setKeys('myKey')
                                            .setArgs('FIELD', 'myValue');
await RedisProvider.GetConnection().runScripts(inlineScriptResource);

File scripts

Using the ofFile method allows us to create a script from inline text

Example:

const fileScriptResource = OfFile(__dirname + '/lua-scripts/myScript.lua')
                                            .setKeys('myKey')
                                            .setArgs('FIELD', 'myValue');
await RedisProvider.GetConnection().runScripts(fileScriptResource);

Documentation

You can find further information on Sugoi official website