Redis with node-redis (Node.js)
Uses the official redis package 6.3. The code is an ordinary application for the original database; nothing in it knows about MachNoSQL.
The only change
Section titled “The only change”redis://:secret@redis.example.com:6379/0redis://:secret@machnosql.example.com:6379/0The address comes from the environment variable REDIS_URL, so switching to MachNoSQL changes configuration only.
export REDIS_URL='redis://:secret@machnosql.example.com:6379/0'npm installnpm startSource
Section titled “Source”The program as it is kept and tested in the Mach Database NoSQL repository; highlighted lines read the connection setting.
// Cache, counter, session and leaderboard on Redis, unchanged on MachNoSQL.import { createClient } from "redis";
const client = await createClient({ url: process.env.REDIS_URL }).connect();
await client.set("cache:home", "<html>...</html>", { expiration: { type: "EX", value: 60 } });console.log("cached:", await client.get("cache:home"), "ttl:", await client.ttl("cache:home"));
console.log("views:", await client.incr("views:home"));
await client.hSet("session:42", { user: "ada", cart_items: "3" });console.log("session:", await client.hGetAll("session:42"));
await client.zAdd("leaderboard", [ { score: 120, value: "ada" }, { score: 95, value: "linus" }, { score: 130, value: "grace" },]);console.log("top:", await client.zRangeWithScores("leaderboard", 0, 2, { REV: true }));
const results = await client .multi() .incr("orders:count") .lPush("orders:recent", "order-1001") .lTrim("orders:recent", 0, 99) .exec();console.log("pipeline:", results);
await client.close();{ "name": "machnosql-example-node-redis", "private": true, "type": "module", "scripts": { "start": "node app.js" }, "dependencies": { "redis": "^6.3.1" }}