Skip to content

Commit 66eaa3e

Browse files
authored
Create README.md
1 parent ef92ec3 commit 66eaa3e

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# ModJS
2+
A Javascript Module for KeyDB and Redis.
3+
4+
ModJS allows you to extend Redis and KeyDB with new functionality implemented in JavaScript. ModJS uses the V8 JIT engine so complex scripts can execute significantly faster than their Lua equivalents. In addition ModJS supports many node.js modules offering extensive library support for common tasks.
5+
6+
## Quick Start Guide
7+
There are two ways to use ModJS, the first is similar to Lua with the EVALJS Command:
8+
9+
> EVALJS "redis.call('get', 'testkey')"
10+
11+
While EVALJS is quick and easy, a much more powerful method exists in the form of startup scripts.
12+
In a startup script you can define your own custom commands and call them from any client as though they were built-in. In addition,
13+
these commands can skip the parsing step of EVALJS and so will execute much faster.
14+
15+
### Adding a Command
16+
17+
All commands must be defined at the time the module is loaded in a startup script. A startup script is simply a javascript file who's path is passed to ModJS at load time.
18+
19+
Below we create an example script named startup.js which defines a new command named "concat". This command fetches two keys and returns the string concatenation of the two values.
20+
We then register this function with the server so that it will be directly callable from Redis or KeyDB clients.
21+
22+
function concat(key1, key2) {
23+
var str1 = redis.call('get', key1);
24+
var str2 = redis.call('get', key2);
25+
return str1 + str2;
26+
}
27+
28+
keydb.register(concat);
29+
30+
To run this script on startup simply add the path as a module parameter, e.g. ``loadmodule /path/to/modjs.so /path/to/startup.js``
31+
32+
We may now use this command from any client. E.g.:
33+
34+
127.0.0.1:6379> set keyA foo
35+
OK
36+
127.0.0.1:6379> set keyB bar
37+
OK
38+
127.0.0.1:6379> concat keyA keyB
39+
"foobar"
40+
41+
### Importing scripts from npm
42+
43+
The above examples were simple enough not to require external libraries, however for more complex tasks it may be desireable to import modules fetched via npm. ModJS implements the require() api with similar semantics to node.js.
44+
45+
In this example we will use the popular lodash library, installed with: ``npm install loadash``. Below we've updated our example script to use the camelCase() function in lodash:
46+
47+
var _ = require("lodash")
48+
49+
function concat(key1, key2) {
50+
var str1 = redis.call('get', key1);
51+
var str2 = redis.call('get', key2);
52+
return _.camelCase(str1 + " " + str2)
53+
}
54+
55+
keydb.register(concat);
56+
57+
The lodash module is imported with require() as it would be in a node.js script. Note that require() will search for modules starting from the working directory of Redis or KeyDB.
58+
59+
Once loaded this new script will concatenate the two strings using camel case.
60+
61+
62+
# Compiling ModJS
63+
64+
ModJS requires you to first build V8, as a result we recommend using a pre-compiled docker image. However if you wish to compile ModJS first follow the instructions to download and build V8 here: https://v8.dev/docs/build
65+
66+
Once you have built the core V8 library we must then build the monolith with the following command:
67+
68+
/path/to/v8/$ ninja -C out.gn/x64.release.sample v8_monolith
69+
70+
If V8 compiled successfully you are now ready to build ModJS. ModJS can be built with one line:
71+
72+
make V8_PATH=/path/to/v8
73+
74+

0 commit comments

Comments
 (0)