Skip to content

Commit

Permalink
Fix Auto-pipeline Proxy Json Issue (#1080)
Browse files Browse the repository at this point in the history
* fix auto-pipeline proxy json issue

* fmt

* rm ts-ignore
  • Loading branch information
CahidArda committed May 23, 2024
1 parent 427ace4 commit f32ab58
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
32 changes: 32 additions & 0 deletions pkg/auto-pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,37 @@ describe("Auto pipeline", () => {
// @ts-expect-error pipelineCounter is not in type but accessible
expect(redis.pipelineCounter).toBe(1);
});

test("should handle JSON commands correctly", async () => {

const redis = Redis.fromEnv({
latencyLogging: false,
enableAutoPipelining: true
});

// @ts-expect-error pipelineCounter is not in type but accessible
expect(redis.pipelineCounter).toBe(0);

const res = await Promise.all([
redis.set("foo1", "bar"),
redis.json.set("baz1", "$", { hello: "world" }),
redis.get("foo1"),
redis.json.get("baz1"),
redis.json.del("baz1"),
redis.json.get("baz1"),
])

// @ts-expect-error pipelineCounter is not in type but accessible
expect(redis.pipelineCounter).toBe(1);

expect(res).toEqual([
"OK",
"OK",
"bar",
{ hello: "world" },
1,
null
])
})
});

17 changes: 12 additions & 5 deletions pkg/auto-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CommandArgs } from "./types";
// properties which are only available in redis
type redisOnly = Exclude<keyof Redis, keyof Pipeline>;

export function createAutoPipelineProxy(_redis: Redis) {
export function createAutoPipelineProxy(_redis: Redis, json?: boolean): Redis {
const redis = _redis as Redis & {
autoPipelineExecutor: AutoPipelineExecutor;
};
Expand All @@ -22,27 +22,34 @@ export function createAutoPipelineProxy(_redis: Redis) {
return redis.autoPipelineExecutor.pipelineCounter;
}

if (command === "json") {
return createAutoPipelineProxy(redis, true);
};

const commandInRedisButNotPipeline =
command in redis && !(command in redis.autoPipelineExecutor.pipeline);

if (commandInRedisButNotPipeline) {
return redis[command as redisOnly];
}

command = command as keyof Pipeline;
// If the method is a function on the pipeline, wrap it with the executor logic
if (typeof redis.autoPipelineExecutor.pipeline[command] === "function") {
if (typeof redis.autoPipelineExecutor.pipeline[command as keyof Pipeline] === "function") {
return (...args: CommandArgs<typeof Command>) => {
// pass the function as a callback
return redis.autoPipelineExecutor.withAutoPipeline((pipeline) => {
(pipeline[command] as Function)(...args);
if (json) {
(pipeline.json[command as keyof Pipeline["json"]] as Function)(...args)
} else {
(pipeline[command as keyof Pipeline] as Function)(...args);
}
});
};
}

// if the property is not a function, a property of redis or "pipelineCounter"
// simply return it from pipeline
return redis.autoPipelineExecutor.pipeline[command];
return redis.autoPipelineExecutor.pipeline[command as keyof Pipeline];
},
}) as Redis;
}
Expand Down

0 comments on commit f32ab58

Please sign in to comment.