Skip to content

Commit

Permalink
Fix import isse and run formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
ogzhanolguncu committed Oct 19, 2023
1 parent 0235c4c commit 61ad4c0
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 87 deletions.
12 changes: 9 additions & 3 deletions pkg/commands/geo_pos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Deno.test("should swallow non-existing member and return only the valid ones", a
{ longitude: 15.087269, latitude: 37.502669, member: members[1] },
{ longitude: 12.4372, latitude: 37.7981, member: members[2] },
]).exec(client);
const response = await new GeoPosCommand([key, [...members, "FooBar"]]).exec(client);
const response = await new GeoPosCommand([key, [...members, "FooBar"]]).exec(
client,
);
assertEquals(response.length, 3);
});

Expand Down Expand Up @@ -46,13 +48,17 @@ Deno.test("should return empty array due to null value FooBar", async () => {

Deno.test("should work with object members", async () => {
const key = "Sicily";
const members = [{ name: "Palermo" }, { name: "Catania" }, { name: "Marsala" }];
const members = [{ name: "Palermo" }, { name: "Catania" }, {
name: "Marsala",
}];
await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: members[0] },
{ longitude: 15.087269, latitude: 37.502669, member: members[1] },
{ longitude: 12.4372, latitude: 37.7981, member: members[2] },
]).exec(client);
const response = await new GeoPosCommand([key, [...members, "FooBar"]]).exec(client);
const response = await new GeoPosCommand([key, [...members, "FooBar"]]).exec(
client,
);
assertEquals(response.length, 3);
});
5 changes: 3 additions & 2 deletions pkg/commands/geo_pos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ type Coordinates = {
/**
* @see https://redis.io/commands/geopos
*/
export class GeoPosCommand<TMember = string> extends Command<(string | null)[][], Coordinates[]> {
export class GeoPosCommand<TMember = string>
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).
Expand Down
139 changes: 94 additions & 45 deletions pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import {
ExpireCommand,
FlushAllCommand,
FlushDBCommand,
GeoPosCommand,
GeoAddCommand,
GeoDistCommand,
GeoPosCommand,
GetBitCommand,
GetCommand,
GetDelCommand,
Expand Down Expand Up @@ -154,7 +155,6 @@ import { CommandArgs } from "./types.ts";
import { ZMScoreCommand } from "./commands/zmscore.ts";
import { HRandFieldCommand } from "./commands/hrandfield.ts";
import { ZDiffStoreCommand } from "./commands/zdiffstore.ts";
import { GeoDistCommand } from "./commands/geo_dist.ts";

// Given a tuple of commands, returns a tuple of the response data of each command
type InferResponseData<T extends unknown[]> = {
Expand Down Expand Up @@ -229,9 +229,8 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
* ```
*/
exec = async <
TCommandResults extends unknown[] = [] extends TCommands
? unknown[]
: InferResponseData<TCommands>
TCommandResults extends unknown[] = [] extends TCommands ? unknown[]
: InferResponseData<TCommands>,
>(): Promise<TCommandResults> => {
if (this.commands.length === 0) {
throw new Error("Pipeline is empty");
Expand All @@ -245,7 +244,9 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
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}`,
);
}

Expand All @@ -264,7 +265,9 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
* Pushes a command into the pipeline and returns a chainable instance of the
* pipeline
*/
private chain<T>(command: Command<any, T>): Pipeline<[...TCommands, Command<any, T>]> {
private chain<T>(
command: Command<any, T>,
): Pipeline<[...TCommands, Command<any, T>]> {
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
}
Expand All @@ -291,15 +294,22 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
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,
sourceKey: string,
...sourceKeys: string[]
) =>
this.chain(
new BitOpCommand([op as any, destinationKey, sourceKey, ...sourceKeys], this.commandOptions)
new BitOpCommand(
[op as any, destinationKey, sourceKey, ...sourceKeys],
this.commandOptions,
),
);

/**
Expand Down Expand Up @@ -436,8 +446,9 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
/**
* @see https://redis.io/commands/hgetall
*/
hgetall = <TData extends Record<string, unknown>>(...args: CommandArgs<typeof HGetAllCommand>) =>
this.chain(new HGetAllCommand<TData>(args, this.commandOptions));
hgetall = <TData extends Record<string, unknown>>(
...args: CommandArgs<typeof HGetAllCommand>
) => this.chain(new HGetAllCommand<TData>(args, this.commandOptions));

/**
* @see https://redis.io/commands/hincrby
Expand Down Expand Up @@ -466,8 +477,9 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
/**
* @see https://redis.io/commands/hmget
*/
hmget = <TData extends Record<string, unknown>>(...args: CommandArgs<typeof HMGetCommand>) =>
this.chain(new HMGetCommand<TData>(args, this.commandOptions));
hmget = <TData extends Record<string, unknown>>(
...args: CommandArgs<typeof HMGetCommand>
) => this.chain(new HMGetCommand<TData>(args, this.commandOptions));

/**
* @see https://redis.io/commands/hmset
Expand All @@ -481,9 +493,14 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
hrandfield = <TData extends string | string[] | Record<string, unknown>>(
key: string,
count?: number,
withValues?: boolean
withValues?: boolean,
) =>
this.chain(new HRandFieldCommand<TData>([key, count, withValues] as any, this.commandOptions));
this.chain(
new HRandFieldCommand<TData>(
[key, count, withValues] as any,
this.commandOptions,
),
);

/**
* @see https://redis.io/commands/hscan
Expand All @@ -501,7 +518,9 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
* @see https://redis.io/commands/hsetnx
*/
hsetnx = <TData>(key: string, field: string, value: TData) =>
this.chain(new HSetNXCommand<TData>([key, field, value], this.commandOptions));
this.chain(
new HSetNXCommand<TData>([key, field, value], this.commandOptions),
);

/**
* @see https://redis.io/commands/hstrlen
Expand Down Expand Up @@ -548,8 +567,18 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
/**
* @see https://redis.io/commands/linsert
*/
linsert = <TData>(key: string, direction: "before" | "after", pivot: TData, value: TData) =>
this.chain(new LInsertCommand<TData>([key, direction, pivot, value], this.commandOptions));
linsert = <TData>(
key: string,
direction: "before" | "after",
pivot: TData,
value: TData,
) =>
this.chain(
new LInsertCommand<TData>(
[key, direction, pivot, value],
this.commandOptions,
),
);

/**
* @see https://redis.io/commands/llen
Expand Down Expand Up @@ -579,13 +608,17 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
* @see https://redis.io/commands/lpush
*/
lpush = <TData>(key: string, ...elements: TData[]) =>
this.chain(new LPushCommand<TData>([key, ...elements], this.commandOptions));
this.chain(
new LPushCommand<TData>([key, ...elements], this.commandOptions),
);

/**
* @see https://redis.io/commands/lpushx
*/
lpushx = <TData>(key: string, ...elements: TData[]) =>
this.chain(new LPushXCommand<TData>([key, ...elements], this.commandOptions));
this.chain(
new LPushXCommand<TData>([key, ...elements], this.commandOptions),
);

/**
* @see https://redis.io/commands/lrange
Expand Down Expand Up @@ -657,7 +690,9 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
* @see https://redis.io/commands/psetex
*/
psetex = <TData>(key: string, ttl: number, value: TData) =>
this.chain(new PSetEXCommand<TData>([key, ttl, value], this.commandOptions));
this.chain(
new PSetEXCommand<TData>([key, ttl, value], this.commandOptions),
);

/**
* @see https://redis.io/commands/pttl
Expand Down Expand Up @@ -804,20 +839,28 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
/**
* @see https://redis.io/commands/smembers
*/
smembers = <TData extends unknown[] = string[]>(...args: CommandArgs<typeof SMembersCommand>) =>
this.chain(new SMembersCommand<TData>(args, this.commandOptions));
smembers = <TData extends unknown[] = string[]>(
...args: CommandArgs<typeof SMembersCommand>
) => this.chain(new SMembersCommand<TData>(args, this.commandOptions));

/**
* @see https://redis.io/commands/smismember
*/
smismember = <TMembers extends unknown[]>(key: string, members: TMembers) =>
this.chain(new SMIsMemberCommand<TMembers>([key, members], this.commandOptions));
this.chain(
new SMIsMemberCommand<TMembers>([key, members], this.commandOptions),
);

/**
* @see https://redis.io/commands/smove
*/
smove = <TData>(source: string, destination: string, member: TData) =>
this.chain(new SMoveCommand<TData>([source, destination, member], this.commandOptions));
this.chain(
new SMoveCommand<TData>(
[source, destination, member],
this.commandOptions,
),
);

/**
* @see https://redis.io/commands/spop
Expand Down Expand Up @@ -895,27 +938,31 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
*/
zadd = <TData>(
...args:
| [key: string, scoreMember: ScoreMember<TData>, ...scoreMemberPairs: ScoreMember<TData>[]]
| [
key: string,
opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr,
...scoreMemberPairs: [ScoreMember<TData>, ...ScoreMember<TData>[]]
]
key: string,
scoreMember: ScoreMember<TData>,
...scoreMemberPairs: ScoreMember<TData>[],
]
| [
key: string,
opts: ZAddCommandOptions | ZAddCommandOptionsWithIncr,
...scoreMemberPairs: [ScoreMember<TData>, ...ScoreMember<TData>[]],
]
) => {
if ("score" in args[1]) {
return this.chain(
new ZAddCommand<TData>(
[args[0], args[1] as ScoreMember<TData>, ...(args.slice(2) as any)],
this.commandOptions
)
this.commandOptions,
),
);
}

