From 5e59ebcc6b27e2275fc4d0233d4750cf3a109e3e Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Tue, 17 Oct 2023 16:43:46 +0300 Subject: [PATCH 1/5] Allow mget to accept both array and spreaded array --- pkg/redis.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/redis.ts b/pkg/redis.ts index 0d5d1fb4..ef8de185 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -416,7 +416,9 @@ export class Redis { new BitOpCommand( [op as any, destinationKey, sourceKey, ...sourceKeys], this.opts, - ).exec(this.client); + ).exec( + this.client, + ); /** * @see https://redis.io/commands/bitpos @@ -604,10 +606,8 @@ export class Redis { count?: number, withValues?: boolean, ) => - new HRandFieldCommand( - [key, count, withValues] as any, - this.opts, - ).exec(this.client); + new HRandFieldCommand([key, count, withValues] as any, this.opts) + .exec(this.client); /** * @see https://redis.io/commands/hscan @@ -745,8 +745,14 @@ export class Redis { /** * @see https://redis.io/commands/mget */ - mget = (...args: CommandArgs) => - new MGetCommand(args, this.opts).exec(this.client); + mget = ( + ...args: (string | string[])[] + ): Promise => { + const queries = Array.isArray(args[0]) + ? (args[0] as string[]) + : (args as string[]); + return new MGetCommand(queries, this.opts).exec(this.client); + }; /** * @see https://redis.io/commands/mset From cb226d49753345de0dd1f53744224863ffd04a58 Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Tue, 17 Oct 2023 19:40:48 +0300 Subject: [PATCH 2/5] Add tests for mget to redis.test --- pkg/redis.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/redis.test.ts b/pkg/redis.test.ts index c50ef649..9497c969 100644 --- a/pkg/redis.test.ts +++ b/pkg/redis.test.ts @@ -37,6 +37,29 @@ Deno.test("when storing base64 data", async (t) => { }); }); +Deno.test("mget", async (t) => { + const key = newKey(); + const key1 = newKey(); + const value = "foobar"; + const value1 = "foobar1"; + const redis = new Redis(client); + const queries = [key, key1]; + + await t.step("mget with array", async () => { + await redis.mset({ key: value, key1: value1 }); + const res = await redis.mget(queries); + + assertEquals(res.length, 2); + }); + + await t.step("mget with spreaded array", async () => { + await redis.mset({ key: value, key1: value1 }); + const res = await redis.mget(...queries); + + assertEquals(res.length, 2); + }); +}); + Deno.test("when destructuring the redis class", async (t) => { await t.step("correctly binds this", async () => { const { get, set } = new Redis(client); From 32ca69f5cbce26af31d94ff206a8b612292764a1 Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Wed, 18 Oct 2023 12:09:52 +0300 Subject: [PATCH 3/5] Move allowing array or spreaded values into mget command --- pkg/commands/mget.ts | 13 +++++++------ pkg/redis.ts | 7 ++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pkg/commands/mget.ts b/pkg/commands/mget.ts index 2380751a..feaf16ba 100644 --- a/pkg/commands/mget.ts +++ b/pkg/commands/mget.ts @@ -2,14 +2,15 @@ import { Command, CommandOptions } from "./command.ts"; /** * @see https://redis.io/commands/mget */ -export class MGetCommand extends Command< - (string | null)[], - TData -> { +export class MGetCommand + extends Command<(string | null)[], TData> { constructor( - cmd: [...keys: string[]], + cmd: [...keys: (string | string[])[]], opts?: CommandOptions<(string | null)[], TData>, ) { - super(["mget", ...cmd], opts); + const keys = Array.isArray(cmd[0]) + ? (cmd[0] as string[]) + : (cmd as string[]); + super(["mget", ...keys], opts); } } diff --git a/pkg/redis.ts b/pkg/redis.ts index ef8de185..1d51985e 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -746,12 +746,9 @@ export class Redis { * @see https://redis.io/commands/mget */ mget = ( - ...args: (string | string[])[] + ...args: CommandArgs ): Promise => { - const queries = Array.isArray(args[0]) - ? (args[0] as string[]) - : (args as string[]); - return new MGetCommand(queries, this.opts).exec(this.client); + return new MGetCommand(args, this.opts).exec(this.client); }; /** From 5080841787272e77d3cdc17d02f421e96ffc0a54 Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Wed, 18 Oct 2023 12:12:58 +0300 Subject: [PATCH 4/5] Revert mget change in redis file --- pkg/redis.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pkg/redis.ts b/pkg/redis.ts index 1d51985e..03d17bcc 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -745,11 +745,8 @@ export class Redis { /** * @see https://redis.io/commands/mget */ - mget = ( - ...args: CommandArgs - ): Promise => { - return new MGetCommand(args, this.opts).exec(this.client); - }; + mget = (...args: CommandArgs) => + new MGetCommand(args, this.opts).exec(this.client); /** * @see https://redis.io/commands/mset From 82032a385d19ce4a8c555adb6db85d582afb0cb7 Mon Sep 17 00:00:00 2001 From: chronark Date: Thu, 19 Oct 2023 11:27:54 +0200 Subject: [PATCH 5/5] refactor: clean up command --- pkg/commands/mget.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/mget.ts b/pkg/commands/mget.ts index feaf16ba..ba716210 100644 --- a/pkg/commands/mget.ts +++ b/pkg/commands/mget.ts @@ -5,7 +5,7 @@ import { Command, CommandOptions } from "./command.ts"; export class MGetCommand extends Command<(string | null)[], TData> { constructor( - cmd: [...keys: (string | string[])[]], + cmd: [string[]] | [...string[] | string[]], opts?: CommandOptions<(string | null)[], TData>, ) { const keys = Array.isArray(cmd[0])