From e80da2945c25183b3e4dd608095489c26710846e Mon Sep 17 00:00:00 2001 From: Loosetooth Date: Mon, 5 Apr 2021 21:09:22 +0200 Subject: [PATCH] add open and create functions --- bug.js | 175 +++++++++++++++------------------------------------------ 1 file changed, 46 insertions(+), 129 deletions(-) diff --git a/bug.js b/bug.js index 3e01130..fa6cbe3 100644 --- a/bug.js +++ b/bug.js @@ -1,144 +1,61 @@ -const IPFS = require("ipfs-core") const OrbitDB = require("orbit-db") const IpfsClient = require("ipfs-http-client") -var ipfs = undefined +var ipfs +var orbitdb -const ipfs_config = { - preload: { - enabled: false - }, - EXPERIMENTAL: { - pubsub: true - }, - config: { - Addresses: { - API: '/ip4/127.0.0.1/tcp/0', - Swarm: ['/ip4/0.0.0.0/tcp/0'], - Gateway: '/ip4/0.0.0.0/tcp/0' - }, - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: true, - Interval: 0 - }, - webRTCStar: { - Enabled: false - } +async function init() { + ipfs = IpfsClient({ host: 'localhost', port: '5001', protocol: 'http' }) + orbitdb = await OrbitDB.createInstance(ipfs) +} + +// Creates a certain number of feed databases +// and fills them up with three text values +async function createManyDbs(number) { + await init() + + let dbs = [] + for(let i = 0; i < number; i++) { + let db = await orbitdb.feed("Test" + i) + await db.load() + dbs.push(db) + + for(let j = 0; j<3; j++) { + await db.add({text: "Tweet" + j}) } } + + // Log addresses + console.log("Addresses:") + console.log(dbs.map(db => db.id)) } -const defaultIpfsConfig = { - preload: { enabled: false }, - config: { - Addresses: { - Swarm: [ - // Use IPFS dev signal server - // Websocket: - // '/dns4/ws-star-signal-1.servep2p.com/tcp/443/wss/p2p-websocket-star', - // '/dns4/ws-star-signal-2.servep2p.com/tcp/443/wss/p2p-websocket-star', - // '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star', - // WebRTC: - // '/dns4/star-signal.cloud.ipfs.team/wss/p2p-webrtc-star', - '/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star/', - '/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star/', - '/dns4/webrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/', - // Use local signal server - // '/ip4/0.0.0.0/tcp/9090/wss/p2p-webrtc-star', - ] - } - }, - EXPERIMENTAL: { - pubsub: true - } -} +// Loads a number of feed dbs from their addresses +async function openManyDbs(addresses) { + await init() + let data = [] + let dbs = [] -var orbitdb = undefined + for(let address of addresses) { + let db = await orbitdb.open(address) + await db.load() + dbs.push(db) -async function init_ipfs() { - try { - ipfs = await IpfsClient({ host: 'localhost', port: '5001', protocol: 'http' }) - await ipfs.config.getAll() // Necessary to do a call to make code throw in case of CORS error... - console.log("Succesfully connected to existing node.") - } catch (e) { - console.log("Error while trying to connect to local IPFS node.") - console.log("Now running js-ipfs node...") - try { - ipfs = await IPFS.create(ipfs_config) - } catch (e) { - console.log("Error initializing js-ipfs node.") - console.log(e) - } - } -} - -async function init_orbitdb() { - try { - orbitdb = await OrbitDB.createInstance(ipfs) - } catch (e) { - console.log(e) - } -} - -async function createDBs() { - let db1 = await orbitdb.feed("db1") - await db1.load() - - let db2 = await orbitdb.feed("db2") - await db2.load() - - return { db1: db1, db2: db2 } -} - -async function openExternalDBs(name = "Test", initialize_tweets_in_tweetDB = false) { - await init_ipfs() - await init_orbitdb() - let dbs - - if(initialize_tweets_in_tweetDB) { - await putTweets(dbs) + let elements = await db.iterator({ reverse: true, limit: -1}).collect() + let myData = elements.map((element) => element.payload.value.text) + data.push({name: db.dbname, data: myData}) } - if(!initialize_tweets_in_tweetDB){ - let opendb1 = await orbitdb.feed("/orbitdb/zdpuAtX8772p1ReNvov4Dwr6k3xkAnx62Kap2WdtkPcP4iJaj/bamberibok-tweetDB") - await opendb1.load() - - let opendb2 = await orbitdb.feed("/orbitdb/zdpuAtsDVRVfuW8kckA3UirD7WBZBhZvzN6pexoKo3TL4U6pU/testiebok-tweetDB") - await opendb2.load() - - let opendb3 = await orbitdb.feed("/orbitdb/zdpuAv7453HPrL3eDTQTTLubskR6wXfA44JTSCZog7jL5doZC/iloveduvels-tweetDB") - await opendb3.load() - - let opendb4 = await orbitdb.feed("/orbitdb/zdpuAyFv1KDdcJUjKpWutpu93QDgAfWp5Taw5yxrgMD1N5WuZ/bbboy-tweetDB") - await opendb4.load() - - let payload1 = await opendb1.iterator({ reverse: true, limit: -1 }).collect() - let data1 = payload1.map(element => element.payload.value) - console.log("opendb1 data:") - console.log(data1) - - let payload2 = await opendb2.iterator({ reverse: true, limit: -1 }).collect() - let data2 = payload2.map(element => element.payload.value) - console.log("opendb2 data:") - console.log(data2) - - let payload3 = await opendb3.iterator({ reverse: true, limit: -1 }).collect() - let data3 = payload3.map(element => element.payload.value) - console.log("opendb3 data:") - console.log(data3) - - let payload4 = await opendb4.iterator({ reverse: true, limit: -1 }).collect() - let data4 = payload4.map(element => element.payload.value) - console.log("opendb4 data:") - console.log(data4) - } + console.log(data) } -try { - openExternalDBs() -} catch (e) { - console.log(e) -} +const addresses = [ + '/orbitdb/zdpuAnrgVSgQu6FsgBMuM3HuvkCnatZCPZyDnd8tKNnTjTRV7/Test0', + '/orbitdb/zdpuB3JC6THJ91VrHmdQb6WZAdhV2au91pCrzWN3fGLYZRCga/Test1', + '/orbitdb/zdpuAwSs3AXwd6EUekS254STCJQXP8wyajaLadwuLjYcncsZX/Test2', + '/orbitdb/zdpuAtHT3VkRFQMPzRch7JJgJBeAWAgFMykvX6w4SnH5WDwF2/Test3' + ] + +// openManyDbs(addresses) +createManyDbs(4)