From 732f1f2fe2744b49f2e3f899e4215f235457d146 Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Fri, 27 Oct 2023 11:37:23 +0300 Subject: [PATCH 1/2] Remove exec from pipelined commands --- pkg/pipeline.ts | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/pkg/pipeline.ts b/pkg/pipeline.ts index dcc80741..23d2ae7e 100644 --- a/pkg/pipeline.ts +++ b/pkg/pipeline.ts @@ -17,12 +17,12 @@ import { ExpireCommand, FlushAllCommand, FlushDBCommand, - GeoHashCommand, GeoAddCommand, GeoDistCommand, - GeoSearchStoreCommand, - GeoSearchCommand, + GeoHashCommand, GeoPosCommand, + GeoSearchCommand, + GeoSearchStoreCommand, GetBitCommand, GetCommand, GetDelCommand, @@ -234,7 +234,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"); @@ -248,7 +248,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}`, ); } @@ -302,7 +302,7 @@ 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), ); /** @@ -484,7 +484,7 @@ export class Pipeline[] = []> { hrandfield = >( key: string, count?: number, - withValues?: boolean + withValues?: boolean, ) => this.chain(new HRandFieldCommand([key, count, withValues] as any, this.commandOptions)); @@ -902,23 +902,23 @@ export class Pipeline[] = []> { | [ key: string, opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr, - ...scoreMemberPairs: [ScoreMember, ...ScoreMember[]] + ...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, + ), ); }; @@ -980,13 +980,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)); @@ -1113,37 +1113,37 @@ export class Pipeline[] = []> { * @see https://redis.io/commands/geoadd */ geoadd: (...args: CommandArgs) => - new GeoAddCommand(args, this.commandOptions).exec(this.client), + this.chain(new GeoAddCommand(args, this.commandOptions)), /** * @see https://redis.io/commands/geodist */ geodist: (...args: CommandArgs) => - new GeoDistCommand(args, this.commandOptions).exec(this.client), + this.chain(new GeoDistCommand(args, this.commandOptions)), /** * @see https://redis.io/commands/geopos */ geopos: (...args: CommandArgs) => - new GeoPosCommand(args, this.commandOptions).exec(this.client), + this.chain(new GeoPosCommand(args, this.commandOptions)), /** * @see https://redis.io/commands/geohash */ geohash: (...args: CommandArgs) => - new GeoHashCommand(args, this.commandOptions).exec(this.client), + this.chain(new GeoHashCommand(args, this.commandOptions)), /** * @see https://redis.io/commands/geosearch */ geosearch: (...args: CommandArgs) => - new GeoSearchCommand(args, this.commandOptions).exec(this.client), + this.chain(new GeoSearchCommand(args, this.commandOptions)), /** * @see https://redis.io/commands/geosearchstore */ geosearchstore: (...args: CommandArgs) => - new GeoSearchStoreCommand(args, this.commandOptions).exec(this.client), + this.chain(new GeoSearchStoreCommand(args, this.commandOptions)), /** * @see https://redis.io/commands/json.get From 4c36caf73852bc06288cb2c7ead265f3ef76b6f1 Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Fri, 27 Oct 2023 11:38:13 +0300 Subject: [PATCH 2/2] Format previously unformatted files --- pkg/commands/geo_hash.ts | 8 +++++--- pkg/commands/geo_pos.ts | 2 +- pkg/commands/geo_search.test.ts | 2 +- pkg/commands/geo_search.ts | 15 ++++++++------- pkg/commands/geo_search_store.test.ts | 8 ++++---- pkg/commands/geo_search_store.ts | 6 +++--- pkg/redis.ts | 24 ++++++++++++------------ 7 files changed, 34 insertions(+), 31 deletions(-) diff --git a/pkg/commands/geo_hash.ts b/pkg/commands/geo_hash.ts index 740bf954..57008066 100644 --- a/pkg/commands/geo_hash.ts +++ b/pkg/commands/geo_hash.ts @@ -3,10 +3,12 @@ import { Command, CommandOptions } from "./command.ts"; /** * @see https://redis.io/commands/geohash */ -export class GeoHashCommand - extends Command<(string | null)[], (string | null)[]> { +export class GeoHashCommand extends Command< + (string | null)[], + (string | null)[] +> { constructor( - cmd: [string, ...TMember[] | TMember[]], + cmd: [string, ...(TMember[] | TMember[])], opts?: CommandOptions<(string | null)[], (string | null)[]>, ) { const [key] = cmd; diff --git a/pkg/commands/geo_pos.ts b/pkg/commands/geo_pos.ts index 072ac9a6..2533a6d2 100644 --- a/pkg/commands/geo_pos.ts +++ b/pkg/commands/geo_pos.ts @@ -11,7 +11,7 @@ type Coordinates = { export class GeoPosCommand extends Command<(string | null)[][], Coordinates[]> { constructor( cmd: [string, ...(TMember[] | TMember[])], - opts?: CommandOptions<(string | null)[][], Coordinates[]> + opts?: CommandOptions<(string | null)[][], Coordinates[]>, ) { const [key] = cmd; // Check if the second argument is an array of strings (members). diff --git a/pkg/commands/geo_search.test.ts b/pkg/commands/geo_search.test.ts index 2c370774..a8921958 100644 --- a/pkg/commands/geo_search.test.ts +++ b/pkg/commands/geo_search.test.ts @@ -1,4 +1,4 @@ -import { describe, test, expect, afterAll } from "bun:test"; +import { afterAll, describe, expect, test } from "bun:test"; import { keygen, newHttpClient } from "../test-utils.ts"; import { GeoAddCommand } from "./geo_add.ts"; diff --git a/pkg/commands/geo_search.ts b/pkg/commands/geo_search.ts index 647c01bc..91cc04fb 100644 --- a/pkg/commands/geo_search.ts +++ b/pkg/commands/geo_search.ts @@ -33,9 +33,10 @@ type OptionMappings = { }; type GeoSearchOptions = { - [K in keyof TOptions as K extends keyof OptionMappings - ? OptionMappings[K] - : never]: K extends "withHash" + [K in + keyof TOptions as K extends keyof OptionMappings + ? OptionMappings[K] + : never]: K extends "withHash" ? string : K extends "withCoord" ? { long: number; lat: number } @@ -53,7 +54,7 @@ type GeoSearchResponse = ({ */ export class GeoSearchCommand< TMemberType = string, - TOptions extends GeoSearchCommandOptions = GeoSearchCommandOptions + TOptions extends GeoSearchCommandOptions = GeoSearchCommandOptions, > extends Command> { constructor( [key, centerPoint, shape, order, opts]: [ @@ -61,9 +62,9 @@ export class GeoSearchCommand< centerPoint: CenterPoint, shape: Shape, order: "ASC" | "DESC" | "asc" | "desc", - opts?: TOptions + opts?: TOptions, ], - commandOptions?: CommandOptions> + commandOptions?: CommandOptions>, ) { const command: unknown[] = ["GEOSEARCH", key]; @@ -133,7 +134,7 @@ export class GeoSearchCommand< { ...commandOptions, deserialize: transform, - } + }, ); } } diff --git a/pkg/commands/geo_search_store.test.ts b/pkg/commands/geo_search_store.test.ts index 667832b8..58e255ac 100644 --- a/pkg/commands/geo_search_store.test.ts +++ b/pkg/commands/geo_search_store.test.ts @@ -1,4 +1,4 @@ -import { expect, test, describe, afterAll } from "bun:test"; +import { afterAll, describe, expect, test } from "bun:test"; import { keygen, newHttpClient } from "../test-utils.ts"; import { GeoAddCommand } from "./geo_add.ts"; @@ -32,7 +32,7 @@ describe("GEOSSEARCHSTORE tests", () => { "ASC", ]).exec(client); const zrangeRes = await new ZRangeCommand([destination, 0, -1, { withScores: true }]).exec( - client + client, ); expect(zrangeRes).toEqual([ "Empire State Building", @@ -68,7 +68,7 @@ describe("GEOSSEARCHSTORE tests", () => { { storeDist: true }, ]).exec(client); const zrangeRes = await new ZRangeCommand([destination, 0, -1, { withScores: true }]).exec( - client + client, ); expect(zrangeRes).toEqual([ "Empire State Building", @@ -104,7 +104,7 @@ describe("GEOSSEARCHSTORE tests", () => { { storeDist: true }, ]).exec(client); const zrangeRes = await new ZRangeCommand([destination, 0, -1, { withScores: true }]).exec( - client + client, ); expect(zrangeRes).toEqual([ { name: "Empire State Building" }, diff --git a/pkg/commands/geo_search_store.ts b/pkg/commands/geo_search_store.ts index d4951a78..68f8000f 100644 --- a/pkg/commands/geo_search_store.ts +++ b/pkg/commands/geo_search_store.ts @@ -29,7 +29,7 @@ type GeoSearchCommandOptions = { */ export class GeoSearchStoreCommand< TMemberType = string, - TOptions extends GeoSearchCommandOptions = GeoSearchCommandOptions + TOptions extends GeoSearchCommandOptions = GeoSearchCommandOptions, > extends Command { constructor( [destination, key, centerPoint, shape, order, opts]: [ @@ -38,9 +38,9 @@ export class GeoSearchStoreCommand< centerPoint: CenterPoint, shape: Shape, order: "ASC" | "DESC" | "asc" | "desc", - opts?: TOptions + opts?: TOptions, ], - commandOptions?: CommandOptions + commandOptions?: CommandOptions, ) { const command: unknown[] = ["GEOSEARCHSTORE", destination, key]; diff --git a/pkg/redis.ts b/pkg/redis.ts index 40c707b9..8a291f90 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -18,10 +18,10 @@ import { FlushDBCommand, GeoAddCommand, GeoDistCommand, - GeoSearchStoreCommand, - GeoSearchCommand, GeoHashCommand, GeoPosCommand, + GeoSearchCommand, + GeoSearchStoreCommand, GetBitCommand, GetCommand, GetDelCommand, @@ -362,8 +362,8 @@ 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; @@ -446,7 +446,7 @@ export class Redis { ...sourceKeys: string[] ) => new BitOpCommand([op as any, destinationKey, sourceKey, ...sourceKeys], this.opts).exec( - this.client + this.client, ); /** @@ -626,12 +626,12 @@ export class Redis { >( key: string, count: number, - withValues: boolean + withValues: boolean, ): Promise>; } = >( key: string, count?: number, - withValues?: boolean + withValues?: boolean, ) => new HRandFieldCommand([key, count, withValues] as any, this.opts).exec(this.client); /** @@ -1060,19 +1060,19 @@ export class Redis { | [ key: string, opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr, - ...scoreMemberPairs: [ScoreMember, ...ScoreMember[]] + ...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); }; /** @@ -1139,13 +1139,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);