{"library":"node-redis-script","title":"Redis Script Utility for Node.js","description":"node-redis-script is a JavaScript library designed to simplify the execution of Lua scripts within Redis from Node.js applications. It abstracts the complexities of `EVAL` and `EVALSHA` commands, allowing developers to define scripts as strings and then execute them via a generated function. The current stable version is 2.0.1. It primarily focuses on providing a clean, function-oriented API for script management rather than requiring direct interaction with Redis `EVAL` commands. This library automatically handles script loading and caching using `EVALSHA` for efficiency, reducing network overhead after the initial script load. It offers flexibility by supporting both `node-redis` (v0.10+) and `ioredis` clients, allowing integration with existing Redis client setups.","language":"javascript","status":"maintenance","last_verified":"Tue Apr 21","install":{"commands":["npm install node-redis-script"],"cli":null},"imports":["import { createScript } from 'node-redis-script';","const { createScript } = require('node-redis-script');","import * as RedisScript from 'node-redis-script';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { createClient } from 'redis'; // or from 'ioredis'\nimport { createScript } from 'node-redis-script';\n\nasync function runRedisScript() {\n  const redisClient = createClient();\n  redisClient.on('error', (err) => console.log('Redis Client Error', err));\n  await redisClient.connect();\n\n  const incrbyExSrc = `\n    local current\n    current = redis.call('incrby',KEYS[1],ARGV[1])\n    redis.call('expire',KEYS[1],ARGV[2]);\n    return current\n  `;\n\n  // give it a redis client and script source\n  const opts = { redis: redisClient }; // For ioredis: { ioredis: ioredisClient }\n  const incrbyEx = createScript(opts, incrbyExSrc);\n\n  // redis requires you to tell it how many keys to expect\n  const numKeys = 1;\n  const key = 'myCounterKey';\n  const incr = 5;\n  const ex = 60; // expire in 60 seconds\n\n  const result = await incrbyEx(numKeys, key, incr, ex);\n  console.log(`Current value of ${key}: ${result}`); // Should print 5 the first time, 10 the second, etc.\n\n  await redisClient.disconnect();\n}\n\nrunRedisScript().catch(console.error);","lang":"javascript","description":"This example demonstrates how to initialize a Redis client, define a Lua script, create a script function using `createScript`, and then execute it with specific keys and arguments. It showcases a common pattern for an atomic increment-and-expire operation.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}