Skip to content

Commit

Permalink
Node: implement GETDEL (valkey-io#1968)
Browse files Browse the repository at this point in the history
Signed-off-by: Chloe Yip <[email protected]>
  • Loading branch information
cyip10 authored Jul 18, 2024
1 parent f19f4b1 commit bc0e1b1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Node: Added GETDEL command ([#1968](https://github.com/valkey-io/valkey-glide/pull/1968))
* Node: Added LPUSHX and RPUSHX command([#1959](https://github.com/valkey-io/valkey-glide/pull/1959))
* Node: Added LSET command ([#1952](https://github.com/valkey-io/valkey-glide/pull/1952))
* Node: Added SDIFFSTORE command ([#1931](https://github.com/valkey-io/valkey-glide/pull/1931))
Expand Down
21 changes: 21 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
createExpire,
createExpireAt,
createGet,
createGetDel,
createHDel,
createHExists,
createHGet,
Expand Down Expand Up @@ -745,6 +746,26 @@ export class BaseClient {
return this.createWritePromise(createGet(key));
}

/**
* Gets a string value associated with the given `key`and deletes the key.
*
* See https://valkey.io/commands/getdel/ for details.
*
* @param key - The key to retrieve from the database.
* @returns If `key` exists, returns the `value` of `key`. Otherwise, return `null`.
*
* @example
* ```typescript
* const result = client.getdel("key");
* console.log(result); // Output: 'value'
*
* const value = client.getdel("key"); // value is null
* ```
*/
public getdel(key: string): Promise<string | null> {
return this.createWritePromise(createGetDel(key));
}

/** Set the given key with the given value. Return value is dependent on the passed options.
* See https://valkey.io/commands/set/ for details.
*
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ export function createGet(key: string): command_request.Command {
return createCommand(RequestType.Get, [key]);
}

/**
* @internal
*/
export function createGetDel(key: string): command_request.Command {
return createCommand(RequestType.GetDel, [key]);
}

export type SetOptions = {
/**
* `onlyIfDoesNotExist` - Only set the key if it does not already exist.
Expand Down
14 changes: 14 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
createExpireAt,
createFlushAll,
createGet,
createGetDel,
createHDel,
createHExists,
createHGet,
Expand Down Expand Up @@ -189,6 +190,19 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createGet(key));
}

/**
* Gets a string value associated with the given `key`and deletes the key.
*
* See https://valkey.io/commands/getdel/ for details.
*
* @param key - The key to retrieve from the database.
*
* Command Response - If `key` exists, returns the `value` of `key`. Otherwise, return `null`.
*/
public getdel(key: string): T {
return this.addAndReturn(createGetDel(key));
}

/** Set the given key with the given value. Return value is dependent on the passed options.
* See https://valkey.io/commands/set/ for details.
*
Expand Down
20 changes: 20 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,26 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`getdel test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
const value1 = uuidv4();
const key2 = uuidv4();

expect(await client.set(key1, value1)).toEqual("OK");
checkSimple(await client.getdel(key1)).toEqual(value1);
expect(await client.getdel(key1)).toEqual(null);

// key isn't a string
expect(await client.sadd(key2, ["a"])).toEqual(1);
await expect(client.getdel(key2)).rejects.toThrow(RequestError);
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`testing hset and hget with multiple existing fields and one non existing field_%p`,
async (protocol) => {
Expand Down
4 changes: 4 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ export async function transactionTest(
args.push(0);
baseTransaction.set(key1, "bar");
args.push("OK");
baseTransaction.getdel(key1);
args.push("bar");
baseTransaction.set(key1, "bar");
args.push("OK");
baseTransaction.objectEncoding(key1);
args.push("embstr");
baseTransaction.type(key1);
Expand Down

0 comments on commit bc0e1b1

Please sign in to comment.