Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: Add binary variant to connection management commands. #2160

Merged
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#### Changes
* Node: Added/updated binary variant to connection management commands and WATCH/UNWATCH ([#2160](https:/valkey-io/valkey-glide/pull/2160))
* Node: Added FUNCTION DUMP and FUNCTION RESTORE commands ([#2129](https:/valkey-io/valkey-glide/pull/2129))
* Node: Added binary variant to generic commands ([#2158](https:/valkey-io/valkey-glide/pull/2158))
* Node: Added binary variant to geo commands ([#2149](https:/valkey-io/valkey-glide/pull/2149))
* Node: Added binary variant to HYPERLOGLOG commands ([#2176](https:/valkey-io/valkey-glide/pull/2176))
* Node: Added FUNCTION DUMP and FUNCTION RESTORE commands ([#2129](https:/valkey-io/valkey-glide/pull/2129), [#2173](https:/valkey-io/valkey-glide/pull/2173))
* Node: Added ZUNIONSTORE command ([#2145](https:/valkey-io/valkey-glide/pull/2145))
* Node: Added XREADGROUP command ([#2124](https:/valkey-io/valkey-glide/pull/2124))
* Node: Added XINFO GROUPS command ([#2122](https:/valkey-io/valkey-glide/pull/2122))
Expand Down
47 changes: 30 additions & 17 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,16 @@ export class GlideClient extends BaseClient {
}

/**
* Pings the Redis server.
* Pings the server.
*
* @see {@link https://valkey.io/commands/ping/|valkey.io} for details.
*
* @param message - (Optional) A message to include in the PING command.
* If not provided, the server will respond with "PONG".
* If provided, the server will respond with a copy of the message.
* @returns - "PONG" if `message` is not provided, otherwise return a copy of `message`.
* @param options - (Optional) Additional parameters:
* - (Optional) `message` : a message to include in the `PING` command.
* + If not provided, the server will respond with "PONG".
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
* + If provided, the server will respond with a copy of the message.
* - (Optional) `decoder`: see {@link DecoderOption}.
* @returns "PONG" if `message` is not provided, otherwise return a copy of `message`.
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ```typescript
Expand All @@ -248,12 +250,13 @@ export class GlideClient extends BaseClient {
* console.log(result); // Output: 'Hello'
* ```
*/
public async ping(message?: GlideString): Promise<GlideString> {
return this.createWritePromise(createPing(message), {
decoder:
!message || typeof message === "string"
? Decoder.String
: Decoder.Bytes,
public async ping(
options?: {
message?: GlideString;
} & DecoderOption,
): Promise<GlideString> {
return this.createWritePromise(createPing(options?.message), {
decoder: options?.decoder,
});
}

Expand All @@ -269,12 +272,12 @@ export class GlideClient extends BaseClient {
}

/**
* Changes the currently selected Redis database.
* Changes the currently selected database.
*
* @see {@link https://valkey.io/commands/select/|valkey.io} for details.
*
* @param index - The index of the database to select.
* @returns A simple `"OK"` response.
* @returns A simple "OK" response.
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ```typescript
Expand Down Expand Up @@ -349,7 +352,13 @@ export class GlideClient extends BaseClient {
*
* @see {@link https://valkey.io/commands/client-id/|valkey.io} for details.
*
* @returns The ID of the client.
* @returns The ID of the connection.
*
* @example
* ```typescript
* const result = await client.clientId();
* console.log("Connection id: " + result);
* ```
*/
public async clientId(): Promise<number> {
return this.createWritePromise(createClientId());
Expand Down Expand Up @@ -400,6 +409,8 @@ export class GlideClient extends BaseClient {
* @see {@link https://valkey.io/commands/echo|valkey.io} for more details.
*
* @param message - The message to be echoed back.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response.
* If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used.
* @returns The provided `message`.
*
* @example
Expand All @@ -409,10 +420,12 @@ export class GlideClient extends BaseClient {
* console.log(echoedMessage); // Output: 'valkey-glide'
* ```
*/
public async echo(message: GlideString): Promise<GlideString> {
public async echo(
message: GlideString,
decoder?: Decoder,
): Promise<GlideString> {
return this.createWritePromise(createEcho(message), {
decoder:
typeof message === "string" ? Decoder.String : Decoder.Bytes,
decoder: decoder,
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
86 changes: 48 additions & 38 deletions node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,19 @@ export class GlideClusterClient extends BaseClient {
}

/**
* Pings the Redis server.
* Pings the server.
*
* The command will be routed to all primary nodes, unless `route` is provided.
*
* @see {@link https://valkey.io/commands/ping/|valkey.io} for details.
*
* @param message - (Optional) A message to include in the PING command.
* If not provided, the server will respond with "PONG".
* If provided, the server will respond with a copy of the message.
* @param route - (Optional) The command will be routed to all primaries, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
* @returns - "PONG" if `message` is not provided, otherwise return a copy of `message`.
* @param options - (Optional) Additional parameters:
* - (Optional) `message` : a message to include in the `PING` command.
* + If not provided, the server will respond with "PONG".
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
* + If provided, the server will respond with a copy of the message.
* - (Optional) `route`: see {@link RouteOption}.
* - (Optional) `decoder`: see {@link DecoderOption}.
* @returns "PONG" if `message` is not provided, otherwise return a copy of `message`.
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ```typescript
Expand All @@ -434,16 +437,15 @@ export class GlideClusterClient extends BaseClient {
* console.log(result); // Output: 'Hello'
* ```
*/
public async ping(options?: {
message?: GlideString;
route?: Routes;
}): Promise<GlideString> {
public async ping(
options?: {
message?: GlideString;
} & RouteOption &
DecoderOption,
): Promise<GlideString> {
return this.createWritePromise(createPing(options?.message), {
route: toProtobufRoute(options?.route),
decoder:
!options?.message || typeof options?.message === "string"
? Decoder.String
: Decoder.Bytes,
decoder: options?.decoder,
});
}

Expand All @@ -470,12 +472,11 @@ export class GlideClusterClient extends BaseClient {
/**
* Gets the name of the connection to which the request is routed.
*
* The command will be routed to a random node, unless `route` is provided.
*
* @see {@link https://valkey.io/commands/client-getname/|valkey.io} for details.
*
* @param route - (Optional) The command will be routed a random node, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response.
* If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used.
* @param options - (Optional) See {@link RouteOption} and {@link DecoderOption}.
*
* @returns - The name of the client connection as a string if a name is set, or `null` if no name is assigned.
* When specifying a route other than a single node, it returns a dictionary where each address is the key and
Expand All @@ -495,10 +496,9 @@ export class GlideClusterClient extends BaseClient {
* console.log(result); // Output: {'addr': 'Connection Name', 'addr2': 'Connection Name', 'addr3': 'Connection Name'}
* ```
*/
public async clientGetName(options?: {
route?: Routes;
decoder?: Decoder;
}): Promise<ClusterResponse<GlideString | null>> {
public async clientGetName(
options?: RouteOption & DecoderOption,
): Promise<ClusterResponse<GlideString | null>> {
return this.createWritePromise<ClusterResponse<GlideString | null>>(
createClientGetName(),
{
Expand Down Expand Up @@ -551,17 +551,26 @@ export class GlideClusterClient extends BaseClient {
/**
* Returns the current connection ID.
*
* The command will be routed to a random node, unless `route` is provided.
*
* @see {@link https://valkey.io/commands/client-id/|valkey.io} for details.
*
* @param route - (Optional) The command will be routed to a random node, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
* @returns The ID of the client. When specifying a route other than a single node,
* @param options - (Optional) See {@link RouteOption}.
* @returns The ID of the connection. When specifying a route other than a single node,
* it returns a dictionary where each address is the key and its corresponding node response is the value.
*
* @example
* ```typescript
* const result = await client.clientId();
* console.log("Connection id: " + result);
* ```
*/
public async clientId(route?: Routes): Promise<ClusterResponse<number>> {
public async clientId(
options?: RouteOption,
): Promise<ClusterResponse<number>> {
return this.createWritePromise<ClusterResponse<number>>(
createClientId(),
{ route: toProtobufRoute(route) },
{ route: toProtobufRoute(options?.route) },
);
}

Expand Down Expand Up @@ -628,11 +637,12 @@ export class GlideClusterClient extends BaseClient {
/**
* Echoes the provided `message` back.
*
* The command will be routed to a random node, unless `route` is provided.
*
* @see {@link https://valkey.io/commands/echo/|valkey.io} for details.
*
* @param message - The message to be echoed back.
* @param route - (Optional) The command will be routed to a random node, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
* @param options - (Optional) See {@link RouteOption} and {@link DecoderOption}.
* @returns The provided `message`. When specifying a route other than a single node,
* it returns a dictionary where each address is the key and its corresponding node response is the value.
*
Expand All @@ -651,12 +661,11 @@ export class GlideClusterClient extends BaseClient {
*/
public async echo(
message: GlideString,
route?: Routes,
options?: RouteOption & DecoderOption,
): Promise<ClusterResponse<GlideString>> {
return this.createWritePromise(createEcho(message), {
route: toProtobufRoute(route),
decoder:
typeof message === "string" ? Decoder.String : Decoder.Bytes,
route: toProtobufRoute(options?.route),
decoder: options?.decoder,
});
}

Expand Down Expand Up @@ -1359,10 +1368,11 @@ export class GlideClusterClient extends BaseClient {
* Flushes all the previously watched keys for a transaction. Executing a transaction will
* automatically flush all previously watched keys.
*
* The command will be routed to all primary nodes, unless `route` is provided
*
* @see {@link https://valkey.io/commands/unwatch/|valkey.io} and {@link https://valkey.io/topics/transactions/#cas|Valkey Glide Wiki} for more details.
*
* @param route - (Optional) The command will be routed to all primary nodes, unless `route` is provided,
* in which case the client will route the command to the nodes defined by `route`.
* @param options - (Optional) See {@link RouteOption}.
* @returns A simple "OK" response.
*
* @example
Expand All @@ -1373,9 +1383,9 @@ export class GlideClusterClient extends BaseClient {
* console.log(response); // Output: "OK"
* ```
*/
public async unwatch(route?: Routes): Promise<"OK"> {
public async unwatch(options?: RouteOption): Promise<"OK"> {
return this.createWritePromise(createUnWatch(), {
route: toProtobufRoute(route),
route: toProtobufRoute(options?.route),
decoder: Decoder.String,
});
}
Expand Down
32 changes: 21 additions & 11 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,14 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createSet(key, value, options));
}

/** Ping the Redis server.
/**
* Pings the server.
*
* @see {@link https://valkey.io/commands/ping/|valkey.io} for details.
*
* @param message - An optional message to include in the PING command.
* If not provided, the server will respond with "PONG".
* If provided, the server will respond with a copy of the message.
* @param message - (Optional) A message to include in the PING command.
* - If not provided, the server will respond with "PONG".
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
* - If provided, the server will respond with a copy of the message.
*
* Command Response - "PONG" if `message` is not provided, otherwise return a copy of `message`.
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
*/
Expand Down Expand Up @@ -465,10 +467,12 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createRestore(key, ttl, value, options));
}

/** Get the name of the connection on which the transaction is being executed.
/**
* Gets the name of the connection on which the transaction is being executed.
*
* @see {@link https://valkey.io/commands/client-getname/|valkey.io} for details.
*
* Command Response - the name of the client connection as a string if a name is set, or null if no name is assigned.
* Command Response - The name of the client connection as a string if a name is set, or null if no name is assigned.
*/
public clientGetName(): T {
return this.addAndReturn(createClientGetName());
Expand Down Expand Up @@ -566,10 +570,12 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createIncrByFloat(key, amount));
}

/** Returns the current connection id.
/**
* Returns the current connection ID.
*
* @see {@link https://valkey.io/commands/client-id/|valkey.io} for details.
*
* Command Response - the id of the client.
* Command Response - The ID of the connection.
*/
public clientId(): T {
return this.addAndReturn(createClientId());
Expand Down Expand Up @@ -2225,14 +2231,16 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createBZPopMax(keys, timeout));
}

/** Echoes the provided `message` back.
/**
* Echoes the provided `message` back
*
* @see {@link https://valkey.io/commands/echo/|valkey.io} for more details.
*
* @param message - The message to be echoed back.
*
* Command Response - The provided `message`.
*/
public echo(message: string): T {
public echo(message: GlideString): T {
return this.addAndReturn(createEcho(message));
}

Expand Down Expand Up @@ -3856,7 +3864,9 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
export class Transaction extends BaseTransaction<Transaction> {
/// TODO: add MOVE, SLAVEOF and all SENTINEL commands

/** Change the currently selected Redis database.
/**
* Change the currently selected database.
*
* @see {@link https://valkey.io/commands/select/|valkey.io} for details.
*
* @param index - The index of the database to select.
Expand Down
6 changes: 4 additions & 2 deletions node/tests/GlideClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ describe("GlideClusterClient", () => {
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const message = uuidv4();
const echoDict = await client.echo(message, "allNodes");
const echoDict = await client.echo(message, { route: "allNodes" });

expect(typeof echoDict).toBe("object");
expect(intoArray(echoDict)).toEqual(
Expand Down Expand Up @@ -1735,7 +1735,9 @@ describe("GlideClusterClient", () => {
expect(await client.watch([key1, key2])).toEqual("OK");
expect(await client.set(key2, "hello")).toEqual("OK");
expect(await client.unwatch()).toEqual("OK");
expect(await client.unwatch("allPrimaries")).toEqual("OK");
expect(await client.unwatch({ route: "allPrimaries" })).toEqual(
"OK",
);
setFoobarTransaction.set(key1, "foobar").set(key2, "foobar");
const results = await client.exec(setFoobarTransaction);
expect(results).toEqual(["OK", "OK"]);
Expand Down
Loading
Loading