Skip to content

Commit 53e3575

Browse files
authored
Merge pull request #1 from nkaradzhov/update-docs
Update programmability examples
2 parents 79eb8fb + c4d14aa commit 53e3575

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ await Promise.all([
250250
]);
251251
```
252252

253+
### Programmability
254+
255+
See the [Programmability overview](./docs/programmability.md).
256+
253257
### Clustering
254258

255259
Check out the [Clustering Guide](./docs/clustering.md) when using Node Redis to connect to a Redis Cluster.

docs/programmability.md

+22-11
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@ FUNCTION LOAD "#!lua name=library\nredis.register_function{function_name='add',
2525
Load the prior redis function on the _redis server_ before running the example below.
2626

2727
```typescript
28-
import { createClient } from 'redis';
28+
import { CommandParser } from '@redis/client/lib/client/parser';
29+
import { NumberReply } from '@redis/client/lib/RESP/types';
30+
import { createClient, RedisArgument } from 'redis';
2931

3032
const client = createClient({
3133
functions: {
3234
library: {
3335
add: {
3436
NUMBER_OF_KEYS: 1,
35-
FIRST_KEY_INDEX: 1,
36-
transformArguments(key: string, toAdd: number): Array<string> {
37-
return [key, toAdd.toString()];
37+
parseCommand(
38+
parser: CommandParser,
39+
key: RedisArgument,
40+
toAdd: RedisArgument
41+
) {
42+
parser.pushKey(key)
43+
parser.push(toAdd)
3844
},
3945
transformReply: undefined as unknown as () => NumberReply
4046
}
@@ -43,34 +49,39 @@ const client = createClient({
4349
});
4450

4551
await client.connect();
46-
4752
await client.set('key', '1');
48-
await client.library.add('key', 2); // 3
53+
await client.library.add('key', '2'); // 3
4954
```
5055

5156
## [Lua Scripts](https://redis.io/docs/manual/programmability/eval-intro/)
5257

5358
The following is an end-to-end example of the prior concept.
5459

5560
```typescript
56-
import { createClient, defineScript, NumberReply } from 'redis';
61+
import { CommandParser } from '@redis/client/lib/client/parser';
62+
import { NumberReply } from '@redis/client/lib/RESP/types';
63+
import { createClient, defineScript, RedisArgument } from 'redis';
5764

5865
const client = createClient({
5966
scripts: {
6067
add: defineScript({
6168
SCRIPT: 'return redis.call("GET", KEYS[1]) + ARGV[1];',
6269
NUMBER_OF_KEYS: 1,
6370
FIRST_KEY_INDEX: 1,
64-
transformArguments(key: string, toAdd: number): Array<string> {
65-
return [key, toAdd.toString()];
71+
parseCommand(
72+
parser: CommandParser,
73+
key: RedisArgument,
74+
toAdd: RedisArgument
75+
) {
76+
parser.pushKey(key)
77+
parser.push(toAdd)
6678
},
6779
transformReply: undefined as unknown as () => NumberReply
6880
})
6981
}
7082
});
7183

7284
await client.connect();
73-
7485
await client.set('key', '1');
75-
await client.add('key', 2); // 3
86+
await client.add('key', '2'); // 3
7687
```

0 commit comments

Comments
 (0)