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..56eb048ca0c 100644 --- a/docs/programmability.md +++ b/docs/programmability.md @@ -25,16 +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, - 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 } @@ -43,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/) @@ -53,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: { @@ -61,8 +68,13 @@ 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 }) @@ -70,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 ```