Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type support for scanStream #1287

Merged
merged 1 commit into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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