Skip to content

Repository files navigation

redis-lua2js

Build Statusnpm packageCoverage Status

convert redis lua scripts to a useful node module

Install

$ npm install --save-dev redis-lua2js

Usage

This module is not meant to be used on its own, but rather as part of another module, such as gulp-redis-lua2js or hook-redis-lua. In here, I will demonstrate the usage with the help of module-from-string:

index.js:

import{readFileSync}from'node:fs';import{join}from'node:path';importRedisfrom'ioredis';importlua2jsfrom'redis-lua2js';import{requireFromString}from'module-from-string';constioredis=newRedis();constlua=readFileSync(join(__dirname,'pdel.lua'));constjs=lua2js(lua);// This is a node module string, the template of which you can see in src/lua.jsconstpdel=requireFromString(js);// Parse the module as a stringioredis.defineCommand(pdel.name,{lua: pdel.lua,numberOfKeys: pdel.numberOfKeys,});ioredis.pdel('*');

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

API

lua2js(lua, { name, numberOfKeys, type = "commonjs" })

Takes the contents of a lua script and outputs a node module, as a string, which can be used to load the script into a redis client such as ioredis easily.

lua

Type: string, the contents of a lua script

name

Type: string?

By default it is parsed from the comments of the lua script, as demonstrated in the Usage section

The name of the redis command, will be used when installing to ioredis

numberOfKeys

Type: string?,

By default it is parsed from the comments of the lua script, as demonstrated in the Usage section

The number of keys that the redis command accepts

type

Type: "commonjs" | "module"

Whether the output module is commonjs or esm.

The parsed module will export the following:

name

Type: string | null, contains the name from the API above

numberOfKeys

Type: integer | null, contains the value of the number of keys argument from the API above

lua

Type: string, contains the contents of the lua script, useful for manually installing the script, for example, with ioredis.defineCommand

License

See the LICENSE file for license rights and limitations (MIT).

About

convert redis lua scripts to a useful node module

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages