We have an Redis database from where I want to retrieve all keys from but i cant figure out how. The Db is consisting of key spaces with an unlimited amount of keys which all hold a unique JSON
This is our custom connector:
export async function getMSData(key:string, type:"JSON") {
await connect()
if (type == "JSON") {
return await client.json.get(key)
}
return null
}
the following is our kindof middleware:
'use server'
import { getMSData } from "@/lib/RedisConnector";
export default async function getRemoteConfigs(wkspcid: string) {
const configData = await getMSData(wkspcid, "JSON");
return (configData);
}
and here is how we call it from the frontend:
getRemoteConfigs(`Remote_Configs_${props.currentWkspcid}:*`)
The problem isn't the connection itself because if we call a specific key like this:
getRemoteConfigs(`Remote_Configs_${props.currentWkspcid}:keyname`)
we get a totaly fine response from our database
our DB structure is as follows: Redis key structure (if it helps)
I've already tried other syntax approaches inside the the argument string inside the 'getRemoteConfigs' function which I gathered from google and copilot but neither worked.
This line
client.json.get(key);
only fetches a single value by key from redis. In order to fetch all values with a key matching the wildcard
`Remote_Configs_${props.currentWkspcid}:*`
You need to find all keys in redis matching the wildcard and then iterate over each one and fetch it's value. Eg.
export async function getMSData(key:string, type:"JSON") {
await connect();
// Retrieve all matching keys
const keys = await client.keys(`${prefix}:*`);
if (type == "JSON") {
// Retrieve the value for each key.
return await Promise.all(
keys.map(async (key) => {
const data = await client.json.get(key);
return { key, data };
})
);
}
return null;
}
Note: The keys command will run slow if used on large datasets and should be used with extreme care. See https://redis.io/docs/latest/commands/keys/