Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 22 additions & 11 deletions docs/programmability.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -43,34 +49,39 @@ 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/)

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: {
add: defineScript({
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
})
}
});

await client.connect();

await client.set('key', '1');
await client.add('key', 2); // 3
await client.add('key', '2'); // 3
```