Skip to content

Commit 0e1f7d8

Browse files
Merge pull request #7 from Aditya-ds-1806/release/dicedb-driver-v1.3.0
Release: v1.3.0
2 parents 721e424 + d9c0d75 commit 0e1f7d8

File tree

7 files changed

+135
-6
lines changed

7 files changed

+135
-6
lines changed

index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* --------------------------------------------------------------
44
* This file was automatically generated.
55
* Source: build.ts
6-
* Date: 2025-04-19T21:44:43.742Z
6+
* Date: 2025-04-19T22:01:56.203Z
77
*
88
* ⚠️ DO NOT MODIFY THIS FILE MANUALLY ⚠️
99
* Changes will be overwritten the next time it is built.
@@ -286,6 +286,17 @@ class DiceDB extends DiceDBBase {
286286
}
287287

288288

289+
/**
290+
* Get all keys matching a pattern
291+
*
292+
* @param {string} pattern - The pattern to match keys against
293+
* @returns A promise that resolves with the list of matching keys
294+
*/
295+
async keys(pattern: string) {
296+
return this.execCommand('KEYS', pattern) as Promise<DiceDBResponse>;
297+
}
298+
299+
289300
/**
290301
* Executes the PING command to test the connection to the server.
291302
*
@@ -348,7 +359,7 @@ class DiceDB extends DiceDBBase {
348359
* options for adding elements.
349360
*
350361
* @param {string} key - The key of the sorted set.
351-
* @param {Record<string, number | string>} map - A map of members and their scores.
362+
* @param {Record<string, number | string> | Map<string, number | string>} map - A map of members and their scores.
352363
* @param opts - The options for adding elements to the sorted set.
353364
* @returns A promise that resolves when the elements are added successfully.
354365
*/

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dicedb-driver",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"main": "./dist/index.cjs",
55
"module": "./dist/index.js",
66
"scripts": {

src/commands/Keys.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Command from "../../lib/Command";
2+
import { validateKey } from "../../lib/Validators";
3+
import { COMMANDS } from "../constants/commands";
4+
5+
export default class KeysCommand extends Command {
6+
static get command() {
7+
return COMMANDS.KEYS;
8+
}
9+
10+
/**
11+
* Get all keys matching a pattern
12+
*
13+
* @param {string} pattern - The pattern to match keys against
14+
* @returns A promise that resolves with the list of matching keys
15+
*/
16+
async exec(pattern: string) {
17+
validateKey(pattern);
18+
return super.exec(pattern);
19+
}
20+
};

src/constants/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const COMMANDS = Object.freeze({
2121
HSET: 'HSET',
2222
INCR: 'INCR',
2323
INCRBY: 'INCRBY',
24+
KEYS: 'KEYS',
2425
PING: 'PING',
2526
SET: 'SET',
2627
TTL: 'TTL',
@@ -61,6 +62,7 @@ export const COMMAND_TO_COMMAND_NAME = Object.freeze({
6162
[COMMANDS.HSET]: 'hSet',
6263
[COMMANDS.INCR]: 'increment',
6364
[COMMANDS.INCRBY]: 'incrementBy',
65+
[COMMANDS.KEYS]: 'keys',
6466
[COMMANDS.PING]: 'ping',
6567
[COMMANDS.SET]: 'set',
6668
[COMMANDS.TTL]: 'ttl',

src/registry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* --------------------------------------------------------------
44
* This file was automatically generated.
55
* Source: build.ts
6-
* Date: 2025-04-19T21:44:44.059Z
6+
* Date: 2025-04-19T22:01:56.519Z
77
*
88
* ⚠️ DO NOT MODIFY THIS FILE MANUALLY ⚠️
99
* Changes will be overwritten the next time it is built.
@@ -36,6 +36,7 @@ import HSetCommand from './commands/HSet';
3636
import HandshakeCommand from './commands/Handshake';
3737
import IncrementCommand from './commands/Increment';
3838
import IncrementByCommand from './commands/IncrementBy';
39+
import KeysCommand from './commands/Keys';
3940
import PingCommand from './commands/Ping';
4041
import SetCommand from './commands/Set';
4142
import TTLCommand from './commands/TTL';
@@ -78,6 +79,7 @@ commandRegistry.set(HSetCommand.command, HSetCommand);
7879
commandRegistry.set(HandshakeCommand.command, HandshakeCommand);
7980
commandRegistry.set(IncrementCommand.command, IncrementCommand);
8081
commandRegistry.set(IncrementByCommand.command, IncrementByCommand);
82+
commandRegistry.set(KeysCommand.command, KeysCommand);
8183
commandRegistry.set(PingCommand.command, PingCommand);
8284
commandRegistry.set(SetCommand.command, SetCommand);
8385
commandRegistry.set(TTLCommand.command, TTLCommand);

tests/index.test.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,6 +2674,100 @@ describe('DiceDB test cases', () => {
26742674
});
26752675
});
26762676

2677+
describe('KeysCommand', () => {
2678+
beforeEach(async () => {
2679+
try {
2680+
// Clear any existing keys
2681+
await db.flushDB();
2682+
2683+
// Set up test data
2684+
await db.set('user:1', 'John');
2685+
await db.set('user:2', 'Jane');
2686+
await db.set('post:1', 'Hello');
2687+
await db.hSet('hash:1', { field1: 'value1' });
2688+
await db.zAdd('zset:1', { member1: 10 });
2689+
} catch {
2690+
// Ignore errors during setup
2691+
}
2692+
});
2693+
2694+
it('should return all keys matching exact pattern', async () => {
2695+
const response = await db.keys('user:1');
2696+
expect(response.success).to.be.true;
2697+
expect(response.data.result).to.have.lengthOf(1);
2698+
expect(response.data.result).to.include('user:1');
2699+
});
2700+
2701+
it('should return all keys matching wildcard pattern *', async () => {
2702+
const response = await db.keys('*');
2703+
expect(response.success).to.be.true;
2704+
expect(response.data.result).to.have.lengthOf(5); // All test keys
2705+
expect(response.data.result).to.include.members([
2706+
'user:1',
2707+
'user:2',
2708+
'post:1',
2709+
'hash:1',
2710+
'zset:1',
2711+
]);
2712+
});
2713+
2714+
it('should return all keys matching prefix pattern', async () => {
2715+
const response = await db.keys('user:*');
2716+
expect(response.success).to.be.true;
2717+
expect(response.data.result).to.have.lengthOf(2);
2718+
expect(response.data.result).to.include.members([
2719+
'user:1',
2720+
'user:2',
2721+
]);
2722+
});
2723+
2724+
it('should return all keys matching suffix pattern', async () => {
2725+
const response = await db.keys('*:1');
2726+
expect(response.success).to.be.true;
2727+
expect(response.data.result).to.have.lengthOf(4);
2728+
expect(response.data.result).to.include.members([
2729+
'user:1',
2730+
'post:1',
2731+
'hash:1',
2732+
'zset:1',
2733+
]);
2734+
});
2735+
2736+
it('should return all keys matching question mark pattern', async () => {
2737+
const response = await db.keys('user:?');
2738+
expect(response.success).to.be.true;
2739+
expect(response.data.result).to.have.lengthOf(2);
2740+
expect(response.data.result).to.include.members([
2741+
'user:1',
2742+
'user:2',
2743+
]);
2744+
});
2745+
2746+
it('should return empty array for non-matching pattern', async () => {
2747+
const response = await db.keys('nonexistent:*');
2748+
expect(response.success).to.be.true;
2749+
expect(response.data.result).to.have.lengthOf(0);
2750+
});
2751+
2752+
it('should return empty array when database is empty', async () => {
2753+
await db.flushDB();
2754+
const response = await db.keys('*');
2755+
expect(response.success).to.be.true;
2756+
expect(response.data.result).to.have.lengthOf(0);
2757+
});
2758+
2759+
it('should throw error for invalid pattern', async () => {
2760+
try {
2761+
await db.keys({ invalid: 'pattern' });
2762+
expect.fail('Should have thrown error');
2763+
} catch (error) {
2764+
expect(error.message).to.include(
2765+
'key must be a string or number',
2766+
);
2767+
}
2768+
});
2769+
});
2770+
26772771
it('should run all commands concurrently without error', async () => {
26782772
const data = await Promise.allSettled([
26792773
db.ping(),

0 commit comments

Comments
 (0)