Skip to content

Commit

Permalink
added rpush and rpop commands in node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Oct 17, 2023
1 parent 72f7564 commit bbffd95
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
35 changes: 35 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
createIncrByFloat,
createMGet,
createMSet,
createRPop,
createRPush,
createSet,
} from "./Commands";
import {
Expand Down Expand Up @@ -518,6 +520,39 @@ export class BaseClient {
return this.createWritePromise(createHIncrByFloat(key, field, amount));
}

/** Inserts all the specified values at the tail of the list stored at `key`.
* `elements` are inserted one after the other to the tail of the list, from the leftmost element to the rightmost element.
* If `key` does not exist, it is created as empty list before performing the push operations.
* See https://redis.io/commands/rpush/ for details.
*
* @param key - The key of the list.
* @param elements - The elements to insert at the tail of the list stored at `key`.
* @returns the length of the list after the push operations.
* If `key` holds a value that is not a list, an error is returned.
*/
public rpush(key: string, elements: string[]): Promise<number> {
return this.createWritePromise(createRPush(key, elements));
}

/** Removes and returns the last elements of the list stored at `key`.
* By default, the command pops a single element from the end of the list.
* When `count` is provided, the command pops up to `count` elements, depending on the list's length.
* See https://redis.io/commands/rpop/ for details.
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
* @returns the value of the last element.
* If `count` is provided, list of popped elements will be returned depending on the list's length.
* If `key` does not exist null will be returned.
* If `key` holds a value that is not a list, an error is returned.
*/
public rpop(
key: string,
count?: number
): Promise<string | string[] | null> {
return this.createWritePromise(createRPop(key, count));
}

private readonly MAP_READ_FROM_REPLICA_STRATEGY: Record<
ReadFromReplicaStrategy,
connection_request.ReadFromReplicaStrategy
Expand Down
12 changes: 12 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ export function createHGetAll(key: string): redis_request.Command {
return createCommand(RequestType.HashGetAll, [key]);
}

export function createRPush(
key: string,
elements: string[]
): redis_request.Command {
return createCommand(RequestType.RPush, [key].concat(elements));
}

export function createRPop(key: string, count?: number): redis_request.Command {
const args: string[] = count == undefined ? [key] : [key, count.toString()];
return createCommand(RequestType.RPop, args);
}

export function createCustomCommand(commandName: string, args: string[]) {
return createCommand(RequestType.CustomCommand, [commandName, ...args]);
}
Expand Down
34 changes: 34 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
createMGet,
createMSet,
createPing,
createRPop,
createRPush,
createSelect,
createSet,
} from "./Commands";
Expand Down Expand Up @@ -378,6 +380,38 @@ export class BaseTransaction {
this.commands.push(createHIncrByFloat(key, field, amount));
}

/** Inserts all the specified values at the tail of the list stored at `key`.
* `elements` are inserted one after the other to the tail of the list, from the leftmost element to the rightmost element.
* If `key` does not exist, it is created as empty list before performing the push operations.
* See https://redis.io/commands/rpush/ for details.
*
* @param key - The key of the list.
* @param elements - The elements to insert at the tail of the list stored at `key`.
*
* Command Response - the length of the list after the push operations.
* If `key` holds a value that is not a list, an error is returned.
*/
public rpush(key: string, elements: string[]) {
this.commands.push(createRPush(key, elements));
}

/** Removes and returns the last elements of the list stored at `key`.
* By default, the command pops a single element from the end of the list.
* When `count` is provided, the command pops up to `count` elements, depending on the list's length.
* See https://redis.io/commands/rpop/ for details.
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
*
* Command Response - the value of the last element.
* If `count` is provided, list of popped elements will be returned depending on the list's length.
* If `key` does not exist null will be returned.
* If `key` holds a value that is not a list, an error is returned.
*/
public rpop(key: string, count?: number) {
this.commands.push(createRPop(key, count));
}

/** 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
44 changes: 44 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type BaseClient = {
field: string,
amount: number
) => Promise<string>;
rpush: (key: string, elements: string[]) => Promise<number>;
rpop: (key: string, count?: number) => Promise<string | string[] | null>;
customCommand: (commandName: string, args: string[]) => Promise<ReturnType>;
};

Expand Down Expand Up @@ -692,6 +694,48 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it(
"rpush and rpop with existing and non existing key",
async () => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
const valueList = ["value1", "value2", "value3", "value4"];
expect(await client.rpush(key, valueList)).toEqual(4);
expect(await client.rpop(key)).toEqual("value4");
expect(await client.rpop(key, 2)).toEqual(["value3", "value2"]);
expect(await client.rpop("nonExistingKey")).toEqual(null);
});
},
config.timeout
);

it(
"rpush and rpop with key that holds a value that is not a list",
async () => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
expect(await client.set(key, "foo")).toEqual("OK");

try {
expect(await client.rpush(key, ["bar"])).toThrow();
} catch (e) {
expect((e as Error).message).toMatch(
"Operation against a key holding the wrong kind of value"
);
}

try {
expect(await client.rpop(key)).toThrow();
} catch (e) {
expect((e as Error).message).toMatch(
"Operation against a key holding the wrong kind of value"
);
}
});
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
5 changes: 5 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function transactionTest(
const key2 = "{key}" + uuidv4();
const key3 = "{key}" + uuidv4();
const key4 = "{key}" + uuidv4();
const key5 = "{key}" + uuidv4();
const field = uuidv4();
const value = uuidv4();
baseTransaction.set(key1, "bar");
Expand All @@ -70,6 +71,8 @@ export function transactionTest(
baseTransaction.hdel(key4, [field]);
baseTransaction.hmget(key4, [field]);
baseTransaction.hexists(key4, field);
baseTransaction.rpush(key5, [field + "1", field + "2"]);
baseTransaction.rpop(key5);
return [
"OK",
null,
Expand All @@ -83,5 +86,7 @@ export function transactionTest(
1,
[null],
0,
2,
field + "2",
];
}

0 comments on commit bbffd95

Please sign in to comment.