You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
V5 bringing RESP3, Sentinel and TypeMapping to node-redis
RESP3 Support - Some commands responses in RESP3 aren't stable yet and therefore return an "untyped" ReplyUnion. Sentinel TypeMapping Correctly types Multi commands Note: some API changes to be further documented in v4-to-v5.md
This commit is contained in:
219
packages/client/lib/sentinel/multi-commands.ts
Normal file
219
packages/client/lib/sentinel/multi-commands.ts
Normal file
@@ -0,0 +1,219 @@
|
||||
import COMMANDS from '../commands';
|
||||
import RedisMultiCommand, { MULTI_REPLY, MultiReply, MultiReplyType } from '../multi-command';
|
||||
import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, TransformReply, RedisScript, RedisFunction, TypeMapping } from '../RESP/types';
|
||||
import { attachConfig, functionArgumentsPrefix, getTransformReply } from '../commander';
|
||||
import { RedisSentinelType } from './types';
|
||||
|
||||
type CommandSignature<
|
||||
REPLIES extends Array<unknown>,
|
||||
C extends Command,
|
||||
M extends RedisModules,
|
||||
F extends RedisFunctions,
|
||||
S extends RedisScripts,
|
||||
RESP extends RespVersions,
|
||||
TYPE_MAPPING extends TypeMapping
|
||||
> = (...args: Parameters<C['transformArguments']>) => RedisSentinelMultiCommandType<
|
||||
[...REPLIES, ReplyWithTypeMapping<CommandReply<C, RESP>, TYPE_MAPPING>],
|
||||
M,
|
||||
F,
|
||||
S,
|
||||
RESP,
|
||||
TYPE_MAPPING
|
||||
>;
|
||||
|
||||
type WithCommands<
|
||||
REPLIES extends Array<unknown>,
|
||||
M extends RedisModules,
|
||||
F extends RedisFunctions,
|
||||
S extends RedisScripts,
|
||||
RESP extends RespVersions,
|
||||
TYPE_MAPPING extends TypeMapping
|
||||
> = {
|
||||
[P in keyof typeof COMMANDS]: CommandSignature<REPLIES, (typeof COMMANDS)[P], M, F, S, RESP, TYPE_MAPPING>;
|
||||
};
|
||||
|
||||
type WithModules<
|
||||
REPLIES extends Array<unknown>,
|
||||
M extends RedisModules,
|
||||
F extends RedisFunctions,
|
||||
S extends RedisScripts,
|
||||
RESP extends RespVersions,
|
||||
TYPE_MAPPING extends TypeMapping
|
||||
> = {
|
||||
[P in keyof M]: {
|
||||
[C in keyof M[P]]: CommandSignature<REPLIES, M[P][C], M, F, S, RESP, TYPE_MAPPING>;
|
||||
};
|
||||
};
|
||||
|
||||
type WithFunctions<
|
||||
REPLIES extends Array<unknown>,
|
||||
M extends RedisModules,
|
||||
F extends RedisFunctions,
|
||||
S extends RedisScripts,
|
||||
RESP extends RespVersions,
|
||||
TYPE_MAPPING extends TypeMapping
|
||||
> = {
|
||||
[L in keyof F]: {
|
||||
[C in keyof F[L]]: CommandSignature<REPLIES, F[L][C], M, F, S, RESP, TYPE_MAPPING>;
|
||||
};
|
||||
};
|
||||
|
||||
type WithScripts<
|
||||
REPLIES extends Array<unknown>,
|
||||
M extends RedisModules,
|
||||
F extends RedisFunctions,
|
||||
S extends RedisScripts,
|
||||
RESP extends RespVersions,
|
||||
TYPE_MAPPING extends TypeMapping
|
||||
> = {
|
||||
[P in keyof S]: CommandSignature<REPLIES, S[P], M, F, S, RESP, TYPE_MAPPING>;
|
||||
};
|
||||
|
||||
export type RedisSentinelMultiCommandType<
|
||||
REPLIES extends Array<any>,
|
||||
M extends RedisModules,
|
||||
F extends RedisFunctions,
|
||||
S extends RedisScripts,
|
||||
RESP extends RespVersions,
|
||||
TYPE_MAPPING extends TypeMapping
|
||||
> = (
|
||||
RedisSentinelMultiCommand<REPLIES> &
|
||||
WithCommands<REPLIES, M, F, S, RESP, TYPE_MAPPING> &
|
||||
WithModules<REPLIES, M, F, S, RESP, TYPE_MAPPING> &
|
||||
WithFunctions<REPLIES, M, F, S, RESP, TYPE_MAPPING> &
|
||||
WithScripts<REPLIES, M, F, S, RESP, TYPE_MAPPING>
|
||||
);
|
||||
|
||||
export default class RedisSentinelMultiCommand<REPLIES = []> {
|
||||
private static _createCommand(command: Command, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(command, resp);
|
||||
return function (this: RedisSentinelMultiCommand, ...args: Array<unknown>) {
|
||||
const redisArgs = command.transformArguments(...args);
|
||||
return this.addCommand(
|
||||
command.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
transformReply
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
private static _createModuleCommand(command: Command, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(command, resp);
|
||||
return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array<unknown>) {
|
||||
const redisArgs = command.transformArguments(...args);
|
||||
return this._self.addCommand(
|
||||
command.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
transformReply
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
private static _createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
|
||||
const prefix = functionArgumentsPrefix(name, fn),
|
||||
transformReply = getTransformReply(fn, resp);
|
||||
return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array<unknown>) {
|
||||
const fnArgs = fn.transformArguments(...args);
|
||||
const redisArgs: CommandArguments = prefix.concat(fnArgs);
|
||||
redisArgs.preserve = fnArgs.preserve;
|
||||
return this._self.addCommand(
|
||||
fn.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
transformReply
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
private static _createScriptCommand(script: RedisScript, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(script, resp);
|
||||
return function (this: RedisSentinelMultiCommand, ...args: Array<unknown>) {
|
||||
const scriptArgs = script.transformArguments(...args);
|
||||
this._setState(
|
||||
script.IS_READ_ONLY
|
||||
);
|
||||
this._multi.addScript(
|
||||
script,
|
||||
scriptArgs,
|
||||
transformReply
|
||||
);
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
static extend<
|
||||
M extends RedisModules = Record<string, never>,
|
||||
F extends RedisFunctions = Record<string, never>,
|
||||
S extends RedisScripts = Record<string, never>,
|
||||
RESP extends RespVersions = 2
|
||||
>(config?: CommanderConfig<M, F, S, RESP>) {
|
||||
return attachConfig({
|
||||
BaseClass: RedisSentinelMultiCommand,
|
||||
commands: COMMANDS,
|
||||
createCommand: RedisSentinelMultiCommand._createCommand,
|
||||
createModuleCommand: RedisSentinelMultiCommand._createModuleCommand,
|
||||
createFunctionCommand: RedisSentinelMultiCommand._createFunctionCommand,
|
||||
createScriptCommand: RedisSentinelMultiCommand._createScriptCommand,
|
||||
config
|
||||
});
|
||||
}
|
||||
|
||||
private readonly _multi = new RedisMultiCommand();
|
||||
private readonly _sentinel: RedisSentinelType
|
||||
private _isReadonly: boolean | undefined = true;
|
||||
private readonly _typeMapping?: TypeMapping;
|
||||
|
||||
constructor(sentinel: RedisSentinelType, typeMapping: TypeMapping) {
|
||||
this._sentinel = sentinel;
|
||||
this._typeMapping = typeMapping;
|
||||
}
|
||||
|
||||
private _setState(
|
||||
isReadonly: boolean | undefined,
|
||||
) {
|
||||
this._isReadonly &&= isReadonly;
|
||||
}
|
||||
|
||||
addCommand(
|
||||
isReadonly: boolean | undefined,
|
||||
args: CommandArguments,
|
||||
transformReply?: TransformReply
|
||||
) {
|
||||
this._setState(isReadonly);
|
||||
this._multi.addCommand(args, transformReply);
|
||||
return this;
|
||||
}
|
||||
|
||||
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false) {
|
||||
if (execAsPipeline) return this.execAsPipeline<T>();
|
||||
|
||||
return this._multi.transformReplies(
|
||||
await this._sentinel._executeMulti(
|
||||
this._isReadonly,
|
||||
this._multi.queue
|
||||
),
|
||||
this._typeMapping
|
||||
) as MultiReplyType<T, REPLIES>;
|
||||
}
|
||||
|
||||
EXEC = this.exec;
|
||||
|
||||
execTyped(execAsPipeline = false) {
|
||||
return this.exec<MULTI_REPLY['TYPED']>(execAsPipeline);
|
||||
}
|
||||
|
||||
async execAsPipeline<T extends MultiReply = MULTI_REPLY['GENERIC']>() {
|
||||
if (this._multi.queue.length === 0) return [] as MultiReplyType<T, REPLIES>;
|
||||
|
||||
return this._multi.transformReplies(
|
||||
await this._sentinel._executePipeline(
|
||||
this._isReadonly,
|
||||
this._multi.queue
|
||||
),
|
||||
this._typeMapping
|
||||
) as MultiReplyType<T, REPLIES>;
|
||||
}
|
||||
|
||||
execAsPipelineTyped() {
|
||||
return this.execAsPipeline<MULTI_REPLY['TYPED']>();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user