Skip to content

Commit f32ab58

Browse files
authored
Fix Auto-pipeline Proxy Json Issue (#1080)
* fix auto-pipeline proxy json issue * fmt * rm ts-ignore
1 parent 427ace4 commit f32ab58

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

pkg/auto-pipeline.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,37 @@ describe("Auto pipeline", () => {
299299
// @ts-expect-error pipelineCounter is not in type but accessible
300300
expect(redis.pipelineCounter).toBe(1);
301301
});
302+
303+
test("should handle JSON commands correctly", async () => {
304+
305+
const redis = Redis.fromEnv({
306+
latencyLogging: false,
307+
enableAutoPipelining: true
308+
});
309+
310+
// @ts-expect-error pipelineCounter is not in type but accessible
311+
expect(redis.pipelineCounter).toBe(0);
312+
313+
const res = await Promise.all([
314+
redis.set("foo1", "bar"),
315+
redis.json.set("baz1", "$", { hello: "world" }),
316+
redis.get("foo1"),
317+
redis.json.get("baz1"),
318+
redis.json.del("baz1"),
319+
redis.json.get("baz1"),
320+
])
321+
322+
// @ts-expect-error pipelineCounter is not in type but accessible
323+
expect(redis.pipelineCounter).toBe(1);
324+
325+
expect(res).toEqual([
326+
"OK",
327+
"OK",
328+
"bar",
329+
{ hello: "world" },
330+
1,
331+
null
332+
])
333+
})
302334
});
303335

pkg/auto-pipeline.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { CommandArgs } from "./types";
66
// properties which are only available in redis
77
type redisOnly = Exclude<keyof Redis, keyof Pipeline>;
88

9-
export function createAutoPipelineProxy(_redis: Redis) {
9+
export function createAutoPipelineProxy(_redis: Redis, json?: boolean): Redis {
1010
const redis = _redis as Redis & {
1111
autoPipelineExecutor: AutoPipelineExecutor;
1212
};
@@ -22,27 +22,34 @@ export function createAutoPipelineProxy(_redis: Redis) {
2222
return redis.autoPipelineExecutor.pipelineCounter;
2323
}
2424

25+
if (command === "json") {
26+
return createAutoPipelineProxy(redis, true);
27+
};
28+
2529
const commandInRedisButNotPipeline =
2630
command in redis && !(command in redis.autoPipelineExecutor.pipeline);
2731

2832
if (commandInRedisButNotPipeline) {
2933
return redis[command as redisOnly];
3034
}
3135

32-
command = command as keyof Pipeline;
3336
// If the method is a function on the pipeline, wrap it with the executor logic
34-
if (typeof redis.autoPipelineExecutor.pipeline[command] === "function") {
37+
if (typeof redis.autoPipelineExecutor.pipeline[command as keyof Pipeline] === "function") {
3538
return (...args: CommandArgs<typeof Command>) => {
3639
// pass the function as a callback
3740
return redis.autoPipelineExecutor.withAutoPipeline((pipeline) => {
38-
(pipeline[command] as Function)(...args);
41+
if (json) {
42+
(pipeline.json[command as keyof Pipeline["json"]] as Function)(...args)
43+
} else {
44+
(pipeline[command as keyof Pipeline] as Function)(...args);
45+
}
3946
});
4047
};
4148
}
4249

4350
// if the property is not a function, a property of redis or "pipelineCounter"
4451
// simply return it from pipeline
45-
return redis.autoPipelineExecutor.pipeline[command];
52+
return redis.autoPipelineExecutor.pipeline[command as keyof Pipeline];
4653
},
4754
}) as Redis;
4855
}

0 commit comments

Comments
 (0)