From 567ae381b0b5e6ade455c19264b0433881de20f6 Mon Sep 17 00:00:00 2001 From: Leibale Date: Tue, 25 Apr 2023 09:48:11 -0400 Subject: [PATCH] "typed" multi --- md/v5-new-features.md | 17 ++++------- packages/client/lib/client/multi-command.ts | 33 ++++++++++++++------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/md/v5-new-features.md b/md/v5-new-features.md index 56dd12b49c..bfac25315b 100644 --- a/md/v5-new-features.md +++ b/md/v5-new-features.md @@ -21,20 +21,15 @@ client.withFlags({ }).hGetAll('key'); // Map ``` -# `Multi.exec<'typed'>` +# `multi.exec<'typed'>` / `multi.execTyped` -We have introduced the ability to perform a 'typed' `MULTI`/`EXEC` transaction. Rather than returning `Array`, a transaction invoked with `.exec<'typed'>` will return types appropriate to the commands in the transaction where possible. - -Example: +We have introduced the ability to perform a "typed" `MULTI`/`EXEC` transaction. Rather than returning `Array`, a transaction invoked with `.exec<'typed'>` will return types appropriate to the commands in the transaction where possible: ```javascript -client.multi() - .ping() - .exec(); // Array - -client.multi() - .ping() - .exec<'typed'>(); // [string] +const multi = client.multi().ping(); +await multi.exec(); // Array +await multi.exec<'typed'>(); // [string] +await multi.execTyped(); // [string] ``` # Request & Reply Policies diff --git a/packages/client/lib/client/multi-command.ts b/packages/client/lib/client/multi-command.ts index b52190c10a..3503a34af2 100644 --- a/packages/client/lib/client/multi-command.ts +++ b/packages/client/lib/client/multi-command.ts @@ -83,10 +83,14 @@ export type RedisClientMultiCommandType< WithScripts ); -type ReplyType< - REPLIES, - T = 'generic' -> = T extends 'typed' ? REPLIES : Array; +type MULTI_REPLY = { + GENERIC: 'generic'; + TYPED: 'typed'; +}; + +type MultiReply = MULTI_REPLY[keyof MULTI_REPLY]; + +type ReplyType = T extends MULTI_REPLY['TYPED'] ? REPLIES : Array; export type RedisClientMultiExecutor = ( queue: Array, @@ -144,8 +148,7 @@ export default class RedisClientMultiCommand extends RedisMultiCom M extends RedisModules = Record, F extends RedisFunctions = Record, S extends RedisScripts = Record, - RESP extends RespVersions = 2, - FLAGS extends Flags = {} + RESP extends RespVersions = 2 >(config?: CommanderConfig) { return attachConfig({ BaseClass: RedisClientMultiCommand, @@ -221,7 +224,7 @@ export default class RedisClientMultiCommand extends RedisMultiCom select = this.SELECT; - async exec(execAsPipeline = false) { + async exec(execAsPipeline = false) { if (execAsPipeline) return this.execAsPipeline(); return this.handleExecReplies( @@ -230,19 +233,27 @@ export default class RedisClientMultiCommand extends RedisMultiCom this.#selectedDB, RedisMultiCommand.generateChainId() ) - ) as ReplyType; + ) as ReplyType; } EXEC = this.exec; - async execAsPipeline() { - if (this.queue.length === 0) return [] as ReplyType; + execTyped(execAsPipeline = false) { + return this.exec(execAsPipeline); + } + + async execAsPipeline() { + if (this.queue.length === 0) return [] as ReplyType; return this.transformReplies( await this.#executor( this.queue, this.#selectedDB ) - ) as ReplyType; + ) as ReplyType; + } + + execAsPipelineTyped() { + return this.execAsPipeline(); } }