Skip to content

Commit

Permalink
feat: add type support for scanStream (#1287)
Browse files Browse the repository at this point in the history
Closes #1279
  • Loading branch information
luin authored Feb 6, 2021
1 parent 8003af4 commit ad8ffa0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,15 @@ stream.on("end", () => {
});
```

`scanStream` accepts an option, with which you can specify the `MATCH` pattern and the `COUNT` argument:
`scanStream` accepts an option, with which you can specify the `MATCH` pattern, the `TYPE` filter, and the `COUNT` argument:

```javascript
const stream = redis.scanStream({
// only returns keys following the pattern of `user:*`
match: "user:*",
// only return objects that match a given type,
// (requires Redis >= 6.0)
type: "zset",
// returns approximately 100 elements per call
count: 100,
});
Expand Down
4 changes: 4 additions & 0 deletions lib/ScanStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Readable, ReadableOptions } from "stream";
export interface IScanStreamOptions extends ReadableOptions {
key?: string;
match?: string;
type?: string;
command: string;
redis: any;
count?: string | number;
Expand Down Expand Up @@ -42,6 +43,9 @@ export default class ScanStream extends Readable {
if (this.opt.match) {
args.push("MATCH", this.opt.match);
}
if (this.opt.type) {
args.push("TYPE", this.opt.type);
}
if (this.opt.count) {
args.push("COUNT", String(this.opt.count));
}
Expand Down
22 changes: 22 additions & 0 deletions test/functional/scan_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ describe("*scanStream", function () {
);
});

it("should recognize `TYPE`", function (done) {
let keys = [];
const redis = new Redis();
redis.set("foo1", "bar");
redis.set("foo2", "bar");
redis.set("foo3", "bar");
redis.lpush("loo1", "1");
redis.lpush("loo2", "1");
redis.lpush("loo3", "1");
const stream = redis.scanStream({
type: "list",
});
stream.on("data", function (data) {
keys = keys.concat(data);
});
stream.on("end", function () {
expect(keys.sort()).to.eql(["loo1", "loo2", "loo3"]);
redis.disconnect();
done();
});
});

it("should recognize `COUNT`", function (done) {
let keys = [];
const redis = new Redis();
Expand Down

0 comments on commit ad8ffa0

Please sign in to comment.