Skip to main content

Redis SDK and API Commands

You can access all the familiar Redis commands using the Macrometa SDK or API.

  • Step 1. Install the SDK.
  • Step 2. Create an instance of the jsc8
  • Step 3. Access Redis commands client.redis.<Redis command>.
const jsc8 = require("jsc8");
client = new jsc8({
url: "play.paas.macrometa.io",
apiKey: "xxxxx",
fabricName: "_system",
});

// We need to create a Redis collection on platform using SDK, Console, or API call
const REDIS_COLLECTION = "testRedisCollection";

async function redisExample() {
let response;
// String data type example
// Set string
await client.redis.set("test", "1", REDIS_COLLECTION);
// Get string
response = await client.redis.get("test", REDIS_COLLECTION);
// Response from platform
console.log(response);

// Sorted set data type example
// Add sorted set
await client.redis.zadd("testZadd", [1, "test"], REDIS_COLLECTION);
// Return range of elements
response = await client.redis.zrange("testZadd", 0, 1, REDIS_COLLECTION);
// Response from platform
console.log(response);

// List data type example
const listData = ["iron", "gold", "copper"];
await client.redis.lpush("list", listData, REDIS_COLLECTION);
// Return range of list elements
response = await client.redis.lrange("list", 0, 1, REDIS_COLLECTION);
// Response from platform
console.log(response);

// Hash data type example
// Set hash
await client.redis.hset(
"games",
{"action": "elden", "driving": "GT7"},
REDIS_COLLECTION
);
// Get hash
response = await client.redis.hget("games", "action", REDIS_COLLECTION);
// Response from platform
console.log(response);

// Sets data type example
await client.redis.sadd("animals", ["dog"], REDIS_COLLECTION);
// Pop sets data
response = await client.redis.spop("animals", 1, REDIS_COLLECTION);
// Response from platform
console.log(response);
}

redisExample();