Skip to content

Commit

Permalink
feat(HscanStream): adding NOVALUES option (#1943)
Browse files Browse the repository at this point in the history
* feat(HscanStream): adding NOVALUES option

* fix(Test): adding tests for NOVALUES option

* Update README.md for hscanStream

Co-authored-by: Tihomir Krasimirov Mateev <[email protected]>

---------

Co-authored-by: Nicolas Zambrano <[email protected]>
Co-authored-by: Tihomir Krasimirov Mateev <[email protected]>
  • Loading branch information
3 people authored Jan 28, 2025
1 parent 5d3e7ca commit 2f9843d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,18 @@ the key names are not utf8 strings.
There are also `hscanStream`, `zscanStream` and `sscanStream` to iterate through elements in a hash, zset and set. The interface of each is
similar to `scanStream` except the first argument is the key name:

```javascript
const stream = redis.zscanStream("myhash", {
match: "age:??",
});
```
The `hscanStream` also accepts the `noValues` option to specify whether Redis should return only the keys in the hash table without their corresponding values.
```javascript
const stream = redis.hscanStream("myhash", {
match: "age:??",
noValues: true,
});
```

You can learn more from the [Redis documentation](http://redis.io/commands/scan).

**Useful Tips**
Expand Down
5 changes: 4 additions & 1 deletion lib/ScanStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface Options extends ReadableOptions {
command: string;
redis: any;
count?: string | number;
noValues?: boolean;
}

/**
Expand Down Expand Up @@ -39,7 +40,9 @@ export default class ScanStream extends Readable {
if (this.opt.count) {
args.push("COUNT", String(this.opt.count));
}

if (this.opt.noValues) {
args.push("NOVALUES");
}
this.opt.redis[this.opt.command](args, (err, res) => {
if (err) {
this.emit("error", err);
Expand Down
3 changes: 2 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface ScanStreamOptions {
match?: string;
type?: string;
count?: number;
}
noValues?: boolean;
}
19 changes: 19 additions & 0 deletions test/functional/scan_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ describe("*scanStream", () => {
});
});

describe('hscanStream', () => {
it('should recognize `NOVALUES`', (done) => {
let keys = [];
const redis = new Redis();
redis.hset('object', 'foo1', 'foo1_value');
redis.hset('object', 'foo2', 'foo2_value');
redis.hset('object', 'foo3', 'foo3_value');
const stream = redis.hscanStream('object', { noValues: true });
stream.on('data', function (data) {
keys = keys.concat(data);
});
stream.on('end', () => {
expect(keys).to.eql(['foo1', 'foo2', 'foo3']);
redis.disconnect();
done();
});
});
});

describe("Cluster", () => {
it("should work in cluster mode", (done) => {
const slotTable = [
Expand Down

0 comments on commit 2f9843d

Please sign in to comment.