From 49e1314b049dfa947b6eedfdca3d7ab56c6af7ee Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Wed, 13 Dec 2023 12:13:26 +0300 Subject: [PATCH 1/5] Add GT and LT commands to zadd --- pkg/commands/zadd.test.ts | 178 ++++++++++++++++++++++++++++++++------ pkg/commands/zadd.ts | 89 +++++++++++++------ 2 files changed, 213 insertions(+), 54 deletions(-) diff --git a/pkg/commands/zadd.test.ts b/pkg/commands/zadd.test.ts index 6f4de162..01b0a5a6 100644 --- a/pkg/commands/zadd.test.ts +++ b/pkg/commands/zadd.test.ts @@ -3,6 +3,8 @@ import { keygen, newHttpClient, randomID } from "../test-utils"; import { afterAll, describe, expect, test } from "bun:test"; import { ZAddCommand } from "./zadd"; import { ZScoreCommand } from "./zscore"; +import { ZRangeCommand } from "./zrange"; +import { sleep } from "bun"; const client = newHttpClient(); @@ -12,19 +14,17 @@ afterAll(cleanup); describe("command format", () => { describe("without options", () => { test("build the correct command", () => { - expect(new ZAddCommand(["key", { score: 0, member: "member" }]).command).toEqual([ - "zadd", - "key", - 0, - "member", - ]); + expect( + new ZAddCommand(["key", { score: 0, member: "member" }]).command + ).toEqual(["zadd", "key", 0, "member"]); }); }); describe("with nx", () => { test("build the correct command", () => { expect( - new ZAddCommand(["key", { nx: true }, { score: 0, member: "member" }]).command, + new ZAddCommand(["key", { nx: true }, { score: 0, member: "member" }]) + .command ).toEqual(["zadd", "key", "nx", 0, "member"]); }); }); @@ -32,7 +32,8 @@ describe("command format", () => { describe("with xx", () => { test("build the correct command", () => { expect( - new ZAddCommand(["key", { xx: true }, { score: 0, member: "member" }]).command, + new ZAddCommand(["key", { xx: true }, { score: 0, member: "member" }]) + .command ).toEqual(["zadd", "key", "xx", 0, "member"]); }); }); @@ -40,7 +41,8 @@ describe("command format", () => { describe("with ch", () => { test("build the correct command", () => { expect( - new ZAddCommand(["key", { ch: true }, { score: 0, member: "member" }]).command, + new ZAddCommand(["key", { ch: true }, { score: 0, member: "member" }]) + .command ).toEqual(["zadd", "key", "ch", 0, "member"]); }); }); @@ -48,7 +50,8 @@ describe("command format", () => { describe("with incr", () => { test("build the correct command", () => { expect( - new ZAddCommand(["key", { incr: true }, { score: 0, member: "member" }]).command, + new ZAddCommand(["key", { incr: true }, { score: 0, member: "member" }]) + .command ).toEqual(["zadd", "key", "incr", 0, "member"]); }); }); @@ -56,7 +59,11 @@ describe("command format", () => { describe("with nx and ch", () => { test("build the correct command", () => { expect( - new ZAddCommand(["key", { nx: true, ch: true }, { score: 0, member: "member" }]).command, + new ZAddCommand([ + "key", + { nx: true, ch: true }, + { score: 0, member: "member" }, + ]).command ).toEqual(["zadd", "key", "nx", "ch", 0, "member"]); }); }); @@ -64,8 +71,11 @@ describe("command format", () => { describe("with nx,ch and incr", () => { test("build the correct command", () => { expect( - new ZAddCommand(["key", { nx: true, ch: true, incr: true }, { score: 0, member: "member" }]) - .command, + new ZAddCommand([ + "key", + { nx: true, ch: true, incr: true }, + { score: 0, member: "member" }, + ]).command ).toEqual(["zadd", "key", "nx", "ch", "incr", 0, "member"]); }); }); @@ -78,7 +88,7 @@ describe("command format", () => { { nx: true }, { score: 0, member: "member" }, { score: 1, member: "member1" }, - ]).command, + ]).command ).toEqual(["zadd", "key", "nx", 0, "member", 1, "member1"]); }); }); @@ -102,9 +112,11 @@ describe("xx", () => { const score = Math.floor(Math.random() * 10); await new ZAddCommand([key, { score, member }]).exec(client); const newScore = score + 1; - const res = await new ZAddCommand([key, { xx: true }, { score: newScore, member }]).exec( - client, - ); + const res = await new ZAddCommand([ + key, + { xx: true }, + { score: newScore, member }, + ]).exec(client); expect(res).toEqual(0); const res2 = await new ZScoreCommand([key, member]).exec(client); @@ -118,9 +130,11 @@ describe("xx", () => { const score = Math.floor(Math.random() * 10); await new ZAddCommand([key, { score, member }]).exec(client); const newScore = score + 1; - const res = await new ZAddCommand([key, { xx: true }, { score: newScore, member }]).exec( - client, - ); + const res = await new ZAddCommand([ + key, + { xx: true }, + { score: newScore, member }, + ]).exec(client); expect(res).toEqual(0); }); }); @@ -134,9 +148,11 @@ describe("nx", () => { const score = Math.floor(Math.random() * 10); await new ZAddCommand([key, { score, member }]).exec(client); const newScore = score + 1; - const res = await new ZAddCommand([key, { nx: true }, { score: newScore, member }]).exec( - client, - ); + const res = await new ZAddCommand([ + key, + { nx: true }, + { score: newScore, member }, + ]).exec(client); expect(res).toEqual(0); const res2 = await new ZScoreCommand([key, member]).exec(client); @@ -148,7 +164,11 @@ describe("nx", () => { const key = newKey(); const member = randomID(); const score = Math.floor(Math.random() * 10); - const res = await new ZAddCommand([key, { nx: true }, { score, member }]).exec(client); + const res = await new ZAddCommand([ + key, + { nx: true }, + { score, member }, + ]).exec(client); expect(res).toEqual(1); }); }); @@ -161,9 +181,11 @@ describe("ch", () => { const score = Math.floor(Math.random() * 10); await new ZAddCommand([key, { score, member }]).exec(client); const newScore = score + 1; - const res = await new ZAddCommand([key, { ch: true }, { score: newScore, member }]).exec( - client, - ); + const res = await new ZAddCommand([ + key, + { ch: true }, + { score: newScore, member }, + ]).exec(client); expect(res).toEqual(1); }); }); @@ -174,8 +196,108 @@ describe("incr", () => { const member = randomID(); const score = Math.floor(Math.random() * 10); await new ZAddCommand([key, { score, member }]).exec(client); - const res = await new ZAddCommand([key, { incr: true }, { score: 1, member }]).exec(client); + const res = await new ZAddCommand([ + key, + { incr: true }, + { score: 1, member }, + ]).exec(client); expect(typeof res).toBe("number"); expect(res).toEqual(score + 1); }); }); + +describe("LT and GT", () => { + describe("GT", () => { + test("should replace successfully if greater than", async () => { + const key = newKey(); + + await new ZAddCommand([key, { score: 1, member: "one" }]).exec(client); + await new ZAddCommand([key, { score: 2, member: "two" }]).exec(client); + await new ZAddCommand([key, { score: 3, member: "three" }]).exec(client); + + await new ZAddCommand([ + key, + { gt: true }, + { score: 4, member: "two" }, + ]).exec(client); + + const res2 = await new ZRangeCommand([ + key, + 0, + -1, + { withScores: true }, + ]).exec(client); + + expect(res2).toEqual(["one", 1, "three", 3, "two", 4]); + }); + + test("should fail to replace if its not greater than", async () => { + const key = newKey(); + + await new ZAddCommand([key, { score: 1, member: "one" }]).exec(client); + await new ZAddCommand([key, { score: 2, member: "two" }]).exec(client); + await new ZAddCommand([key, { score: 3, member: "three" }]).exec(client); + + await new ZAddCommand([ + key, + { gt: true }, + { score: 1, member: "two" }, + ]).exec(client); + + const res2 = await new ZRangeCommand([ + key, + 0, + -1, + { withScores: true }, + ]).exec(client); + + expect(res2).toEqual(["one", 1, "two", 2, "three", 3]); + }); + }); + + describe("LT", () => { + test("should replace successfully if less than", async () => { + const key = newKey(); + + await new ZAddCommand([key, { score: 1, member: "one" }]).exec(client); + await new ZAddCommand([key, { score: 2, member: "two" }]).exec(client); + await new ZAddCommand([key, { score: 3, member: "three" }]).exec(client); + await new ZAddCommand([ + key, + { lt: true }, + { score: 2, member: "three" }, + ]).exec(client); + + const res2 = await new ZRangeCommand([ + key, + 0, + -1, + { withScores: true }, + ]).exec(client); + expect(res2).toEqual(["one", 1, "three", 2, "two", 2]); + }); + + test("should fail to replace if its not less than", async () => { + const key = newKey(); + + await new ZAddCommand([key, { score: 1, member: "one" }]).exec(client); + await new ZAddCommand([key, { score: 2, member: "two" }]).exec(client); + await new ZAddCommand([key, { score: 3, member: "three" }]).exec(client); + + await new ZAddCommand([ + key, + { lt: true }, + { score: 6, member: "two" }, + ]).exec(client); + + const res2 = await new ZRangeCommand([ + key, + 0, + -1, + { withScores: true }, + ]).exec(client); + + expect(res2).toEqual(["one", 1, "two", 2, "three", 3]); + }); + }); +}); diff --git a/pkg/commands/zadd.ts b/pkg/commands/zadd.ts index 416f641b..7ebfc296 100644 --- a/pkg/commands/zadd.ts +++ b/pkg/commands/zadd.ts @@ -1,42 +1,73 @@ import { Command, CommandOptions } from "./command"; -export type ZAddCommandOptions = ( - | { nx: true; xx?: never } - | { nx?: never; xx: true } - | { nx?: never; xx?: never } -) & { ch?: true }; +type NXAndXXOptions = + | { + nx: true; + xx?: never; + } + | { + nx?: never; + xx: true; + } + | { + nx?: never; + xx?: never; + }; + +type LTAndGTOptions = + | { + lt: true; + gt?: never; + } + | { + lt?: never; + gt: true; + } + | { + lt?: never; + gt?: never; + }; + +export type ZAddCommandOptions = NXAndXXOptions & + LTAndGTOptions & { ch?: true }; export type ZAddCommandOptionsWithIncr = ZAddCommandOptions & { incr: true }; -/** - * This type is defiend up here because otherwise it would be automatically formatted into - * multiple lines by Deno. As a result of that, Deno will add a comma to the end and then - * complain about the comma being there... - */ -type Arg2 = ScoreMember | ZAddCommandOptions | ZAddCommandOptionsWithIncr; +type Arg2 = + | ScoreMember + | ZAddCommandOptions + | ZAddCommandOptionsWithIncr; export type ScoreMember = { score: number; member: TData }; /** * @see https://redis.io/commands/zadd */ -export class ZAddCommand extends Command { - constructor( - cmd: [key: string, scoreMember: ScoreMember, ...scoreMemberPairs: ScoreMember[]], - opts?: CommandOptions, - ); - constructor( - cmd: [ - key: string, - opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr, - ...scoreMemberPairs: ScoreMember[], - ], - opts?: CommandOptions, - ); +export class ZAddCommand extends Command< + number | null, + number | null +> { + // constructor( + // cmd: [ + // key: string, + // scoreMember: ScoreMember, + // ...scoreMemberPairs: ScoreMember[] + // ], + // opts?: CommandOptions + // ); + + // constructor( + // cmd: [ + // key: string, + // opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr, + // ...scoreMemberPairs: ScoreMember[] + // ], + // opts?: CommandOptions + // ); + constructor( [key, arg1, ...arg2]: [string, Arg2, ...ScoreMember[]], - opts?: CommandOptions, + opts?: CommandOptions ) { const command: unknown[] = ["zadd", key]; - if ("nx" in arg1 && arg1.nx) { command.push("nx"); } else if ("xx" in arg1 && arg1.xx) { @@ -49,6 +80,12 @@ export class ZAddCommand extends Command Date: Wed, 13 Dec 2023 12:16:35 +0300 Subject: [PATCH 2/5] Remove unused import --- pkg/commands/zadd.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/commands/zadd.test.ts b/pkg/commands/zadd.test.ts index 01b0a5a6..88f15fad 100644 --- a/pkg/commands/zadd.test.ts +++ b/pkg/commands/zadd.test.ts @@ -2,9 +2,8 @@ import { keygen, newHttpClient, randomID } from "../test-utils"; import { afterAll, describe, expect, test } from "bun:test"; import { ZAddCommand } from "./zadd"; -import { ZScoreCommand } from "./zscore"; import { ZRangeCommand } from "./zrange"; -import { sleep } from "bun"; +import { ZScoreCommand } from "./zscore"; const client = newHttpClient(); From 497b747a0249ce348cc485d5067e235ba4528a8b Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Wed, 13 Dec 2023 12:18:34 +0300 Subject: [PATCH 3/5] Remove unused codes and format code --- pkg/commands/zadd.ts | 48 ++++++-------------------------------------- 1 file changed, 6 insertions(+), 42 deletions(-) diff --git a/pkg/commands/zadd.ts b/pkg/commands/zadd.ts index 7ebfc296..b65633fc 100644 --- a/pkg/commands/zadd.ts +++ b/pkg/commands/zadd.ts @@ -1,32 +1,14 @@ import { Command, CommandOptions } from "./command"; type NXAndXXOptions = - | { - nx: true; - xx?: never; - } - | { - nx?: never; - xx: true; - } - | { - nx?: never; - xx?: never; - }; + | { nx: true; xx?: never } + | { nx?: never; xx: true } + | { nx?: never; xx?: never }; type LTAndGTOptions = - | { - lt: true; - gt?: never; - } - | { - lt?: never; - gt: true; - } - | { - lt?: never; - gt?: never; - }; + | { lt: true; gt?: never } + | { lt?: never; gt: true } + | { lt?: never; gt?: never }; export type ZAddCommandOptions = NXAndXXOptions & LTAndGTOptions & { ch?: true }; @@ -45,24 +27,6 @@ export class ZAddCommand extends Command< number | null, number | null > { - // constructor( - // cmd: [ - // key: string, - // scoreMember: ScoreMember, - // ...scoreMemberPairs: ScoreMember[] - // ], - // opts?: CommandOptions - // ); - - // constructor( - // cmd: [ - // key: string, - // opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr, - // ...scoreMemberPairs: ScoreMember[] - // ], - // opts?: CommandOptions - // ); - constructor( [key, arg1, ...arg2]: [string, Arg2, ...ScoreMember[]], opts?: CommandOptions From 4faab064eda835dcebf71b827dc92dc94d9c7fa3 Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Wed, 13 Dec 2023 12:23:26 +0300 Subject: [PATCH 4/5] Simplify types --- pkg/commands/zadd.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/pkg/commands/zadd.ts b/pkg/commands/zadd.ts index b65633fc..404bc247 100644 --- a/pkg/commands/zadd.ts +++ b/pkg/commands/zadd.ts @@ -11,14 +11,9 @@ type LTAndGTOptions = | { lt?: never; gt?: never }; export type ZAddCommandOptions = NXAndXXOptions & - LTAndGTOptions & { ch?: true }; + LTAndGTOptions & { ch?: true } & { incr?: true }; -export type ZAddCommandOptionsWithIncr = ZAddCommandOptions & { incr: true }; - -type Arg2 = - | ScoreMember - | ZAddCommandOptions - | ZAddCommandOptionsWithIncr; +type Arg2 = ScoreMember | ZAddCommandOptions; export type ScoreMember = { score: number; member: TData }; /** * @see https://redis.io/commands/zadd From 3ace80ba56cd955af305baa016abd68acd3e98ce Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Wed, 13 Dec 2023 12:34:51 +0300 Subject: [PATCH 5/5] Remove unused imports --- pkg/commands/types.ts | 17 ++++--- pkg/pipeline.ts | 116 +++++++++++++++++++++++++++++------------- pkg/redis.ts | 87 ++++++++++++++++++++----------- 3 files changed, 146 insertions(+), 74 deletions(-) diff --git a/pkg/commands/types.ts b/pkg/commands/types.ts index 546c990a..c1309108 100644 --- a/pkg/commands/types.ts +++ b/pkg/commands/types.ts @@ -127,17 +127,15 @@ export { Type, type TypeCommand } from "./type"; export { type UnlinkCommand } from "./unlink"; export { type XAddCommand } from "./xadd"; export { type XRangeCommand } from "./xrange"; -export { - ScoreMember, - ZAddCommandOptions, - ZAddCommandOptionsWithIncr, - type ZAddCommand, -} from "./zadd"; +export { ScoreMember, ZAddCommandOptions, type ZAddCommand } from "./zadd"; export { type ZCardCommand } from "./zcard"; export { type ZCountCommand } from "./zcount"; export { type ZDiffStoreCommand } from "./zdiffstore"; export { type ZIncrByCommand } from "./zincrby"; -export { type ZInterStoreCommand, ZInterStoreCommandOptions } from "./zinterstore"; +export { + type ZInterStoreCommand, + ZInterStoreCommandOptions, +} from "./zinterstore"; export { type ZLexCountCommand } from "./zlexcount"; export { type ZMScoreCommand } from "./zmscore"; export { type ZPopMaxCommand } from "./zpopmax"; @@ -152,4 +150,7 @@ export { type ZRevRankCommand } from "./zrevrank"; export { type ZScanCommand } from "./zscan"; export { type ZScoreCommand } from "./zscore"; export { type ZUnionCommand, ZUnionCommandOptions } from "./zunion"; -export { type ZUnionStoreCommand, ZUnionStoreCommandOptions } from "./zunionstore"; +export { + type ZUnionStoreCommand, + ZUnionStoreCommandOptions, +} from "./zunionstore"; diff --git a/pkg/pipeline.ts b/pkg/pipeline.ts index 841b4fd9..3e65f355 100644 --- a/pkg/pipeline.ts +++ b/pkg/pipeline.ts @@ -88,10 +88,10 @@ import { PExpireCommand, PSetEXCommand, PTtlCommand, + PersistCommand, PfAddCommand, PfCountCommand, PfMergeCommand, - PersistCommand, PingCommand, PublishCommand, RPopCommand, @@ -135,7 +135,6 @@ import { UnlinkCommand, ZAddCommand, ZAddCommandOptions, - ZAddCommandOptionsWithIncr, ZCardCommand, ZCountCommand, ZIncrByCommand, @@ -159,8 +158,7 @@ import { import { ZDiffStoreCommand } from "./commands/zdiffstore"; import { ZMScoreCommand } from "./commands/zmscore"; import { UpstashError } from "./error"; -import { Requester } from "./http"; -import { UpstashResponse } from "./http"; +import { Requester, UpstashResponse } from "./http"; import { CommandArgs } from "./types"; // Given a tuple of commands, returns a tuple of the response data of each command @@ -238,7 +236,7 @@ export class Pipeline[] = []> { exec = async < TCommandResults extends unknown[] = [] extends TCommands ? unknown[] - : InferResponseData, + : InferResponseData >(): Promise => { if (this.commands.length === 0) { throw new Error("Pipeline is empty"); @@ -252,7 +250,7 @@ export class Pipeline[] = []> { return res.map(({ error, result }, i) => { if (error) { throw new UpstashError( - `Command ${i + 1} [ ${this.commands[i].command[0]} ] failed: ${error}`, + `Command ${i + 1} [ ${this.commands[i].command[0]} ] failed: ${error}` ); } @@ -271,7 +269,9 @@ export class Pipeline[] = []> { * Pushes a command into the pipeline and returns a chainable instance of the * pipeline */ - private chain(command: Command): Pipeline<[...TCommands, Command]> { + private chain( + command: Command + ): Pipeline<[...TCommands, Command]> { this.commands.push(command); return this as any; // TS thinks we're returning Pipeline<[]> here, because we're not creating a new instance of the class, hence the cast } @@ -298,7 +298,9 @@ export class Pipeline[] = []> { sourceKey: string, ...sourceKeys: string[] ): Pipeline<[...TCommands, BitOpCommand]>; - (op: "not", destinationKey: string, sourceKey: string): Pipeline<[...TCommands, BitOpCommand]>; + (op: "not", destinationKey: string, sourceKey: string): Pipeline< + [...TCommands, BitOpCommand] + >; } = ( op: "and" | "or" | "xor" | "not", destinationKey: string, @@ -306,7 +308,10 @@ export class Pipeline[] = []> { ...sourceKeys: string[] ) => this.chain( - new BitOpCommand([op as any, destinationKey, sourceKey, ...sourceKeys], this.commandOptions), + new BitOpCommand( + [op as any, destinationKey, sourceKey, ...sourceKeys], + this.commandOptions + ) ); /** @@ -449,8 +454,9 @@ export class Pipeline[] = []> { /** * @see https://redis.io/commands/hgetall */ - hgetall = >(...args: CommandArgs) => - this.chain(new HGetAllCommand(args, this.commandOptions)); + hgetall = >( + ...args: CommandArgs + ) => this.chain(new HGetAllCommand(args, this.commandOptions)); /** * @see https://redis.io/commands/hincrby @@ -479,8 +485,9 @@ export class Pipeline[] = []> { /** * @see https://redis.io/commands/hmget */ - hmget = >(...args: CommandArgs) => - this.chain(new HMGetCommand(args, this.commandOptions)); + hmget = >( + ...args: CommandArgs + ) => this.chain(new HMGetCommand(args, this.commandOptions)); /** * @see https://redis.io/commands/hmset @@ -494,9 +501,14 @@ export class Pipeline[] = []> { hrandfield = >( key: string, count?: number, - withValues?: boolean, + withValues?: boolean ) => - this.chain(new HRandFieldCommand([key, count, withValues] as any, this.commandOptions)); + this.chain( + new HRandFieldCommand( + [key, count, withValues] as any, + this.commandOptions + ) + ); /** * @see https://redis.io/commands/hscan @@ -514,7 +526,9 @@ export class Pipeline[] = []> { * @see https://redis.io/commands/hsetnx */ hsetnx = (key: string, field: string, value: TData) => - this.chain(new HSetNXCommand([key, field, value], this.commandOptions)); + this.chain( + new HSetNXCommand([key, field, value], this.commandOptions) + ); /** * @see https://redis.io/commands/hstrlen @@ -561,8 +575,18 @@ export class Pipeline[] = []> { /** * @see https://redis.io/commands/linsert */ - linsert = (key: string, direction: "before" | "after", pivot: TData, value: TData) => - this.chain(new LInsertCommand([key, direction, pivot, value], this.commandOptions)); + linsert = ( + key: string, + direction: "before" | "after", + pivot: TData, + value: TData + ) => + this.chain( + new LInsertCommand( + [key, direction, pivot, value], + this.commandOptions + ) + ); /** * @see https://redis.io/commands/llen @@ -592,13 +616,17 @@ export class Pipeline[] = []> { * @see https://redis.io/commands/lpush */ lpush = (key: string, ...elements: TData[]) => - this.chain(new LPushCommand([key, ...elements], this.commandOptions)); + this.chain( + new LPushCommand([key, ...elements], this.commandOptions) + ); /** * @see https://redis.io/commands/lpushx */ lpushx = (key: string, ...elements: TData[]) => - this.chain(new LPushXCommand([key, ...elements], this.commandOptions)); + this.chain( + new LPushXCommand([key, ...elements], this.commandOptions) + ); /** * @see https://redis.io/commands/lrange @@ -660,7 +688,7 @@ export class Pipeline[] = []> { pexpireat = (...args: CommandArgs) => this.chain(new PExpireAtCommand(args, this.commandOptions)); - /** + /** * @see https://redis.io/commands/pfadd */ pfadd = (...args: CommandArgs) => @@ -688,7 +716,9 @@ export class Pipeline[] = []> { * @see https://redis.io/commands/psetex */ psetex = (key: string, ttl: number, value: TData) => - this.chain(new PSetEXCommand([key, ttl, value], this.commandOptions)); + this.chain( + new PSetEXCommand([key, ttl, value], this.commandOptions) + ); /** * @see https://redis.io/commands/pttl @@ -835,20 +865,28 @@ export class Pipeline[] = []> { /** * @see https://redis.io/commands/smembers */ - smembers = (...args: CommandArgs) => - this.chain(new SMembersCommand(args, this.commandOptions)); + smembers = ( + ...args: CommandArgs + ) => this.chain(new SMembersCommand(args, this.commandOptions)); /** * @see https://redis.io/commands/smismember */ smismember = (key: string, members: TMembers) => - this.chain(new SMIsMemberCommand([key, members], this.commandOptions)); + this.chain( + new SMIsMemberCommand([key, members], this.commandOptions) + ); /** * @see https://redis.io/commands/smove */ smove = (source: string, destination: string, member: TData) => - this.chain(new SMoveCommand([source, destination, member], this.commandOptions)); + this.chain( + new SMoveCommand( + [source, destination, member], + this.commandOptions + ) + ); /** * @see https://redis.io/commands/spop @@ -926,27 +964,31 @@ export class Pipeline[] = []> { */ zadd = ( ...args: - | [key: string, scoreMember: ScoreMember, ...scoreMemberPairs: ScoreMember[]] | [ key: string, - opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr, - ...scoreMemberPairs: [ScoreMember, ...ScoreMember[]], + scoreMember: ScoreMember, + ...scoreMemberPairs: ScoreMember[] + ] + | [ + key: string, + opts: ZAddCommandOptions, + ...scoreMemberPairs: [ScoreMember, ...ScoreMember[]] ] ) => { if ("score" in args[1]) { return this.chain( new ZAddCommand( [args[0], args[1] as ScoreMember, ...(args.slice(2) as any)], - this.commandOptions, - ), + this.commandOptions + ) ); } return this.chain( new ZAddCommand( [args[0], args[1] as any, ...(args.slice(2) as any)], - this.commandOptions, - ), + this.commandOptions + ) ); }; @@ -966,7 +1008,9 @@ export class Pipeline[] = []> { * @see https://redis.io/commands/zincrby */ zincrby = (key: string, increment: number, member: TData) => - this.chain(new ZIncrByCommand([key, increment, member], this.commandOptions)); + this.chain( + new ZIncrByCommand([key, increment, member], this.commandOptions) + ); /** * @see https://redis.io/commands/zinterstore @@ -1008,13 +1052,13 @@ export class Pipeline[] = []> { key: string, min: `(${string}` | `[${string}` | "-" | "+", max: `(${string}` | `[${string}` | "-" | "+", - opts: { byLex: true } & ZRangeCommandOptions, + opts: { byLex: true } & ZRangeCommandOptions ] | [ key: string, min: number | `(${number}` | "-inf" | "+inf", max: number | `(${number}` | "-inf" | "+inf", - opts: { byScore: true } & ZRangeCommandOptions, + opts: { byScore: true } & ZRangeCommandOptions ] ) => this.chain(new ZRangeCommand(args as any, this.commandOptions)); diff --git a/pkg/redis.ts b/pkg/redis.ts index 17403770..8c297464 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -137,7 +137,6 @@ import { XRangeCommand, ZAddCommand, ZAddCommandOptions, - ZAddCommandOptionsWithIncr, ZCardCommand, ZCountCommand, ZIncrByCommand, @@ -366,11 +365,14 @@ export class Redis { use = ( middleware: ( r: UpstashRequest, - next: (req: UpstashRequest) => Promise>, - ) => Promise>, + next: ( + req: UpstashRequest + ) => Promise> + ) => Promise> ) => { const makeRequest = this.client.request.bind(this.client); - this.client.request = (req: UpstashRequest) => middleware(req, makeRequest) as any; + this.client.request = (req: UpstashRequest) => + middleware(req, makeRequest) as any; }; /** @@ -449,9 +451,10 @@ export class Redis { sourceKey: string, ...sourceKeys: string[] ) => - new BitOpCommand([op as any, destinationKey, sourceKey, ...sourceKeys], this.opts).exec( - this.client, - ); + new BitOpCommand( + [op as any, destinationKey, sourceKey, ...sourceKeys], + this.opts + ).exec(this.client); /** * @see https://redis.io/commands/bitpos @@ -588,8 +591,9 @@ export class Redis { /** * @see https://redis.io/commands/hgetall */ - hgetall = >(...args: CommandArgs) => - new HGetAllCommand(args, this.opts).exec(this.client); + hgetall = >( + ...args: CommandArgs + ) => new HGetAllCommand(args, this.opts).exec(this.client); /** * @see https://redis.io/commands/hincrby @@ -618,8 +622,9 @@ export class Redis { /** * @see https://redis.io/commands/hmget */ - hmget = >(...args: CommandArgs) => - new HMGetCommand(args, this.opts).exec(this.client); + hmget = >( + ...args: CommandArgs + ) => new HMGetCommand(args, this.opts).exec(this.client); /** * @see https://redis.io/commands/hmset @@ -636,13 +641,17 @@ export class Redis { >( key: string, count: number, - withValues: boolean, + withValues: boolean ): Promise>; } = >( key: string, count?: number, - withValues?: boolean, - ) => new HRandFieldCommand([key, count, withValues] as any, this.opts).exec(this.client); + withValues?: boolean + ) => + new HRandFieldCommand( + [key, count, withValues] as any, + this.opts + ).exec(this.client); /** * @see https://redis.io/commands/hscan @@ -707,8 +716,15 @@ export class Redis { /** * @see https://redis.io/commands/linsert */ - linsert = (key: string, direction: "before" | "after", pivot: TData, value: TData) => - new LInsertCommand([key, direction, pivot, value], this.opts).exec(this.client); + linsert = ( + key: string, + direction: "before" | "after", + pivot: TData, + value: TData + ) => + new LInsertCommand([key, direction, pivot, value], this.opts).exec( + this.client + ); /** * @see https://redis.io/commands/llen @@ -806,10 +822,10 @@ export class Redis { pexpireat = (...args: CommandArgs) => new PExpireAtCommand(args, this.opts).exec(this.client); - /** + /** * @see https://redis.io/commands/pfadd */ - pfadd = (...args: CommandArgs) => + pfadd = (...args: CommandArgs) => new PfAddCommand(args, this.opts).exec(this.client); /** @@ -982,19 +998,24 @@ export class Redis { * @see https://redis.io/commands/smismember */ smismember = (key: string, members: TMembers) => - new SMIsMemberCommand([key, members], this.opts).exec(this.client); + new SMIsMemberCommand([key, members], this.opts).exec( + this.client + ); /** * @see https://redis.io/commands/smembers */ - smembers = (...args: CommandArgs) => - new SMembersCommand(args, this.opts).exec(this.client); + smembers = ( + ...args: CommandArgs + ) => new SMembersCommand(args, this.opts).exec(this.client); /** * @see https://redis.io/commands/smove */ smove = (source: string, destination: string, member: TData) => - new SMoveCommand([source, destination, member], this.opts).exec(this.client); + new SMoveCommand([source, destination, member], this.opts).exec( + this.client + ); /** * @see https://redis.io/commands/spop @@ -1084,23 +1105,27 @@ export class Redis { */ zadd = ( ...args: - | [key: string, scoreMember: ScoreMember, ...scoreMemberPairs: ScoreMember[]] | [ key: string, - opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr, - ...scoreMemberPairs: [ScoreMember, ...ScoreMember[]], + scoreMember: ScoreMember, + ...scoreMemberPairs: ScoreMember[] + ] + | [ + key: string, + opts: ZAddCommandOptions, + ...scoreMemberPairs: [ScoreMember, ...ScoreMember[]] ] ) => { if ("score" in args[1]) { return new ZAddCommand( [args[0], args[1] as ScoreMember, ...(args.slice(2) as any)], - this.opts, + this.opts ).exec(this.client); } return new ZAddCommand( [args[0], args[1] as any, ...(args.slice(2) as any)], - this.opts, + this.opts ).exec(this.client); }; /** @@ -1125,7 +1150,9 @@ export class Redis { * @see https://redis.io/commands/zincrby */ zincrby = (key: string, increment: number, member: TData) => - new ZIncrByCommand([key, increment, member], this.opts).exec(this.client); + new ZIncrByCommand([key, increment, member], this.opts).exec( + this.client + ); /** * @see https://redis.io/commands/zinterstore @@ -1167,13 +1194,13 @@ export class Redis { key: string, min: `(${string}` | `[${string}` | "-" | "+", max: `(${string}` | `[${string}` | "-" | "+", - opts: { byLex: true } & ZRangeCommandOptions, + opts: { byLex: true } & ZRangeCommandOptions ] | [ key: string, min: number | `(${number}` | "-inf" | "+inf", max: number | `(${number}` | "-inf" | "+inf", - opts: { byScore: true } & ZRangeCommandOptions, + opts: { byScore: true } & ZRangeCommandOptions ] ) => new ZRangeCommand(args as any, this.opts).exec(this.client);