KV Raw Connection
The KV Raw Connection block gives your JavaScript direct access to the key-value store's own client. Use it for anything the KV Operations block doesn't cover — counters, setting expiry on a key that already exists, hashes, lists, sets, or several commands in one go.
Inputs
- Connection: The KV store integration to use — see KV Stores.
- JS: The JavaScript code to execute. Your code has access to a
kvobject, the client for the selected connection.
Logic
- The block runs your JS code with
kvavailable. - You call whatever commands you need on
kv. - Whatever your code returns becomes the block output.
kv exists only while your code runs, so you can't reach it from other blocks.
Redis
The client exposes the standard Redis command set as methods, and each one returns a promise — so await them.
// count an API hit and return the new total
const hits = await kv.incr("hits:" + getRouteParam("id"));
return hits; // 42// put an expiry on a key that already exists
await kv.expire("session:" + getHeader("x-session"), 900);
return true;// read several keys in one round trip
const [name, email] = await kv.mget("user:7:name", "user:7:email");
return { name, email };TIP
Values come back as text. Use JSON.parse() if you stored an object, and remember a missing key reads as null.
Memcached
The Memcached client works differently: its methods take a callback instead of returning a promise. Wrap the call so you can await it.
// add an expiry to an existing key
const ok = await new Promise((resolve, reject) => {
kv.touch("session:abc", 900, (err) => (err ? reject(err) : resolve(true)));
});
return ok;// fetch many keys at once
const values = await new Promise((resolve, reject) => {
kv.getMulti(["a", "b", "c"], (err, data) => (err ? reject(err) : resolve(data)));
});
return values; // { a: "1", b: "2", c: "3" }Different stores, different commands
Redis and Memcached do not offer the same commands. Memcached has no hashes, lists, or sets, and no way to read a key's remaining time to live. Code written against one store will not run unchanged against the other, so if you swap the connection, re-check the commands you used.
Notes
- Always
awaityour commands. Without it you return a pending promise instead of a value. - If a command fails, the block fails, and the original error is kept as the cause so you can see what the store reported.
- Stick to KV Operations for plain get, set, and delete — it works the same on both stores and needs no code.
- Tick Save output to variable to keep what you returned under
outputs.<name>for later blocks.
