Skip to content

Commit

Permalink
feat: add nx and xx options to SetOpts (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored Oct 23, 2024
1 parent f23f2e0 commit 08193b7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
28 changes: 27 additions & 1 deletion command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ export type StralgoTarget = "KEYS" | "STRINGS";
export interface SetOpts {
ex?: number;
px?: number;
/** Sets `NX` option. */
nx?: boolean;
/** Sets `XX` option. */
xx?: boolean;
/**
* Sets `EXAT` option.
*
Expand All @@ -138,7 +142,13 @@ export interface SetOpts {
get?: boolean;
}

/**
* @deprecated Use {@linkcode SetOpts.nx}/{@linkcode SetOpts.xx} instead. This type will be removed in the future.
*/
export interface SetWithModeOpts extends SetOpts {
/**
* @deprecated Use {@linkcode SetOpts.nx}/{@linkcode SetOpts.xx} instead. This option will be removed in the future.
*/
mode: "NX" | "XX";
}

Expand Down Expand Up @@ -345,13 +355,29 @@ export interface RedisCommands {
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "get"> & { get?: false | null },
opts?: Omit<SetOpts, "get" | "nx" | "xx"> & {
get?: false | null;
nx?: false | null;
xx?: false | null;
},
): Promise<SimpleString>;
set(
key: string,
value: RedisValue,
opts?: (Omit<SetOpts, "get"> & { get: true }) | SetWithModeOpts,
): Promise<SimpleString | BulkNil>;
/** Executes `SET` command with `NX` option enabled. */
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "nx"> & { nx: true },
): Promise<SimpleString | BulkNil>;
/** Executes `SET` command with `XX` option enabled. */
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "xx"> & { xx: true },
): Promise<SimpleString | BulkNil>;
setbit(key: string, offset: number, value: RedisValue): Promise<Integer>;
setex(key: string, seconds: number, value: RedisValue): Promise<SimpleString>;
setnx(key: string, value: RedisValue): Promise<Integer>;
Expand Down
30 changes: 26 additions & 4 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1347,19 +1347,35 @@ class RedisImpl implements Redis {
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "get"> & { get?: false | null },
opts?: Omit<SetOpts, "get" | "nx" | "xx"> & {
get?: false | null;
nx?: false | null;
xx?: false | null;
},
): Promise<SimpleString>;
set(
key: string,
value: RedisValue,
opts?: (Omit<SetOpts, "get"> & { get: true }) | SetWithModeOpts,
): Promise<SimpleString | BulkNil>;
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "nx"> & { nx: true },
): Promise<SimpleString | BulkNil>;
set(
key: string,
value: RedisValue,
opts?: Omit<SetOpts, "xx"> & { xx: true },
): Promise<SimpleString | BulkNil>;
set(
key: string,
value: RedisValue,
opts?:
| (Omit<SetOpts, "get"> & {
get?: true | false | null;
| (Omit<SetOpts, "get" | "nx" | "xx"> & {
get?: boolean | null;
nx?: boolean | null;
xx?: boolean | null;
})
| SetWithModeOpts,
) {
Expand All @@ -1379,7 +1395,13 @@ class RedisImpl implements Redis {
}

let isAbleToReturnNil = false;
if ((opts as SetWithModeOpts)?.mode) {
if (opts?.nx) {
args.push("NX");
isAbleToReturnNil = true;
} else if (opts?.xx) {
args.push("XX");
isAbleToReturnNil = true;
} else if ((opts as SetWithModeOpts)?.mode) {
args.push((opts as SetWithModeOpts).mode);
isAbleToReturnNil = true;
}
Expand Down
14 changes: 13 additions & 1 deletion tests/commands/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,19 @@ export function stringTests(
const prev = "foo";
const v1 = await client.set("setWithNX", prev, { mode: "NX" });
assertEquals(v1, "OK");
const v2 = await client.set("setWithNX", "bar", { mode: "NX" });
assertType<IsNullable<typeof v1>>(true);

const v2 = await client.set("setWithNX", "bar", { nx: true });
assertEquals(v2, null);
assertType<IsNullable<typeof v2>>(true);
});

it("supports `XX` option", async () => {
const v1 = await client.set("setWithXX", "foo", { mode: "XX" });
assertEquals(v1, null);
assertType<IsNullable<typeof v1>>(true);

const v2 = await client.set("setWithXX", "foo", { xx: true });
assertEquals(v2, null);
assertType<IsNullable<typeof v2>>(true);
});
Expand Down

0 comments on commit 08193b7

Please sign in to comment.