1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00

Update README.MD (#2924)

* Update README.MD

* docs: update programmability.md examples

+ add Programmability section to README and

* fix imports according to the new v5 exports

* more v5 docs updates

---------

Co-authored-by: Nikolay Karadzhov <nkaradzhov89@gmail.com>
This commit is contained in:
Bobby I.
2025-04-30 16:28:22 +03:00
committed by GitHub
parent 9459660d96
commit 49d6b2d465
5 changed files with 341 additions and 30 deletions

View File

@@ -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<string> {
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<string> {
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
```