Skip to content

Commit 6b0dc1e

Browse files
committedMar 14, 2022
fix: support passing keyPrefix via redisOptions
1 parent 687d3eb commit 6b0dc1e

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed
 

‎lib/cluster/ClusterOptions.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SrvRecord, resolveSrv, lookup } from "dns";
2+
import { RedisOptions } from "../redis/RedisOptions";
23
import { CommanderOptions } from "../utils/Commander";
34
import { NodeRole } from "./util";
45

@@ -123,7 +124,16 @@ export interface ClusterOptions extends CommanderOptions {
123124
*
124125
* @default null
125126
*/
126-
redisOptions?: any;
127+
redisOptions?: Omit<
128+
RedisOptions,
129+
| "port"
130+
| "host"
131+
| "path"
132+
| "sentinels"
133+
| "retryStrategy"
134+
| "enableOfflineQueue"
135+
| "readOnly"
136+
>;
127137

128138
/**
129139
* By default, When a new Cluster instance is created,

‎lib/cluster/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class Cluster extends Commander {
108108
this.startupNodes = startupNodes;
109109
this.options = defaults({}, options, DEFAULT_CLUSTER_OPTIONS, this.options);
110110

111+
if (
112+
this.options.redisOptions &&
113+
this.options.redisOptions.keyPrefix &&
114+
!this.options.keyPrefix
115+
) {
116+
this.options.keyPrefix = this.options.redisOptions.keyPrefix;
117+
}
118+
111119
// validate options
112120
if (
113121
typeof this.options.scaleReads !== "function" &&

‎test-cluster/basic.ts

+20
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,24 @@ describe("cluster", () => {
125125
expect(await cluster.get("foo")).to.eql("auto pipelining");
126126
});
127127
});
128+
129+
describe("key prefixing", () => {
130+
it("works when passing via redisOptions", async () => {
131+
const cluster1 = new Cluster([{ host: "127.0.0.1", port: masters[0] }], {
132+
redisOptions: { keyPrefix: "prefix:" },
133+
});
134+
await cluster1.set("foo", "bar");
135+
const cluster2 = new Cluster([{ host: "127.0.0.1", port: masters[0] }]);
136+
expect(await cluster2.get("prefix:foo")).to.eql("bar");
137+
});
138+
139+
it("works when passing via root", async () => {
140+
const cluster1 = new Cluster([{ host: "127.0.0.1", port: masters[0] }], {
141+
keyPrefix: "prefix:",
142+
});
143+
await cluster1.set("foo", "bar");
144+
const cluster2 = new Cluster([{ host: "127.0.0.1", port: masters[0] }]);
145+
expect(await cluster2.get("prefix:foo")).to.eql("bar");
146+
});
147+
});
128148
});

‎test/unit/clusters/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ describe("cluster", () => {
2626
expect(cluster.options).to.have.property("showFriendlyErrorStack", true);
2727
});
2828

29+
it("should support passing keyPrefix via redisOptions", () => {
30+
const cluster = new Cluster([{ port: 7777 }], {
31+
redisOptions: { keyPrefix: "prefix:" },
32+
});
33+
expect(cluster.options).to.have.property("keyPrefix", "prefix:");
34+
});
35+
2936
it("throws when scaleReads is invalid", () => {
3037
expect(() => {
3138
new Cluster([{}], { scaleReads: "invalid" });

0 commit comments

Comments
 (0)
Please sign in to comment.