Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions pkg/commands/copy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { keygen, newHttpClient, randomID } from "../test-utils";

import { afterAll, describe, expect, test } from "bun:test";
import { CopyCommand } from "./copy";
import { SetCommand } from "./set";
import { LPushCommand } from "./lpush";

const client = newHttpClient();

const { newKey, cleanup } = keygen();
afterAll(cleanup);

describe("COPY test", () => {
test("should copy key-value to another key", async () => {
const key = newKey();
const destinationKey = newKey();
const value = randomID();
await new SetCommand([key, value]).exec(client);
const res = await new CopyCommand([key, destinationKey]).exec(client);
expect(res).toEqual("COPIED");
});

test("should not override existing destination", async () => {
const key = newKey();
const destinationKey = newKey();
const value = randomID();
await new SetCommand([key, value]).exec(client);
await new SetCommand([destinationKey, value]).exec(client);
const res = await new CopyCommand([key, destinationKey]).exec(client);
expect(res).toEqual("NOT_COPIED");
});

test("should override existing destination with replace", async () => {
const key = newKey();
const destinationKey = newKey();
const value = randomID();
await new SetCommand([key, value]).exec(client);
await new SetCommand([destinationKey, value]).exec(client);
const res = await new CopyCommand([key, destinationKey, { replace: true }]).exec(client);
expect(res).toEqual("COPIED");
});

test("should handle non-existent source key", async () => {
const key = newKey();
const destinationKey = newKey();
const res = await new CopyCommand([key, destinationKey]).exec(client);
expect(res).toEqual("NOT_COPIED");
});

test("should handle same source and destination keys", async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value]).exec(client);
const res = await new CopyCommand([key, key]).exec(client);
expect(res).toEqual("NOT_COPIED");
});

test("should copy list data type", async () => {
const key = newKey();
const destinationKey = newKey();
await new LPushCommand([key, "value1", "value2"]).exec(client);
const res = await new CopyCommand([key, destinationKey]).exec(client);
expect(res).toEqual("COPIED");
});
});
21 changes: 21 additions & 0 deletions pkg/commands/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Command, CommandOptions } from "./command";

/**
* @see https://redis.io/commands/copy
*/
export class CopyCommand extends Command<number, "COPIED" | "NOT_COPIED"> {
constructor(
[key, destinationKey, opts]: [key: string, destinationKey: string, opts?: { replace: boolean }],
commandOptions?: CommandOptions<number, "COPIED" | "NOT_COPIED">
) {
super(["COPY", key, destinationKey, ...(opts?.replace ? ["REPLACE"] : [])], {
...commandOptions,
deserialize(result) {
if (result > 0) {
return "COPIED";
}
return "NOT_COPIED";
},
});
}
}
1 change: 1 addition & 0 deletions pkg/commands/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./bitcount";
export * from "./bitop";
export * from "./bitpos";
export * from "./command";
export * from "./copy";
export * from "./dbsize";
export * from "./decr";
export * from "./decrby";
Expand Down
7 changes: 7 additions & 0 deletions pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
BitCountCommand,
BitOpCommand,
BitPosCommand,
CopyCommand,
DBSizeCommand,
DecrByCommand,
DecrCommand,
Expand Down Expand Up @@ -311,6 +312,12 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
bitpos = (...args: CommandArgs<typeof BitPosCommand>) =>
this.chain(new BitPosCommand(args, this.commandOptions));

/**
* @see https://redis.io/commands/copy
*/
copy = (...args: CommandArgs<typeof CopyCommand>) =>
this.chain(new CopyCommand(args, this.commandOptions));

/**
* @see https://redis.io/commands/zdiffstore
*/
Expand Down
7 changes: 7 additions & 0 deletions pkg/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BitOpCommand,
BitPosCommand,
CommandOptions,
CopyCommand,
DBSizeCommand,
DecrByCommand,
DecrCommand,
Expand Down Expand Up @@ -455,6 +456,12 @@ export class Redis {
bitpos = (...args: CommandArgs<typeof BitPosCommand>) =>
new BitPosCommand(args, this.opts).exec(this.client);

/**
* @see https://redis.io/commands/copy
*/
copy = (...args: CommandArgs<typeof CopyCommand>) =>
new CopyCommand(args, this.opts).exec(this.client);

/**
* @see https://redis.io/commands/dbsize
*/
Expand Down