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.
npm install @sugoi/redis --save
Initialization done by the static method CreateConnection
of the RedisProvider
static CreateConnection(connectionConfig: IRedisConfig, connectionName: string): TRedisProvider;
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) {
}
}
The RedisProvider class instance re-exports all of redis methods with promise handler for callback.
await RedisProvider.GetConnection().set('test', '1');
await RedisProvider.GetConnection().get('test'); // Result will be '1'
Publish done by redis client publish method
publish(channel: string, value: string): Promise<number>;
RedisProvider.GetConnection().publish("room-1", "data");
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
}
RedisProvider.GetConnection().getSubscriber(true, "room-*").subscribe(
message=>{
// handle message
},
err=>{
// handle error
});
Another way to subscribe channel\pattern messages is using the following decorators
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>);
@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
}
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>);
@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;
}
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
Using the ofScript
method allows us to create a script from inline text
const inlineScriptResource = ScriptResource.OfScript('return redis.call("HSET",KEYS[1],ARGV[1],ARGV[2])')
.setKeys('myKey')
.setArgs('FIELD', 'myValue');
await RedisProvider.GetConnection().runScripts(inlineScriptResource);
Using the ofFile
method allows us to create a script from inline text
const fileScriptResource = OfFile(__dirname + '/lua-scripts/myScript.lua')
.setKeys('myKey')
.setArgs('FIELD', 'myValue');
await RedisProvider.GetConnection().runScripts(fileScriptResource);
You can find further information on Sugoi official website