Open simonprickett opened 2 years ago
const redis = require('redis');
async function getJsonData(client, key, options) {
// Fetch JSON data from Redis
const rawResults = await client.json.get(key, { path: options.path });
// Check if responseMap exists and process the results
if (options.responseMap) {
const mappedResults = {};
for (const [originalKey, newKey] of Object.entries(options.responseMap)) {
if (rawResults[originalKey] !== undefined) {
mappedResults[newKey] = rawResults[originalKey];
}
}
return mappedResults; // Return the mapped results
}
return rawResults; // Return raw results if no responseMap is provided
}
// Usage example
(async () => {
const client = redis.createClient();
try {
await client.connect();
const results = await getJsonData(client, 'noderedis:jsondata', {
path: ['.pets[1].name', '.pets[1].age'],
responseMap: {
'.pets[1].name': 'name',
'.pets[1].age': 'age'
}
});
console.log(results); // Expected output: { name: 'Rex', age: 3 }
} catch (err) {
console.error('Error fetching JSON data:', err);
} finally {
await client.quit();
}
})();
const redis = require('redis');
async function getJsonData(client, key, options) {
// Fetch JSON data from Redis
const rawResults = await client.json.get(key, { path: options.path });
// Check if responseMap exists and process the results
if (options.responseMap) {
const mappedResults = {};
for (const [originalKey, newKey] of Object.entries(options.responseMap)) {
if (rawResults[originalKey] !== undefined) {
mappedResults[newKey] = rawResults[originalKey];
}
}
return mappedResults; // Return the mapped results
}
return rawResults; // Return raw results if no responseMap is provided
}
// Usage example
(async () => {
const client = redis.createClient();
try {
await client.connect();
const results = await getJsonData(client, 'noderedis:jsondata', {
path: ['.pets[1].name', '.pets[1].age'],
responseMap: {
'.pets[1].name': 'name',
'.pets[1].age': 'age'
}
});
console.log(results); // Expected output: { name: 'Rex', age: 3 }
} catch (err) {
console.error('Error fetching JSON data:', err);
} finally {
await client.quit();
}
})();
When we perform a search over JSON document content, it would be nice to provide an extra object parameter describing how to name fields in the objects returned...
Example:
might return:
Proposing that something like this:
would return: