orbit-db/examples/reader.js
2016-04-13 09:53:03 +02:00

58 lines
1.7 KiB
JavaScript

'use strict';
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const OrbitClient = require('../src/Client');
const Timer = require('./Timer');
// usage: reader.js <host> <username> <channel> <data> <interval in ms>
// orbit-server
const host = process.argv[2] ? process.argv[2] : 'localhost'
const port = 3333;
const username = process.argv[3] ? process.argv[3] : 'LambOfGod';
const password = '';
const channelName = process.argv[4] ? process.argv[4] : 'test';
const prefix = process.argv[5] ? process.argv[5] : 'Hello';
let run = (async(() => {
try {
const orbit = await(OrbitClient.connect(host, port, username, password));
const db = await(orbit.channel(channelName));
let count = 1;
let running = false;
setInterval(async(() => {
if(!running) {
running = true;
let timer = new Timer(true);
await(db.add(prefix + count));
console.log(`Query #${count} took ${timer.stop(true)} ms\n`);
let timer2 = new Timer(true);
let items = db.iterator({ limit: 20 }).collect();
console.log("---------------------------------------------------")
console.log("Key | Value")
console.log("---------------------------------------------------")
console.log(items.map((e) => `${e.key} | ${e.value}`).join("\n"));
console.log("---------------------------------------------------")
console.log(`Query 2 #${count} took ${timer2.stop(true)} ms\n`);
running = false;
count ++;
}
}), process.argv[6] ? process.argv[6] : 1000);
} catch(e) {
console.error(e.stack);
console.log("Exiting...")
process.exit(1);
}
}))();
module.exports = run;