diff --git a/packages/client/lib/client/multi-command.ts b/packages/client/lib/client/multi-command.ts index a145c81d5a..a687655b60 100644 --- a/packages/client/lib/client/multi-command.ts +++ b/packages/client/lib/client/multi-command.ts @@ -91,38 +91,34 @@ export default class RedisClientMultiCommand { static #createCommand(command: Command, resp: RespVersions) { const transformReply = getTransformReply(command, resp); - return function (this: RedisClientMultiCommand, ...args: Array): RedisClientMultiCommand { + return function (this: RedisClientMultiCommand, ...args: Array) { const parser = new BasicCommandParser(); command.parseCommand(parser, ...args); const redisArgs: CommandArguments = parser.redisArgs; redisArgs.preserve = parser.preserve; - this.#multi.addCommand( + return this.addCommand( redisArgs, transformReply ); - - return this; }; } static #createModuleCommand(command: Command, resp: RespVersions) { const transformReply = getTransformReply(command, resp); - return function (this: { _self: RedisClientMultiCommand }, ...args: Array): RedisClientMultiCommand { + return function (this: { _self: RedisClientMultiCommand }, ...args: Array) { const parser = new BasicCommandParser(); command.parseCommand(parser, ...args); const redisArgs: CommandArguments = parser.redisArgs; redisArgs.preserve = parser.preserve; - this._self.#multi.addCommand( + return this._self.addCommand( redisArgs, transformReply ); - - return this._self; }; } @@ -130,7 +126,7 @@ export default class RedisClientMultiCommand { const prefix = functionArgumentsPrefix(name, fn); const transformReply = getTransformReply(fn, resp); - return function (this: { _self: RedisClientMultiCommand }, ...args: Array): RedisClientMultiCommand { + return function (this: { _self: RedisClientMultiCommand }, ...args: Array) { const parser = new BasicCommandParser(); parser.push(...prefix); fn.parseCommand(parser, ...args); @@ -138,12 +134,10 @@ export default class RedisClientMultiCommand { const redisArgs: CommandArguments = parser.redisArgs; redisArgs.preserve = parser.preserve; - this._self.#multi.addCommand( + return this._self.addCommand( redisArgs, transformReply ); - - return this._self; }; } @@ -157,13 +151,11 @@ export default class RedisClientMultiCommand { const redisArgs: CommandArguments = parser.redisArgs; redisArgs.preserve = parser.preserve; - this.#multi.addScript( + return this.#addScript( script, redisArgs, transformReply ); - - return this; }; } @@ -204,6 +196,21 @@ export default class RedisClientMultiCommand { select = this.SELECT; + addCommand(args: CommandArguments, transformReply?: TransformReply) { + this.#multi.addCommand(args, transformReply); + return this; + } + + #addScript( + script: RedisScript, + args: CommandArguments, + transformReply?: TransformReply + ) { + this.#multi.addScript(script, args, transformReply); + + return this; + } + async exec(execAsPipeline = false): Promise> { if (execAsPipeline) return this.execAsPipeline(); diff --git a/packages/client/lib/cluster/multi-command.ts b/packages/client/lib/cluster/multi-command.ts index aa3047bd8a..f370618ff3 100644 --- a/packages/client/lib/cluster/multi-command.ts +++ b/packages/client/lib/cluster/multi-command.ts @@ -1,6 +1,6 @@ import COMMANDS from '../commands'; import RedisMultiCommand, { MULTI_REPLY, MultiReply, MultiReplyType, RedisMultiQueuedCommand } from '../multi-command'; -import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, RedisScript, RedisFunction, TypeMapping, RedisArgument } from '../RESP/types'; +import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, TransformReply, RedisScript, RedisFunction, TypeMapping, RedisArgument } from '../RESP/types'; import { attachConfig, functionArgumentsPrefix, getTransformReply } from '../commander'; import { BasicCommandParser } from '../client/parser'; import { Tail } from '../commands/generic-transformers'; @@ -95,25 +95,27 @@ export default class RedisClusterMultiCommand { static #createCommand(command: Command, resp: RespVersions) { const transformReply = getTransformReply(command, resp); - return function (this: RedisClusterMultiCommand, ...args: Array): RedisClusterMultiCommand { + return function (this: RedisClusterMultiCommand, ...args: Array) { const parser = new BasicCommandParser(); command.parseCommand(parser, ...args); const redisArgs: CommandArguments = parser.redisArgs; redisArgs.preserve = parser.preserve; const firstKey = parser.firstKey; - - this.#setState(firstKey, command.IS_READ_ONLY); - this.#multi.addCommand(redisArgs, transformReply); - - return this; + + return this.addCommand( + firstKey, + command.IS_READ_ONLY, + redisArgs, + transformReply + ); }; } static #createModuleCommand(command: Command, resp: RespVersions) { const transformReply = getTransformReply(command, resp); - return function (this: { _self: RedisClusterMultiCommand }, ...args: Array): RedisClusterMultiCommand { + return function (this: { _self: RedisClusterMultiCommand }, ...args: Array) { const parser = new BasicCommandParser(); command.parseCommand(parser, ...args); @@ -121,10 +123,12 @@ export default class RedisClusterMultiCommand { redisArgs.preserve = parser.preserve; const firstKey = parser.firstKey; - this._self.#setState(firstKey, command.IS_READ_ONLY); - this._self.#multi.addCommand(redisArgs, transformReply); - - return this._self; + return this._self.addCommand( + firstKey, + command.IS_READ_ONLY, + redisArgs, + transformReply + ); }; } @@ -132,7 +136,7 @@ export default class RedisClusterMultiCommand { const prefix = functionArgumentsPrefix(name, fn); const transformReply = getTransformReply(fn, resp); - return function (this: { _self: RedisClusterMultiCommand }, ...args: Array): RedisClusterMultiCommand { + return function (this: { _self: RedisClusterMultiCommand }, ...args: Array) { const parser = new BasicCommandParser(); parser.push(...prefix); fn.parseCommand(parser, ...args); @@ -141,17 +145,19 @@ export default class RedisClusterMultiCommand { redisArgs.preserve = parser.preserve; const firstKey = parser.firstKey; - this._self.#setState(firstKey, fn.IS_READ_ONLY); - this._self.#multi.addCommand(redisArgs, transformReply); - - return this._self; + return this._self.addCommand( + firstKey, + fn.IS_READ_ONLY, + redisArgs, + transformReply + ); }; } static #createScriptCommand(script: RedisScript, resp: RespVersions) { const transformReply = getTransformReply(script, resp); - return function (this: RedisClusterMultiCommand, ...args: Array): RedisClusterMultiCommand { + return function (this: RedisClusterMultiCommand, ...args: Array) { const parser = new BasicCommandParser(); script.parseCommand(parser, ...args); @@ -159,10 +165,13 @@ export default class RedisClusterMultiCommand { scriptArgs.preserve = parser.preserve; const firstKey = parser.firstKey; - this.#setState(firstKey, script.IS_READ_ONLY); - this.#multi.addScript(script, scriptArgs, transformReply); - - return this; + return this.#addScript( + firstKey, + script.IS_READ_ONLY, + script, + scriptArgs, + transformReply + ); }; } @@ -183,7 +192,7 @@ export default class RedisClusterMultiCommand { }); } - readonly #multi: RedisMultiCommand; + readonly #multi: RedisMultiCommand readonly #executeMulti: ClusterMultiExecute; readonly #executePipeline: ClusterMultiExecute; @@ -210,6 +219,30 @@ export default class RedisClusterMultiCommand { this.#isReadonly &&= isReadonly; } + addCommand( + firstKey: RedisArgument | undefined, + isReadonly: boolean | undefined, + args: CommandArguments, + transformReply?: TransformReply + ) { + this.#setState(firstKey, isReadonly); + this.#multi.addCommand(args, transformReply); + return this; + } + + #addScript( + firstKey: RedisArgument | undefined, + isReadonly: boolean | undefined, + script: RedisScript, + args: CommandArguments, + transformReply?: TransformReply + ) { + this.#setState(firstKey, isReadonly); + this.#multi.addScript(script, args, transformReply); + + return this; + } + async exec(execAsPipeline = false) { if (execAsPipeline) return this.execAsPipeline(); diff --git a/packages/client/lib/sentinel/multi-commands.ts b/packages/client/lib/sentinel/multi-commands.ts index 641acb8c0f..e70dc45c79 100644 --- a/packages/client/lib/sentinel/multi-commands.ts +++ b/packages/client/lib/sentinel/multi-commands.ts @@ -1,6 +1,6 @@ import COMMANDS from '../commands'; import RedisMultiCommand, { MULTI_REPLY, MultiReply, MultiReplyType } from '../multi-command'; -import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, RedisScript, RedisFunction, TypeMapping } from '../RESP/types'; +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'; import { BasicCommandParser } from '../client/parser'; @@ -90,34 +90,36 @@ export default class RedisSentinelMultiCommand { private static _createCommand(command: Command, resp: RespVersions) { const transformReply = getTransformReply(command, resp); - return function (this: RedisSentinelMultiCommand, ...args: Array): RedisSentinelMultiCommand { + return function (this: RedisSentinelMultiCommand, ...args: Array) { const parser = new BasicCommandParser(); command.parseCommand(parser, ...args); const redisArgs: CommandArguments = parser.redisArgs; redisArgs.preserve = parser.preserve; - this.#setState(command.IS_READ_ONLY); - this.#multi.addCommand(redisArgs, transformReply); - - return this; + 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): RedisSentinelMultiCommand { + return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array) { const parser = new BasicCommandParser(); command.parseCommand(parser, ...args); const redisArgs: CommandArguments = parser.redisArgs; redisArgs.preserve = parser.preserve; - this._self.#setState(command.IS_READ_ONLY); - this._self.#multi.addCommand(redisArgs, transformReply); - - return this._self; + return this._self.addCommand( + command.IS_READ_ONLY, + redisArgs, + transformReply + ); }; } @@ -125,7 +127,7 @@ export default class RedisSentinelMultiCommand { const prefix = functionArgumentsPrefix(name, fn); const transformReply = getTransformReply(fn, resp); - return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array): RedisSentinelMultiCommand { + return function (this: { _self: RedisSentinelMultiCommand }, ...args: Array) { const parser = new BasicCommandParser(); parser.push(...prefix); fn.parseCommand(parser, ...args); @@ -133,27 +135,30 @@ export default class RedisSentinelMultiCommand { const redisArgs: CommandArguments = parser.redisArgs; redisArgs.preserve = parser.preserve; - this._self.#setState(fn.IS_READ_ONLY); - this._self.#multi.addCommand(redisArgs, transformReply); - - return this._self; + 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): RedisSentinelMultiCommand { + return function (this: RedisSentinelMultiCommand, ...args: Array) { const parser = new BasicCommandParser(); script.parseCommand(parser, ...args); const scriptArgs: CommandArguments = parser.redisArgs; scriptArgs.preserve = parser.preserve; - this.#setState(script.IS_READ_ONLY); - this.#multi.addScript(script, scriptArgs, transformReply); - - return this; + return this.#addScript( + script.IS_READ_ONLY, + script, + scriptArgs, + transformReply + ); }; } @@ -189,6 +194,28 @@ export default class RedisSentinelMultiCommand { this.#isReadonly &&= isReadonly; } + addCommand( + isReadonly: boolean | undefined, + args: CommandArguments, + transformReply?: TransformReply + ) { + this.#setState(isReadonly); + this.#multi.addCommand(args, transformReply); + return this; + } + + #addScript( + isReadonly: boolean | undefined, + script: RedisScript, + args: CommandArguments, + transformReply?: TransformReply + ) { + this.#setState(isReadonly); + this.#multi.addScript(script, args, transformReply); + + return this; + } + async exec(execAsPipeline = false) { if (execAsPipeline) return this.execAsPipeline();