const redis = require('redis');
async function main() {
console.log('🔥 Ignix Node.js Client Example');
console.log('=' .repeat(40));
let client;
try {
console.log('Connecting to Ignix server at localhost:7379...');
client = redis.createClient({
host: 'localhost',
port: 7379,
connect_timeout: 5000,
socket_timeout: 5000
});
client.on('error', (err) => {
console.error('❌ Redis Client Error:', err);
});
client.on('connect', () => {
console.log('🔗 Connected to Ignix server');
});
await client.connect();
console.log('Testing connection...');
const pong = await client.ping();
console.log(`PING response: ${pong}`);
console.log('\n📝 Basic Operations:');
console.log('-'.repeat(20));
console.log("Setting key 'hello' to 'world'...");
await client.set('hello', 'world');
console.log('✅ SET hello world');
const value = await client.get('hello');
console.log(`✅ GET hello: ${value}`);
const exists = await client.exists('hello');
console.log(`✅ EXISTS hello: ${exists}`);
await client.set('counter', '0');
await client.set('user:1:name', 'Alice');
await client.set('user:1:age', '25');
console.log('\n🔢 Counter Operations:');
console.log('-'.repeat(25));
for (let i = 0; i < 5; i++) {
const counter = await client.incr('counter');
console.log(`✅ INCR counter: ${counter}`);
await sleep(100); }
console.log('\n👤 User Data:');
console.log('-'.repeat(15));
const name = await client.get('user:1:name');
const age = await client.get('user:1:age');
console.log(`✅ User: ${name}, Age: ${age}`);
console.log('\n🗂️ Bulk Operations:');
console.log('-'.repeat(20));
await client.mSet([
'fruit:1', 'apple',
'fruit:2', 'banana',
'fruit:3', 'orange'
]);
console.log('✅ MSET fruit:1=apple, fruit:2=banana, fruit:3=orange');
const fruits = await client.mGet(['fruit:1', 'fruit:2', 'fruit:3']);
console.log(`✅ MGET fruits: ${fruits}`);
console.log('\n🔄 Key Management:');
console.log('-'.repeat(20));
await client.rename('hello', 'greeting');
console.log('✅ RENAME hello -> greeting');
const oldExists = await client.exists('hello');
const newExists = await client.exists('greeting');
const newValue = await client.get('greeting');
console.log(`✅ hello exists: ${oldExists}, greeting exists: ${newExists}, value: ${newValue}`);
const deleted = await client.del('greeting');
console.log(`✅ DEL greeting: ${deleted} key(s) deleted`);
console.log('\n📊 Statistics:');
console.log('-'.repeat(15));
const allKeys = await client.keys('*');
console.log(`✅ Total keys: ${allKeys.length}`);
console.log(`✅ Keys: ${allKeys}`);
console.log('\n✅ All operations completed successfully!');
} catch (error) {
if (error.code === 'ECONNREFUSED') {
console.error('❌ Connection Error: Could not connect to Ignix server');
console.error('Make sure Ignix server is running: cargo run --release');
} else {
console.error('❌ Error:', error.message);
}
process.exit(1);
} finally {
if (client) {
await client.quit();
console.log('\n🔌 Disconnected from Ignix server');
}
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
process.exit(1);
});
main().catch(console.error);