From a9dce4f461c87a0c01b50fce60140e0642c6881b Mon Sep 17 00:00:00 2001 From: Adan Wattad Date: Mon, 28 Aug 2023 14:20:57 +0300 Subject: [PATCH] added select command in node --- node/src/Commands.ts | 6 +++++ node/src/RedisClient.ts | 11 ++++++++ node/src/Transaction.ts | 15 ++++++++++- node/tests/RedisClient.test.ts | 47 +++++++++++++++++++++++++++++++++- node/tests/SharedTests.ts | 23 ----------------- 5 files changed, 77 insertions(+), 25 deletions(-) diff --git a/node/src/Commands.ts b/node/src/Commands.ts index a8ad29fd74..3a50f618fb 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -157,6 +157,12 @@ export function createDel( return createCommand(RequestType.Del, keys); } +export function createSelect( + index: number +): redis_request.Command { + return createCommand(RequestType.Select, [index.toString()]); +} + export function createCustomCommand(commandName: string, args: string[]) { return createCommand(RequestType.CustomCommand, [commandName, ...args]); } diff --git a/node/src/RedisClient.ts b/node/src/RedisClient.ts index a6593db474..46f7253524 100644 --- a/node/src/RedisClient.ts +++ b/node/src/RedisClient.ts @@ -12,6 +12,7 @@ import { createDel, createGet, createInfo, + createSelect, createSet } from "./Commands"; import { Logger } from "./Logger"; @@ -252,6 +253,16 @@ export class RedisClient { return this.createWritePromise(createDel(keys)); } + /** Change the currently selected Redis database. + * See https://redis.io/commands/select/ for details. + * + * @param index : The index of the database to select. + * @returns A simple OK response. + */ + public select(index: number): Promise<"OK"> { + return this.createWritePromise(createSelect(index)); + } + /** Executes a single command, without checking inputs. Every part of the command, including subcommands, * should be added as a separate value in args. * diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 2326d0e171..e945c3505a 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -5,6 +5,7 @@ import { createDel, createGet, createInfo, + createSelect, createSet } from "./Commands"; import { redis_request } from "./ProtobufMessage"; @@ -52,10 +53,22 @@ export class BaseTransaction { } } +/// Extends BaseTransaction class for Redis standalone commands. export class Transaction extends BaseTransaction{ - /// TODO: add SELECT, MOVE, SLAVEOF and all SENTINEL commands + /// TODO: add MOVE, SLAVEOF and all SENTINEL commands + + /** Change the currently selected Redis database. + * See https://redis.io/commands/select/ for details. + * + * @param index : The index of the database to select. + * @CommandResponse : A simple OK response. + */ + public select(index: number) { + this.commands.push(createSelect(index)); + } } +/// Extends BaseTransaction class for cluster mode commands. export class ClusterTransaction extends BaseTransaction{ /// TODO: add all CLUSTER commands } diff --git a/node/tests/RedisClient.test.ts b/node/tests/RedisClient.test.ts index 78f4a35ea3..b0e2fedc5d 100644 --- a/node/tests/RedisClient.test.ts +++ b/node/tests/RedisClient.test.ts @@ -8,7 +8,8 @@ import { } from "@jest/globals"; import { BufferReader, BufferWriter } from "protobufjs"; import RedisServer from "redis-server"; -import { ConnectionOptions, RedisClient } from "../build-ts"; +import { v4 as uuidv4 } from "uuid"; +import { ConnectionOptions, RedisClient, Transaction } from "../build-ts"; import { redis_request } from "../src/ProtobufMessage"; import { runBaseTests } from "./SharedTests"; import { flushallOnPort } from "./TestUtilities"; @@ -113,6 +114,50 @@ describe("RedisClient", () => { } ); + it( + "simple select test", + async () => { + const client = await RedisClient.createClient(getOptions(port)); + let selectResult = await client.select(0); + expect(selectResult).toEqual("OK"); + + const key = uuidv4(); + const value = uuidv4(); + const result = await client.set(key, value); + expect(result).toEqual("OK"); + + selectResult = await client.select(1); + expect(selectResult).toEqual("OK"); + expect(await client.get(key)).toEqual(null); + + selectResult = await client.select(0); + expect(selectResult).toEqual("OK"); + expect(await client.get(key)).toEqual(value); + client.dispose(); + }, + ); + + it( + "can send transactions", + async () => { + const client = await RedisClient.createClient(getOptions(port)); + const key1 = "{key}" + uuidv4(); + const key2 = "{key}" + uuidv4(); + const transaction = new Transaction(); + transaction.set(key1, "bar"); + transaction.set(key2, "baz", { + conditionalSet: "onlyIfDoesNotExist", + returnOldValue: true, + }); + transaction.customCommand("MGET", [key1, key2]); + transaction.select(0); + + const result = await client.exec(transaction); + expect(result).toEqual(["OK", null, ["bar", "baz"], "OK"]); + client.dispose(); + }, + ); + runBaseTests({ init: async () => { const client = await RedisClient.createClient(getOptions(port)); diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index b71d45450c..91f0ef7829 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -1,7 +1,6 @@ import { expect, it } from "@jest/globals"; import { v4 as uuidv4 } from "uuid"; import { ReturnType, SetOptions } from "../"; -import { BaseTransaction } from "../src/Transaction"; import { Client, GetAndSetRandomValue } from "./TestUtilities"; type BaseClient = { @@ -13,7 +12,6 @@ type BaseClient = { get: (key: string) => Promise; del: (keys: string[]) => Promise; customCommand: (commandName: string, args: string[]) => Promise; - exec: (transaction: BaseTransaction) => Promise; }; export function runBaseTests(config: { @@ -141,27 +139,6 @@ export function runBaseTests(config: { config.timeout ); - it( - "can send transactions", - async () => { - await runTest(async (client: BaseClient) => { - const key1 = "{key}" + uuidv4(); - const key2 = "{key}" + uuidv4(); - const transaction = new BaseTransaction(); - transaction.set(key1, "bar"); - transaction.set(key2, "baz", { - conditionalSet: "onlyIfDoesNotExist", - returnOldValue: true, - }); - transaction.customCommand("MGET", [key1, key2]); - - const result = await client.exec(transaction); - expect(result).toEqual(["OK", null, ["bar", "baz"]]); - }); - }, - config.timeout - ); - it( "delete multiple existing keys and an non existing key", async () => {