Skip to content

Commit

Permalink
feat: Allow Number and Uint8Array values as argumets (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored Mar 27, 2021
1 parent 3bc7ea4 commit a9518b1
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 141 deletions.
107 changes: 58 additions & 49 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ConditionalArray,
Integer,
Raw,
RedisValue,
SimpleString,
} from "./protocol/mod.ts";
import type { RedisPipeline } from "./pipeline.ts";
Expand Down Expand Up @@ -216,9 +217,9 @@ export interface RedisCommands {
// Connection
auth(password: string): Promise<SimpleString>;
auth(username: string, password: string): Promise<SimpleString>;
echo(message: string): Promise<BulkString>;
echo(message: RedisValue): Promise<BulkString>;
ping(): Promise<SimpleString>;
ping(message: string): Promise<BulkString>;
ping(message: RedisValue): Promise<BulkString>;
quit(): Promise<SimpleString>;
select(index: number): Promise<SimpleString>;

Expand Down Expand Up @@ -275,7 +276,7 @@ export interface RedisCommands {
wait(numreplicas: number, timeout: number): Promise<Integer>;

// String
append(key: string, value: string): Promise<Integer>;
append(key: string, value: RedisValue): Promise<Integer>;
bitcount(key: string): Promise<Integer>;
bitcount(key: string, start: number, end: number): Promise<Integer>;
bitfield(
Expand All @@ -302,36 +303,36 @@ export interface RedisCommands {
get(key: string): Promise<Bulk>;
getbit(key: string, offset: number): Promise<Integer>;
getrange(key: string, start: number, end: number): Promise<BulkString>;
getset(key: string, value: string): Promise<Bulk>;
getset(key: string, value: RedisValue): Promise<Bulk>;
incr(key: string): Promise<Integer>;
incrby(key: string, increment: number): Promise<Integer>;
incrbyfloat(key: string, increment: number): Promise<BulkString>;
mget(...keys: string[]): Promise<Bulk[]>;
mset(key: string, value: string): Promise<SimpleString>;
mset(...key_values: [string, string][]): Promise<SimpleString>;
mset(key_values: Record<string, string>): Promise<SimpleString>;
msetnx(key: string, value: string): Promise<Integer>;
msetnx(...key_values: [string, string][]): Promise<Integer>;
msetnx(key_values: Record<string, string>): Promise<Integer>;
mset(key: string, value: RedisValue): Promise<SimpleString>;
mset(...key_values: [string, RedisValue][]): Promise<SimpleString>;
mset(key_values: Record<string, RedisValue>): Promise<SimpleString>;
msetnx(key: string, value: RedisValue): Promise<Integer>;
msetnx(...key_values: [string, RedisValue][]): Promise<Integer>;
msetnx(key_values: Record<string, RedisValue>): Promise<Integer>;
psetex(
key: string,
milliseconds: number,
value: string,
value: RedisValue,
): Promise<SimpleString>;
set(
key: string,
value: string,
value: RedisValue,
opts?: SetOpts,
): Promise<SimpleString>;
set(
key: string,
value: string,
value: RedisValue,
opts?: SetWithModeOpts,
): Promise<SimpleString | BulkNil>;
setbit(key: string, offset: number, value: string): Promise<Integer>;
setex(key: string, seconds: number, value: string): Promise<SimpleString>;
setnx(key: string, value: string): Promise<Integer>;
setrange(key: string, offset: number, value: string): Promise<Integer>;
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>;
setrange(key: string, offset: number, value: RedisValue): Promise<Integer>;
stralgo(
algorithm: StralgoAlgorithm,
target: StralgoTarget,
Expand Down Expand Up @@ -402,20 +403,20 @@ export interface RedisCommands {
/**
* @deprecated since 4.0.0, use hset
*/
hmset(key: string, field: string, value: string): Promise<SimpleString>;
hmset(key: string, field: string, value: RedisValue): Promise<SimpleString>;
/**
* @deprecated since 4.0.0, use hset
*/
hmset(
key: string,
...field_values: [string, string][]
...field_values: [string, RedisValue][]
): Promise<SimpleString>;
/**
* @deprecated since 4.0.0, use hset
*/
hmset(
key: string,
field_values: Record<string, string>,
field_values: Record<string, RedisValue>,
): Promise<SimpleString>;
hscan(
key: string,
Expand All @@ -427,20 +428,20 @@ export interface RedisCommands {
* @description Sets `field` in the hash to `value`.
* @see https://redis.io/commands/hset
*/
hset(key: string, field: string, value: string): Promise<Integer>;
hset(key: string, field: string, value: RedisValue): Promise<Integer>;

/**
* @description Sets the field-value pairs specified by `fieldValues` to the hash stored at `key`.
* NOTE: Variadic form for `HSET` is supported only in Redis v4.0.0 or higher.
*/
hset(key: string, ...fieldValues: [string, string][]): Promise<Integer>;
hset(key: string, ...fieldValues: [string, RedisValue][]): Promise<Integer>;

/**
* @description Sets the field-value pairs specified by `fieldValues` to the hash stored at `key`.
* NOTE: Variadic form for `HSET` is supported only in Redis v4.0.0 or higher.
*/
hset(key: string, fieldValues: Record<string, string>): Promise<Integer>;
hsetnx(key: string, field: string, value: string): Promise<Integer>;
hset(key: string, fieldValues: Record<string, RedisValue>): Promise<Integer>;
hsetnx(key: string, field: string, value: RedisValue): Promise<Integer>;
hstrlen(key: string, field: string): Promise<Integer>;
hvals(key: string): Promise<BulkString[]>;

Expand All @@ -463,7 +464,7 @@ export interface RedisCommands {
key: string,
loc: LInsertLocation,
pivot: string,
value: string,
value: RedisValue,
): Promise<Integer>;
llen(key: string): Promise<Integer>;
lpop(key: string): Promise<Bulk>;
Expand All @@ -474,7 +475,7 @@ export interface RedisCommands {
*/
lpos(
key: string,
element: string,
element: RedisValue,
opts?: LPosOpts,
): Promise<Integer | BulkNil>;

Expand All @@ -486,20 +487,20 @@ export interface RedisCommands {
*/
lpos(
key: string,
element: string,
element: RedisValue,
opts: LPosWithCountOpts,
): Promise<Integer[]>;

lpush(key: string, ...elements: string[]): Promise<Integer>;
lpushx(key: string, ...elements: string[]): Promise<Integer>;
lpush(key: string, ...elements: RedisValue[]): Promise<Integer>;
lpushx(key: string, ...elements: RedisValue[]): Promise<Integer>;
lrange(key: string, start: number, stop: number): Promise<BulkString[]>;
lrem(key: string, count: number, element: string): Promise<Integer>;
lset(key: string, index: number, element: string): Promise<SimpleString>;
lrem(key: string, count: number, element: RedisValue): Promise<Integer>;
lset(key: string, index: number, element: RedisValue): Promise<SimpleString>;
ltrim(key: string, start: number, stop: number): Promise<SimpleString>;
rpop(key: string): Promise<Bulk>;
rpoplpush(source: string, destination: string): Promise<Bulk>;
rpush(key: string, ...elements: string[]): Promise<Integer>;
rpushx(key: string, ...elements: string[]): Promise<Integer>;
rpush(key: string, ...elements: RedisValue[]): Promise<Integer>;
rpushx(key: string, ...elements: RedisValue[]): Promise<Integer>;

// HyperLogLog
pfadd(key: string, ...elements: string[]): Promise<Integer>;
Expand All @@ -513,26 +514,30 @@ export interface RedisCommands {
pubsubChannels(pattern?: string): Promise<BulkString[]>;
pubsubNumsub(...channels: string[]): Promise<(BulkString | Integer)[]>;
pubsubNumpat(): Promise<Integer>;
publish(channel: string, message: string): Promise<Integer>;
publish(channel: string, message: RedisValue): Promise<Integer>;
subscribe<TMessage extends string | string[] = string>(
...channels: string[]
): Promise<RedisSubscription<TMessage>>;

// Set
sadd(key: string, ...members: string[]): Promise<Integer>;
sadd(key: string, ...members: RedisValue[]): Promise<Integer>;
scard(key: string): Promise<Integer>;
sdiff(...keys: string[]): Promise<BulkString[]>;
sdiffstore(destination: string, ...keys: string[]): Promise<Integer>;
sinter(...keys: string[]): Promise<BulkString[]>;
sinterstore(destination: string, ...keys: string[]): Promise<Integer>;
sismember(key: string, member: string): Promise<Integer>;
sismember(key: string, member: RedisValue): Promise<Integer>;
smembers(key: string): Promise<BulkString[]>;
smove(source: string, destination: string, member: string): Promise<Integer>;
smove(
source: string,
destination: string,
member: RedisValue,
): Promise<Integer>;
spop(key: string): Promise<Bulk>;
spop(key: string, count: number): Promise<BulkString[]>;
srandmember(key: string): Promise<Bulk>;
srandmember(key: string, count: number): Promise<BulkString[]>;
srem(key: string, ...members: string[]): Promise<Integer>;
srem(key: string, ...members: RedisValue[]): Promise<Integer>;
sscan(
key: string,
cursor: number,
Expand Down Expand Up @@ -900,28 +905,32 @@ XRANGE somestream - +
zadd(
key: string,
score: number,
member: string,
member: RedisValue,
opts?: ZAddOpts,
): Promise<Integer>;
zadd(
key: string,
score_members: [number, string][],
score_members: [number, RedisValue][],
opts?: ZAddOpts,
): Promise<Integer>;
zadd(
key: string,
member_scores: Record<string, number>,
member_scores: Record<string | number, number>,
opts?: ZAddOpts,
): Promise<Integer>;
zaddIncr(
key: string,
score: number,
member: string,
member: RedisValue,
opts?: ZAddOpts,
): Promise<Bulk>;
zcard(key: string): Promise<Integer>;
zcount(key: string, min: number, max: number): Promise<Integer>;
zincrby(key: string, increment: number, member: string): Promise<BulkString>;
zincrby(
key: string,
increment: number,
member: RedisValue,
): Promise<BulkString>;
zinterstore(
destination: string,
keys: string[],
Expand Down Expand Up @@ -958,8 +967,8 @@ XRANGE somestream - +
max: number | string,
opts?: ZRangeByScoreOpts,
): Promise<BulkString[]>;
zrank(key: string, member: string): Promise<Integer | BulkNil>;
zrem(key: string, ...members: string[]): Promise<Integer>;
zrank(key: string, member: RedisValue): Promise<Integer | BulkNil>;
zrem(key: string, ...members: RedisValue[]): Promise<Integer>;
zremrangebylex(key: string, min: string, max: string): Promise<Integer>;
zremrangebyrank(key: string, start: number, stop: number): Promise<Integer>;
zremrangebyscore(
Expand All @@ -985,13 +994,13 @@ XRANGE somestream - +
min: number | string,
opts?: ZRangeByScoreOpts,
): Promise<BulkString[]>;
zrevrank(key: string, member: string): Promise<Integer | BulkNil>;
zrevrank(key: string, member: RedisValue): Promise<Integer | BulkNil>;
zscan(
key: string,
cursor: number,
opts?: ZScanOpts,
): Promise<[BulkString, BulkString[]]>;
zscore(key: string, member: string): Promise<Bulk>;
zscore(key: string, member: RedisValue): Promise<Bulk>;
zunionstore(
destination: string,
keys: string[],
Expand Down Expand Up @@ -1178,8 +1187,8 @@ XRANGE somestream - +
time(): Promise<[BulkString, BulkString]>;

// Scripting
eval(script: string, keys: string[], args: string[]): Promise<Raw>;
evalsha(sha1: string, keys: string[], args: string[]): Promise<Raw>;
eval(script: string, keys: string[], args: RedisValue[]): Promise<Raw>;
evalsha(sha1: string, keys: string[], args: RedisValue[]): Promise<Raw>;
scriptDebug(mode: ScriptDebugMode): Promise<SimpleString>;
scriptExists(...sha1s: string[]): Promise<Integer[]>;
scriptFlush(): Promise<SimpleString>;
Expand Down
8 changes: 4 additions & 4 deletions executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Connection } from "./connection.ts";
import { EOFError } from "./errors.ts";
import { Deferred, deferred } from "./vendor/https/deno.land/std/async/mod.ts";
import { sendCommand } from "./protocol/mod.ts";
import type { RedisReply } from "./protocol/mod.ts";
import type { RedisReply, RedisValue } from "./protocol/mod.ts";

export abstract class CommandExecutor {
connection: Connection;
Expand All @@ -13,20 +13,20 @@ export abstract class CommandExecutor {

abstract exec(
command: string,
...args: (string | number)[]
...args: RedisValue[]
): Promise<RedisReply>;
}

export class MuxExecutor extends CommandExecutor {
private queue: {
command: string;
args: (string | number)[];
args: RedisValue[];
d: Deferred<RedisReply>;
}[] = [];

exec(
command: string,
...args: (string | number)[]
...args: RedisValue[]
): Promise<RedisReply> {
const d = deferred<RedisReply>();
this.queue.push({ command, args, d });
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type {
Raw,
RedisReply,
RedisReplyOrError,
RedisValue,
Reply,
SimpleString,
SimpleStringReply,
Expand Down
7 changes: 4 additions & 3 deletions pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createSimpleStringReply,
RedisReply,
RedisReplyOrError,
RedisValue,
sendCommands,
} from "./protocol/mod.ts";
import { Redis, RedisImpl } from "./redis.ts";
Expand All @@ -28,12 +29,12 @@ export function createRedisPipeline(
export class PipelineExecutor extends CommandExecutor {
private commands: {
command: string;
args: (number | string)[];
args: RedisValue[];
}[] = [];
private queue: {
commands: {
command: string;
args: (number | string)[];
args: RedisValue[];
}[];
d: Deferred<RedisReplyOrError[]>;
}[] = [];
Expand All @@ -44,7 +45,7 @@ export class PipelineExecutor extends CommandExecutor {

exec(
command: string,
...args: (string | number)[]
...args: RedisValue[]
): Promise<RedisReply> {
this.commands.push({ command, args });
return Promise.resolve(createSimpleStringReply("OK"));
Expand Down
Loading

0 comments on commit a9518b1

Please sign in to comment.