return this.chain(
new ZAddCommand<TData>(
[args[0], args[1] as any, ...(args.slice(2) as any)],
this.commandOptions
)
this.commandOptions,
),
);
};

Expand All @@ -935,7 +982,9 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
* @see https://redis.io/commands/zincrby
*/
zincrby = <TData>(key: string, increment: number, member: TData) =>
this.chain(new ZIncrByCommand<TData>([key, increment, member], this.commandOptions));
this.chain(
new ZIncrByCommand<TData>([key, increment, member], this.commandOptions),
);

/**
* @see https://redis.io/commands/zinterstore
Expand Down Expand Up @@ -974,17 +1023,17 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
...args:
| [key: string, min: number, max: number, opts?: ZRangeCommandOptions]
| [
key: string,
min: `(${string}` | `[${string}` | "-" | "+",
max: `(${string}` | `[${string}` | "-" | "+",
opts: { byLex: true } & ZRangeCommandOptions
]
key: string,
min: `(${string}` | `[${string}` | "-" | "+",
max: `(${string}` | `[${string}` | "-" | "+",
opts: { byLex: true } & ZRangeCommandOptions,
]
| [
key: string,
min: number | `(${number}` | "-inf" | "+inf",
max: number | `(${number}` | "-inf" | "+inf",
opts: { byScore: true } & ZRangeCommandOptions
]
key: string,
min: number | `(${number}` | "-inf" | "+inf",
max: number | `(${number}` | "-inf" | "+inf",
opts: { byScore: true } & ZRangeCommandOptions,
]
) => this.chain(new ZRangeCommand<TData>(args as any, this.commandOptions));

/**
Expand Down
Loading

0 comments on commit 61ad4c0

Please sign in to comment.