Skip to content

Repository files navigation

node-hook-redis-lua

Travisnpm packageCoverage StatusDependency StatusdevDependency Status

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 1localfunctiondeleteKeys (keys)
fori, nameinipairs(keys) doredis.call("DEL", name)
endendiftype(redis.replicate_commands) =='function' andredis.replicate_commands() then-- Redis 3.2+localcount=0localcursor="0"localkeysrepeatcursor, keys=unpack(redis.call("SCAN", cursor, "MATCH", KEYS[1]))
count=count+#keysdeleteKeys(keys)
untilcursor=="0"returncountelselocalkeys=redis.call("KEYS", KEYS[1])
deleteKeys(keys)
return#keysend

index.js:

constRedis=require('ioredis');constfs=require('fs');const{ hook, unhook }=require('hook-redis-lua');hook();// Uses default redis-lua2js behavior for parsing name and numberOfKeys out of lua commentsconstpdel=require('./pdel.lua');constioredis=newRedis();ioredis.defineCommand(pdel.name,{lua: pdel.lua,numberOfKeys: pdel.numberOfKeys,});ioredis.pdel('*');console.log(pdel.name);// pdelconsole.log(pdel.numberOfKeys);// 1console.log(pdel.lua);// the content of pdel.luaunhook();// 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).

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages