Skip to content

Commit

Permalink
feat: support multiple fields for hset
Browse files Browse the repository at this point in the history
  • Loading branch information
mskuybeda authored and luin committed Nov 29, 2019
1 parent 06b28e1 commit 51b1478
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ Command.setReplyTransformer("hgetall", function(result) {
return result;
});

Command.setArgumentTransformer("hset", function(args) {
if (args.length === 2) {
if (typeof Map !== "undefined" && args[1] instanceof Map) {
return [args[0]].concat(convertMapToArray(args[1]));
}
if (typeof args[1] === "object" && args[1] !== null) {
return [args[0]].concat(convertObjectToArray(args[1]));
}
}
return args;
});

class MixedBuffers {
length = 0;
items = [];
Expand Down
39 changes: 39 additions & 0 deletions test/functional/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,44 @@ describe("transformer", function() {
});
});
});

describe("hset", function() {
it("should support object", function(done) {
var redis = new Redis();
redis.hset("foo", { a: 1, b: "e", c: 123 }, function(err, result) {
expect(result).to.eql(3);
redis.hget("foo", "b", function(err, result) {
expect(result).to.eql("e");
done();
});
});
});
it("should support Map", function(done) {
if (typeof Map === "undefined") {
return done();
}
var redis = new Redis();
var map = new Map();
map.set("a", 1);
map.set("b", "e");
redis.hset("foo", map, function(err, result) {
expect(result).to.eql(2);
redis.hget("foo", "b", function(err, result) {
expect(result).to.eql("e");
done();
});
});
});
it("should affect the old way", function(done) {
var redis = new Redis();
redis.hset("foo", "a", 1, "b", "e", function(err, result) {
expect(result).to.eql(2);
redis.hget("foo", "b", function(err, result) {
expect(result).to.eql("e");
done();
});
});
});
});
});
});

0 comments on commit 51b1478

Please sign in to comment.