From ead1054a402ee97055fe5c8dd16ed3f50ac23ae4 Mon Sep 17 00:00:00 2001 From: Nikolay Karadzhov Date: Wed, 23 Apr 2025 14:17:12 +0300 Subject: [PATCH 1/2] docs: update programmability.md examples + add Programmability section to README and --- README.md | 4 ++++ docs/programmability.md | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3c8830f5c9c..94fec8d6483 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,10 @@ await Promise.all([ ]); ``` +### Programmability + +See the [Programmability overview](./docs/programmability.md). + ### Clustering Check out the [Clustering Guide](./docs/clustering.md) when using Node Redis to connect to a Redis Cluster. diff --git a/docs/programmability.md b/docs/programmability.md index f6ae42033c6..ce77f390d19 100644 --- a/docs/programmability.md +++ b/docs/programmability.md @@ -33,8 +33,9 @@ const client = createClient({ add: { NUMBER_OF_KEYS: 1, FIRST_KEY_INDEX: 1, - transformArguments(key: string, toAdd: number): Array { - return [key, toAdd.toString()]; + parseCommand(parser: CommandParser, key: RedisArgument, toAdd: RedisArgument) { + parser.pushKey(key); + parser.push(toAdd); }, transformReply: undefined as unknown as () => NumberReply } @@ -61,8 +62,9 @@ const client = createClient({ SCRIPT: 'return redis.call("GET", KEYS[1]) + ARGV[1];', NUMBER_OF_KEYS: 1, FIRST_KEY_INDEX: 1, - transformArguments(key: string, toAdd: number): Array { - return [key, toAdd.toString()]; + parseCommand(parser: CommandParser, key: RedisArgument, toAdd: RedisArgument) { + parser.pushKey(key); + parser.push(toAdd); }, transformReply: undefined as unknown as () => NumberReply }) From c4d14aaf03006a9b319de571bb810dde54cd495a Mon Sep 17 00:00:00 2001 From: Nikolay Karadzhov Date: Wed, 30 Apr 2025 13:29:19 +0300 Subject: [PATCH 2/2] fix imports according to the new v5 exports --- docs/programmability.md | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/docs/programmability.md b/docs/programmability.md index ce77f390d19..56eb048ca0c 100644 --- a/docs/programmability.md +++ b/docs/programmability.md @@ -25,17 +25,22 @@ FUNCTION LOAD "#!lua name=library\nredis.register_function{function_name='add', Load the prior redis function on the _redis server_ before running the example below. ```typescript -import { createClient } from 'redis'; +import { CommandParser } from '@redis/client/lib/client/parser'; +import { NumberReply } from '@redis/client/lib/RESP/types'; +import { createClient, RedisArgument } from 'redis'; const client = createClient({ functions: { library: { add: { NUMBER_OF_KEYS: 1, - FIRST_KEY_INDEX: 1, - parseCommand(parser: CommandParser, key: RedisArgument, toAdd: RedisArgument) { - parser.pushKey(key); - parser.push(toAdd); + parseCommand( + parser: CommandParser, + key: RedisArgument, + toAdd: RedisArgument + ) { + parser.pushKey(key) + parser.push(toAdd) }, transformReply: undefined as unknown as () => NumberReply } @@ -44,9 +49,8 @@ const client = createClient({ }); await client.connect(); - await client.set('key', '1'); -await client.library.add('key', 2); // 3 +await client.library.add('key', '2'); // 3 ``` ## [Lua Scripts](https://redis.io/docs/manual/programmability/eval-intro/) @@ -54,7 +58,9 @@ await client.library.add('key', 2); // 3 The following is an end-to-end example of the prior concept. ```typescript -import { createClient, defineScript, NumberReply } from 'redis'; +import { CommandParser } from '@redis/client/lib/client/parser'; +import { NumberReply } from '@redis/client/lib/RESP/types'; +import { createClient, defineScript, RedisArgument } from 'redis'; const client = createClient({ scripts: { @@ -62,9 +68,13 @@ const client = createClient({ SCRIPT: 'return redis.call("GET", KEYS[1]) + ARGV[1];', NUMBER_OF_KEYS: 1, FIRST_KEY_INDEX: 1, - parseCommand(parser: CommandParser, key: RedisArgument, toAdd: RedisArgument) { - parser.pushKey(key); - parser.push(toAdd); + parseCommand( + parser: CommandParser, + key: RedisArgument, + toAdd: RedisArgument + ) { + parser.pushKey(key) + parser.push(toAdd) }, transformReply: undefined as unknown as () => NumberReply }) @@ -72,7 +82,6 @@ const client = createClient({ }); await client.connect(); - await client.set('key', '1'); -await client.add('key', 2); // 3 +await client.add('key', '2'); // 3 ```