|
4 | 4 | [](https://codecov.io/gh/redis/node-redis)
|
5 | 5 | [](https://github.com/redis/node-redis/blob/master/LICENSE)
|
6 | 6 | [](https://lgtm.com/projects/g/redis/node-redis/alerts)
|
7 |
| -[](https://discord.gg/redis) |
8 | 7 |
|
9 |
| -node-redis is a modern, high performance [Redis](https://redis.io) client for Node.js with built-in support for Redis 6.2 commands and modules including [RediSearch](https://redisearch.io) and [RedisJSON](https://redisjson.io). |
| 8 | +[](https://discord.gg/redis) |
| 9 | +[](https://www.twitch.tv/redisinc) |
| 10 | +[](https://www.youtube.com/redisinc) |
| 11 | +[](https://twitter.com/redisinc) |
| 12 | + |
| 13 | +node-redis is a modern, high performance [Redis](https://redis.io) client for Node.js. |
10 | 14 |
|
11 | 15 |
|
12 | 16 | ## Packages
|
13 | 17 |
|
14 |
| -| Name | Description | |
15 |
| -|---------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
16 |
| -| [redis](./) | [](https://www.npmjs.com/package/redis) [](https://www.npmjs.com/package/redis) | |
17 |
| -| [@node-redis/client](./packages/client) | [](https://www.npmjs.com/package/@node-redis/client) [](https://www.npmjs.com/package/@node-redis/client) [](https://redis.js.org/documentation/client/) | |
18 |
| -| [@node-redis/bloom](./packages/bloom) | [](https://www.npmjs.com/package/@node-redis/bloom) [](https://www.npmjs.com/package/@node-redis/bloom) [](https://redis.js.org/documentation/bloom/) [Redis Bloom](https://oss.redis.com/redisbloom/) commands | |
19 |
| -| [@node-redis/graph](./packages/graph) | [](https://www.npmjs.com/package/@node-redis/graph) [](https://www.npmjs.com/package/@node-redis/graph) [](https://redis.js.org/documentation/graph/) [Redis Graph](https://oss.redis.com/redisgraph/) commands | |
20 |
| -| [@node-redis/json](./packages/json) | [](https://www.npmjs.com/package/@node-redis/json) [](https://www.npmjs.com/package/@node-redis/json) [](https://redis.js.org/documentation/json/) [Redis JSON](https://oss.redis.com/redisjson/) commands | |
21 |
| -| [@node-redis/search](./packages/search) | [](https://www.npmjs.com/package/@node-redis/search) [](https://www.npmjs.com/package/@node-redis/search) [](https://redis.js.org/documentation/search/) [Redis Search](https://oss.redis.com/redisearch/) commands | |
22 |
| -| [@node-redis/time-series](./packages/time-series) | [](https://www.npmjs.com/package/@node-redis/time-series) [](https://www.npmjs.com/package/@node-redis/time-series) [](https://redis.js.org/documentation/time-series/) [Redis Time-Series](https://oss.redis.com/redistimeseries/) commands | |
| 18 | +| Name | Description | |
| 19 | +|----------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 20 | +| [redis](./) | [](https://www.npmjs.com/package/redis) [](https://www.npmjs.com/package/redis) | |
| 21 | +| [@redis/client](./packages/client) | [](https://www.npmjs.com/package/@redis/client) [](https://www.npmjs.com/package/@redis/client) [](https://redis.js.org/documentation/client/) | |
| 22 | +| [@redis/bloom](./packages/bloom) | [](https://www.npmjs.com/package/@redis/bloom) [](https://www.npmjs.com/package/@redis/bloom) [](https://redis.js.org/documentation/bloom/) [Redis Bloom](https://oss.redis.com/redisbloom/) commands | |
| 23 | +| [@redis/graph](./packages/graph) | [](https://www.npmjs.com/package/@redis/graph) [](https://www.npmjs.com/package/@redis/graph) [](https://redis.js.org/documentation/graph/) [Redis Graph](https://oss.redis.com/redisgraph/) commands | |
| 24 | +| [@redis/json](./packages/json) | [](https://www.npmjs.com/package/@redis/json) [](https://www.npmjs.com/package/@redis/json) [](https://redis.js.org/documentation/json/) [Redis JSON](https://oss.redis.com/redisjson/) commands | |
| 25 | +| [@redis/search](./packages/search) | [](https://www.npmjs.com/package/@redis/search) [](https://www.npmjs.com/package/@redis/search) [](https://redis.js.org/documentation/search/) [Redis Search](https://oss.redis.com/redisearch/) commands | |
| 26 | +| [@redis/time-series](./packages/time-series) | [](https://www.npmjs.com/package/@redis/time-series) [](https://www.npmjs.com/package/@redis/time-series) [](https://redis.js.org/documentation/time-series/) [Redis Time-Series](https://oss.redis.com/redistimeseries/) commands | |
| 27 | + |
| 28 | +> :warning: In version 4.1.0 we moved our subpackages from `@node-redis` to `@redis`. If you're just using `npm install redis`, you don't need to do anything—it'll upgrade automatically. If you're using the subpackages directly, you'll need to point to the new scope (e.g. `@redis/client` instead of `@node-redis/client`). |
23 | 29 |
|
24 | 30 | ## Installation
|
25 | 31 |
|
@@ -219,36 +225,84 @@ client.scanIterator({
|
219 | 225 | });
|
220 | 226 | ```
|
221 | 227 |
|
222 |
| -### Lua Scripts |
| 228 | +### [Programmability](https://redis.io/docs/manual/programmability/) |
| 229 | + |
| 230 | +Redis provides a programming interface allowing code execution on the redis server. |
| 231 | + |
| 232 | +#### [Functions](https://redis.io/docs/manual/programmability/functions-intro/) |
| 233 | + |
| 234 | +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. |
| 235 | + |
| 236 | +```lua |
| 237 | +#!lua name=library |
| 238 | + |
| 239 | +redis.register_function { |
| 240 | + function_name = 'add', |
| 241 | + callback = function(keys, args) return redis.call('GET', keys[1]) + args[1] end, |
| 242 | + flags = { 'no-writes' } |
| 243 | +} |
| 244 | +``` |
| 245 | + |
| 246 | +Here is the same example, but in a format that can be pasted into the `redis-cli`. |
223 | 247 |
|
224 |
| -Define new functions using [Lua scripts](https://redis.io/commands/eval) which execute on the Redis server: |
| 248 | +``` |
| 249 | +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\"}}" |
| 250 | +``` |
| 251 | + |
| 252 | +Load the prior redis function on the _redis server_ before running the example below. |
225 | 253 |
|
226 | 254 | ```typescript
|
227 |
| -import { createClient, defineScript } from 'redis'; |
| 255 | +import { createClient } from 'redis'; |
228 | 256 |
|
229 |
| -(async () => { |
230 |
| - const client = createClient({ |
231 |
| - scripts: { |
232 |
| - add: defineScript({ |
| 257 | +const client = createClient({ |
| 258 | + functions: { |
| 259 | + library: { |
| 260 | + add: { |
233 | 261 | NUMBER_OF_KEYS: 1,
|
234 |
| - SCRIPT: |
235 |
| - 'local val = redis.pcall("GET", KEYS[1]);' + |
236 |
| - 'return val + ARGV[1];', |
237 | 262 | transformArguments(key: string, toAdd: number): Array<string> {
|
238 | 263 | return [key, toAdd.toString()];
|
239 | 264 | },
|
240 | 265 | transformReply(reply: number): number {
|
241 | 266 | return reply;
|
242 | 267 | }
|
243 |
| - }) |
| 268 | + } |
244 | 269 | }
|
245 |
| - }); |
| 270 | + } |
| 271 | +}); |
| 272 | + |
| 273 | +await client.connect(); |
| 274 | + |
| 275 | +await client.set('key', '1'); |
| 276 | +await client.library.add('key', 2); // 3 |
| 277 | +``` |
| 278 | + |
| 279 | +#### [Lua Scripts](https://redis.io/docs/manual/programmability/eval-intro/) |
246 | 280 |
|
247 |
| - await client.connect(); |
| 281 | +The following is an end-to-end example of the prior concept. |
| 282 | + |
| 283 | +```typescript |
| 284 | +import { createClient, defineScript } from 'redis'; |
| 285 | + |
| 286 | +const client = createClient({ |
| 287 | + scripts: { |
| 288 | + add: defineScript({ |
| 289 | + NUMBER_OF_KEYS: 1, |
| 290 | + SCRIPT: |
| 291 | + 'return redis.call("GET", KEYS[1]) + ARGV[1];', |
| 292 | + transformArguments(key: string, toAdd: number): Array<string> { |
| 293 | + return [key, toAdd.toString()]; |
| 294 | + }, |
| 295 | + transformReply(reply: number): number { |
| 296 | + return reply; |
| 297 | + } |
| 298 | + }) |
| 299 | + } |
| 300 | +}); |
| 301 | + |
| 302 | +await client.connect(); |
248 | 303 |
|
249 |
| - await client.set('key', '1'); |
250 |
| - await client.add('key', 2); // 3 |
251 |
| -})(); |
| 304 | +await client.set('key', '1'); |
| 305 | +await client.add('key', 2); // 3 |
252 | 306 | ```
|
253 | 307 |
|
254 | 308 | ### Disconnecting
|
@@ -323,9 +377,10 @@ Node Redis is supported with the following versions of Redis:
|
323 | 377 |
|
324 | 378 | | Version | Supported |
|
325 | 379 | |---------|--------------------|
|
| 380 | +| 7.0.z | :heavy_check_mark: | |
326 | 381 | | 6.2.z | :heavy_check_mark: |
|
327 | 382 | | 6.0.z | :heavy_check_mark: |
|
328 |
| -| 5.y.z | :heavy_check_mark: | |
| 383 | +| 5.0.z | :heavy_check_mark: | |
329 | 384 | | < 5.0 | :x: |
|
330 | 385 |
|
331 | 386 | > Node Redis should work with older versions of Redis, but it is not fully tested and we cannot offer support.
|
|
0 commit comments