node-redis-script-patch
v2.0.4
Published
Easily use redis scripts in Node with latest changes
Readme
node-redis-script-patch
Easily run redis scripts from Node. Removes the promisify as the new redis client has a built-in promise return.
Fixes
(node:69510) [DEP0174] DeprecationWarning: Calling promisify on a function that returns a Promise is likely a mistake.To override subdependency of node-redis-script-patch. Add this in your package json
Replace the package-name with the name of dependency that is using the node-redis-script-patch.
For npm
"overrides": { "package-name": { "node-redis-script": "npm:node-redis-script-patch@latest" } },
For yarn
"resolutions": { "package-name/node-redis-script": "npm:node-redis-script-patch@latest" },
Requirements
- node-redis v0.10+ or ioredis
- Redis
v2.6or above - Node 21.0+ or above
Install
npm install node-redis-script-patchUsage
const redis = require('redis').createClient();
const { createScript } = require('node-redis-script-patch');
const incrbyExSrc = `
local current
current = redis.call('incrby',KEYS[1],ARGV[1])
redis.call('expire',KEYS[1],ARGV[2]);
return current
`;
// give it a redis client and script source
const opts = { redis }; // or { ioredis } for ioredis
const incrbyEx = createScript(opts, incrbyExSrc);
// you get back a function that runs your script with given args
// redis requires you to tell it how many keys to expect
const numKeys = 1;
const key = 'test';
const incr = 1;
const ex = 10;
const result = await incrbyEx(numKeys, key, incr, ex);
// Should print 1
console.log(result);Options
const opts = {
// you can use either node-redis or ioredis client
redis, // node-redis client
ioredis // ioredis client
};Test
# install docker & docker-compose for local redis setup
npm test