Skip to content

Commit

Permalink
added select command in node
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan Wattad authored Aug 28, 2023
1 parent 618782c commit a9dce4f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 25 deletions.
6 changes: 6 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
11 changes: 11 additions & 0 deletions node/src/RedisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
createDel,
createGet,
createInfo,
createSelect,
createSet
} from "./Commands";
import { Logger } from "./Logger";
Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 14 additions & 1 deletion node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createDel,
createGet,
createInfo,
createSelect,
createSet
} from "./Commands";
import { redis_request } from "./ProtobufMessage";
Expand Down Expand Up @@ -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
}
47 changes: 46 additions & 1 deletion node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<Context>({
init: async () => {
const client = await RedisClient.createClient(getOptions(port));
Expand Down
23 changes: 0 additions & 23 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -13,7 +12,6 @@ type BaseClient = {
get: (key: string) => Promise<string | null>;
del: (keys: string[]) => Promise<number>;
customCommand: (commandName: string, args: string[]) => Promise<ReturnType>;
exec: (transaction: BaseTransaction) => Promise<ReturnType>;
};

export function runBaseTests<Context>(config: {
Expand Down Expand Up @@ -141,27 +139,6 @@ export function runBaseTests<Context>(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 () => {
Expand Down

0 comments on commit a9dce4f

Please sign in to comment.