node-hook-redis-lua
import redis lua scripts as a normal node module
Install
$ npm install --save hook-redis-lua
Usage
If you have a redis lua script in your project, you can load it and install it into ioredis
clients, or any other client, as follows:
pdel.lua:
--!/usr/bin/env lua-- name pdel-- nkeys 1 local function deleteKeys (keys) for i, name in ipairs(keys) do redis.call("DEL", name) endend if type(redis.replicate_commands) == 'function' and redis.replicate_commands() then -- Redis 3.2+ local count = 0 local cursor = "0" local keys repeat cursor, keys = unpack(redis.call("SCAN", cursor, "MATCH", KEYS[1])) count = count + #keys deleteKeys(keys) until cursor == "0" return countelse local keys = redis.call("KEYS", KEYS[1]) deleteKeys(keys) return #keysend
index.js:
const Redis = ;const fs = ;const hook unhook = ; ; // Uses default redis-lua2js behavior for parsing name and numberOfKeys out of lua comments const pdel = ;const ioredis = ;ioredis; ioredis; console; // pdelconsole; // 1console; // the content of pdel.lua ; // can't require lua scripts from here
API
hook({ name, numberOfKeys })
Hooks .lua
files to be parsed when called by require
with redis-lua2js
name
Type: any | (filename: string, source: string) => any
, default: undefined
How to determine the name of the lua script. If undefined, it will use the default redis-lua2js
behavior of parsing lua comments for the name.
If a constant is passed, it will use this constant for the name of all scripts.
If a function is passed, it will be used to determine the name of the script. The function must be synchronous.
Example: (filename) => path.basename(filename, path.extname(filename))
numberOfKeys
Type: any | (filename: string, source: string) => any
, default: undefined
How to determine the number of keys of the lua script. If undefined, it will use the default redis-lua2js
behavior of parsing lua comments for the number of keys.
If a constant is passed, it will use this constant for the number of keys of all scripts.
If a function is passed, it will be used to determine the number of keys of the script. The function must be synchronous.
Example: (filename, source) => getNumberOfKeysSomehow(source)
unhook()
Removes .lua
hooks from require
For details on the properties exported by the hook, check redis-lua2js.
Tests
npm test
License
See the LICENSE file for license rights and limitations (MIT).