Skip to content

Commit

Permalink
Node: Update examples in docs. (valkey-io#1360)
Browse files Browse the repository at this point in the history
Update examples in docs. (#248)

Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand authored and cyip10 committed Jun 24, 2024
1 parent 3f4b941 commit d2a6de7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
38 changes: 23 additions & 15 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,16 @@ export type BaseClientConfiguration = {
* DNS Addresses and ports of known nodes in the cluster.
* If the server is in cluster mode the list can be partial, as the client will attempt to map out the cluster and find all nodes.
* If the server is in standalone mode, only nodes whose addresses were provided will be used by the client.
*
* @example
* <code>
* ```typescript
* configuration.addresses =
* [
* \{ address:sample-address-0001.use1.cache.amazonaws.com, port:6378 \},
* \{ address: sample-address-0002.use2.cache.amazonaws.com \}
* \{ address: sample-address-0003.use2.cache.amazonaws.com, port:6380 \}
* { address: sample-address-0001.use1.cache.amazonaws.com, port:6378 },
* { address: sample-address-0002.use2.cache.amazonaws.com }
* { address: sample-address-0003.use2.cache.amazonaws.com, port:6380 }
* ]
* </code>
* ```
*/
addresses: {
host: string;
Expand Down Expand Up @@ -1276,9 +1278,11 @@ export class BaseClient {
* If `key` does not exist, empty list will be returned.
*
* @example
* ```typescript
* // Example usage of spopCount method to remove and return multiple random members from a set
* const result = await client.spopCount("my_set", 2);
* console.log(result); // Output: ['member2', 'member3'] - Removes and returns 2 random members from the set "my_set".
* ```
*
* @example
* ```typescript
Expand Down Expand Up @@ -1488,16 +1492,18 @@ export class BaseClient {
*
* @param script - The Lua script to execute.
* @param options - The script option that contains keys and arguments for the script.
* @returns a value that depends on the script that was executed.
* @returns A value that depends on the script that was executed.
*
* @example
* const luaScript = new Script("return \{ KEYS[1], ARGV[1] \}");
* const scriptOptions = \{
* keys: ["foo"],
* args: ["bar"],
* \};
* await invokeScript(luaScript, scriptOptions);
* ["foo", "bar"]
* ```typescript
* const luaScript = new Script("return { KEYS[1], ARGV[1] }");
* const scriptOptions = {
* keys: ["foo"],
* args: ["bar"],
* };
* const result = await invokeScript(luaScript, scriptOptions);
* console.log(result); // Output: ['foo', 'bar']
* ```
*/
public invokeScript(
script: Script,
Expand Down Expand Up @@ -1727,8 +1733,9 @@ export class BaseClient {
* // Example usage of zrange method to retrieve all members of a sorted set in ascending order
* const result = await client.zrange("my_sorted_set", { start: 0, stop: -1 });
* console.log(result1); // Output: ['member1', 'member2', 'member3'] - Returns all members in ascending order.
*
* ```
* @example
* ```typescript
* // Example usage of zrange method to retrieve members within a score range in ascending order
* const result = await client.zrange("my_sorted_set", {
* start: "negativeInfinity",
Expand Down Expand Up @@ -1768,8 +1775,9 @@ export class BaseClient {
* type: "byScore",
* });
* console.log(result); // Output: {'member1': 10.5, 'member2': 15.2} - Returns members with scores between 10 and 20 with their scores.
*
* ```
* @example
* ```typescript
* // Example usage of zrangeWithScores method to retrieve members within a score range with their scores
* const result = await client.zrangeWithScores("my_sorted_set", {
* start: "negativeInfinity",
Expand Down
36 changes: 16 additions & 20 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ import { redis_request } from "./ProtobufMessage";
* Specific response types are documented alongside each method.
*
* @example
* transaction = new BaseTransaction()
* .set("key", "value")
* .get("key");
* await client.exec(transaction);
* [OK , "value"]
* ```typescript
* const transaction = new BaseTransaction()
* .set("key", "value")
* .get("key");
* const result = await client.exec(transaction);
* console.log(result); // Output: ['OK', 'value']
* ```
*/
export class BaseTransaction<T extends BaseTransaction<T>> {
/**
Expand Down Expand Up @@ -322,10 +324,6 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* @param parameters - A List of keyValuePairs consisting of configuration parameters and their respective values to set.
*
* Command Response - "OK" when the configuration was set properly. Otherwise, the transaction fails with an error.
*
* @example
* config_set([("timeout", "1000")], [("maxmemory", "1GB")]) - Returns OK
*
*/
public configSet(parameters: Record<string, string>): T {
return this.addAndReturn(createConfigSet(parameters));
Expand Down Expand Up @@ -1149,11 +1147,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* @remarks - This function should only be used for single-response commands. Commands that don't return response (such as SUBSCRIBE), or that return potentially more than a single response (such as XREAD), or that change the client's behavior (such as entering pub/sub mode on RESP2 connections) shouldn't be called using this function.
*
* @example
* Returns a list of all pub/sub clients:
* ```ts
* connection.customCommand(["CLIENT", "LIST","TYPE", "PUBSUB"])
* ```
* Command Response - A response from Redis with an `Object`.
*/
public customCommand(args: string[]): T {
return this.addAndReturn(createCustomCommand(args));
Expand Down Expand Up @@ -1287,12 +1281,14 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* Specific response types are documented alongside each method.
*
* @example
* transaction = new Transaction()
* .set("key", "value")
* .select(1) /// Standalone command
* .get("key");
* await RedisClient.exec(transaction);
* [OK , OK , null]
* ```typescript
* const transaction = new Transaction()
* .set("key", "value")
* .select(1) /// Standalone command
* .get("key");
* const result = await redisClient.exec(transaction);
* console.log(result); // Output: ['OK', 'OK', null]
* ```
*/
export class Transaction extends BaseTransaction<Transaction> {
/// TODO: add MOVE, SLAVEOF and all SENTINEL commands
Expand Down

0 comments on commit d2a6de7

Please sign in to comment.