diff --git a/CHANGELOG.md b/CHANGELOG.md index a4e304e30e..657b747ed4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index 5af5ce1ade..b946972bc5 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -34,6 +34,7 @@ import { createExpire, createExpireAt, createGet, + createGetDel, createHDel, createHExists, createHGet, @@ -743,6 +744,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 { + 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. * diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 461bd4f155..a3ddfccc26 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -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. diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index d2f637f77e..7beb57a922 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -39,6 +39,7 @@ import { createExpireAt, createFlushAll, createGet, + createGetDel, createHDel, createHExists, createHGet, @@ -187,6 +188,19 @@ export class BaseTransaction> { 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. * diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index 470c60a77e..248c437fef 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -513,6 +513,26 @@ export function runBaseTests(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) => { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index d66ede6145..e58f27e79e 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -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);