Skip to content

Commit

Permalink
Node: added type command. (valkey-io#980)
Browse files Browse the repository at this point in the history
* Node: added type command.

* update the CHANGELOG.MD file
  • Loading branch information
Adan Wattad authored Feb 16, 2024
1 parent 1b09b1c commit 10cdf60
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Python, Node: Added RPOPCOUNT and LPOPCOUNT to transaction ([#874](https://github.com/aws/glide-for-redis/pull/874))
* Standalone client: Improve connection errors. ([#854](https://github.com/aws/glide-for-redis/pull/854))
* Python, Node: When recieving LPOP/RPOP with count, convert result to Array. ([#811](https://github.com/aws/glide-for-redis/pull/811))
* Python: Added TYPE command ([#945](https://github.com/aws/glide-for-redis/pull/945))
* Python, Node: Added TYPE command ([#945](https://github.com/aws/glide-for-redis/pull/945), [#980](https://github.com/aws/glide-for-redis/pull/980))
* Python: Added HLEN command ([#944](https://github.com/aws/glide-for-redis/pull/944))
* Node: Added ZCOUNT command ([#909](https://github.com/aws/glide-for-redis/pull/909))

Expand Down
11 changes: 11 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
createSRem,
createSet,
createTTL,
createType,
createUnlink,
createZadd,
createZcard,
Expand Down Expand Up @@ -1065,6 +1066,16 @@ export class BaseClient {
return this.createWritePromise(createZcount(key, minScore, maxScore));
}

/** Returns the string representation of the type of the value stored at `key`.
* See https://redis.io/commands/type/ for more details.
*
* @param key - The key to check its data type.
* @returns If the key exists, the type of the stored value is returned. Otherwise, a "none" string is returned.
*/
public type(key: string): Promise<string> {
return this.createWritePromise(createType(key));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,10 @@ export function createZcount(

return createCommand(RequestType.Zcount, args);
}

/**
* @internal
*/
export function createType(key: string): redis_request.Command {
return createCommand(RequestType.Type, [key]);
}
12 changes: 12 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
createSelect,
createSet,
createTTL,
createType,
createUnlink,
createZadd,
createZcard,
Expand Down Expand Up @@ -828,6 +829,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createZcount(key, minScore, maxScore));
}

/** Returns the string representation of the type of the value stored at `key`.
* See https://redis.io/commands/type/ for more details.
*
* @param key - The key to check its data type.
*
* Command Response - If the key exists, the type of the stored value is returned. Otherwise, a "none" string is returned.
*/
public type(key: string): T {
return this.addAndReturn(createType(key));
}

/** 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
41 changes: 41 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,47 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`type test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
expect(await client.set(key, "value")).toEqual("OK");
expect(await client.type(key)).toEqual("string");
expect(await client.del([key])).toEqual(1);

expect(await client.lpush(key, ["value"])).toEqual(1);
expect(await client.type(key)).toEqual("list");
expect(await client.del([key])).toEqual(1);

expect(await client.sadd(key, ["value"])).toEqual(1);
expect(await client.type(key)).toEqual("set");
expect(await client.del([key])).toEqual(1);

expect(await client.zadd(key, { member: 1.0 })).toEqual(1);
expect(await client.type(key)).toEqual("zset");
expect(await client.del([key])).toEqual(1);

expect(await client.hset(key, { field: "value" })).toEqual(1);
expect(await client.type(key)).toEqual("hash");
expect(await client.del([key])).toEqual(1);

await client.customCommand([
"XADD",
key,
"*",
"field",
"value",
]);
expect(await client.type(key)).toEqual("stream");
expect(await client.del([key])).toEqual(1);

expect(await client.type(key)).toEqual("none");
}, protocol);
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export function transactionTest(
const args: ReturnType[] = [];
baseTransaction.set(key1, "bar");
args.push("OK");
baseTransaction.type(key1);
args.push("string");
baseTransaction.set(key2, "baz", {
returnOldValue: true,
});
Expand Down

0 comments on commit 10cdf60

Please sign in to comment.