Skip to content

Commit 9c1f2a0

Browse files
committed
restore programmability docs
1 parent 91be6ac commit 9c1f2a0

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
node-redis is a modern, high performance [Redis](https://redis.io) client for Node.js.
1313

14-
1514
## Packages
1615

1716
| Name | Description |

docs/programmability.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [Programmability](https://redis.io/docs/manual/programmability/)
2+
3+
Redis provides a programming interface allowing code execution on the redis server.
4+
5+
## [Functions](https://redis.io/docs/manual/programmability/functions-intro/)
6+
7+
The following example retrieves a key in redis, returning the value of the key, incremented by an integer. For example, if your key _foo_ has the value _17_ and we run `add('foo', 25)`, it returns the answer to Life, the Universe and Everything.
8+
9+
```lua
10+
#!lua name=library
11+
12+
redis.register_function {
13+
function_name = 'add',
14+
callback = function(keys, args) return redis.call('GET', keys[1]) + args[1] end,
15+
flags = { 'no-writes' }
16+
}
17+
```
18+
19+
Here is the same example, but in a format that can be pasted into the `redis-cli`.
20+
21+
```
22+
FUNCTION LOAD "#!lua name=library\nredis.register_function{function_name=\"add\", callback=function(keys, args) return redis.call('GET', keys[1])+args[1] end, flags={\"no-writes\"}}"
23+
```
24+
25+
Load the prior redis function on the _redis server_ before running the example below.
26+
27+
```typescript
28+
import { createClient } from 'redis';
29+
30+
const client = createClient({
31+
functions: {
32+
library: {
33+
add: {
34+
NUMBER_OF_KEYS: 1,
35+
FIRST_KEY_INDEX: 1,
36+
transformArguments(key: string, toAdd: number): Array<string> {
37+
return [key, toAdd.toString()];
38+
},
39+
transformReply: undefined as unknown as () => NumberReply
40+
}
41+
}
42+
}
43+
});
44+
45+
await client.connect();
46+
47+
await client.set('key', '1');
48+
await client.library.add('key', 2); // 3
49+
```
50+
51+
## [Lua Scripts](https://redis.io/docs/manual/programmability/eval-intro/)
52+
53+
The following is an end-to-end example of the prior concept.
54+
55+
```typescript
56+
import { createClient, defineScript, NumberReply } from 'redis';
57+
58+
const client = createClient({
59+
scripts: {
60+
add: defineScript({
61+
SCRIPT: 'return redis.call("GET", KEYS[1]) + ARGV[1];',
62+
NUMBER_OF_KEYS: 1,
63+
FIRST_KEY_INDEX: 1,
64+
transformArguments(key: string, toAdd: number): Array<string> {
65+
return [key, toAdd.toString()];
66+
},
67+
transformReply: undefined as unknown as () => NumberReply
68+
})
69+
}
70+
});
71+
72+
await client.connect();
73+
74+
await client.set('key', '1');
75+
await client.add('key', 2); // 3
76+
```

0 commit comments

Comments
 (0)