From 51b14786eef4c627c178de4967434e8d4a51ebe0 Mon Sep 17 00:00:00 2001 From: Nick Skuybeda Date: Mon, 25 Nov 2019 20:01:41 -0500 Subject: [PATCH] feat: support multiple fields for hset --- lib/command.ts | 12 +++++++++++ test/functional/transformer.ts | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/command.ts b/lib/command.ts index baa15395..e26b1b2d 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -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 = []; diff --git a/test/functional/transformer.ts b/test/functional/transformer.ts index dd82d06d..d5d6818f 100644 --- a/test/functional/transformer.ts +++ b/test/functional/transformer.ts @@ -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(); + }); + }); + }); + }); }); });