From cef97b847fc4cb83db615b1ceb66a93de3fd72f2 Mon Sep 17 00:00:00 2001 From: vasa Date: Wed, 25 Mar 2020 23:46:51 +0530 Subject: [PATCH] fix: fix Module with IPFS Instance I tried out both versions and added working examples for both versions ranges --- README.md | 82 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index d3b0992..de6da48 100644 --- a/README.md +++ b/README.md @@ -93,46 +93,70 @@ npm install orbit-db ipfs const IPFS = require('ipfs') const OrbitDB = require('orbit-db') +// For js-ipfs >= 0.38 + // Create IPFS instance - -var ipfs; - const initIPFSInstance = async () => { - // For js-ipfs >= 0.38 - ipfs = await IPFS.create({ repo: "./path-for-js-ipfs-repo" }); - - // For js-ipfs < 0.38 - const ipfsOptions = { + return await IPFS.create({ repo: "./path-for-js-ipfs-repo" }); +}; + +initIPFSInstance().then(async ipfs => { + const orbitdb = await OrbitDB.createInstance(ipfs); + + // Create / Open a database + const db = await orbitdb.log("hello"); + await db.load(); + + // Listen for updates from peers + db.events.on("replicated", address => { + console.log(db.iterator({ limit: -1 }).collect()); + }); + + // Add an entry + const hash = await db.add("world"); + console.log(hash); + + // Query + const result = db.iterator({ limit: -1 }).collect(); + console.log(JSON.stringify(result, null, 2)); +}); + + +// For js-ipfs < 0.38 + +// Create IPFS instance +const ipfsOptions = { EXPERIMENTAL: { pubsub: true } - } - ipfs = new IPFS(ipfsOptions) -} + }; -initIPFSInstance() +ipfs = new IPFS(ipfsOptions); -ipfs.on('error', (e) => console.error(e)) -ipfs.on('ready', async () => { - const orbitdb = await OrbitDB.createInstance(ipfs) +initIPFSInstance().then(ipfs => { + ipfs.on("error", e => console.error(e)); + ipfs.on("ready", async () => { + const orbitdb = await OrbitDB.createInstance(ipfs); - // Create / Open a database - const db = await orbitdb.log('hello') - await db.load() + // Create / Open a database + const db = await orbitdb.log("hello"); + await db.load(); - // Listen for updates from peers - db.events.on('replicated', (address) => { - console.log(db.iterator({ limit: -1 }).collect()) - }) + // Listen for updates from peers + db.events.on("replicated", address => { + console.log(db.iterator({ limit: -1 }).collect()); + }); - // Add an entry - const hash = await db.add('world') - console.log(hash) + // Add an entry + const hash = await db.add("world"); + console.log(hash); + + // Query + const result = db.iterator({ limit: -1 }).collect(); + console.log(JSON.stringify(result, null, 2)); + }); +}); - // Query - const result = db.iterator({ limit: -1 }).collect() - console.log(JSON.stringify(result, null, 2)) -}) ``` ### Module with IPFS Daemon