fix: fix Module with IPFS Instance

I tried out both versions and added working examples for both versions ranges
This commit is contained in:
vasa 2020-03-25 23:46:51 +05:30 committed by Mark Robert Henderson
parent 2c9dd9480d
commit cef97b847f

View File

@ -93,46 +93,70 @@ npm install orbit-db ipfs
const IPFS = require('ipfs') const IPFS = require('ipfs')
const OrbitDB = require('orbit-db') const OrbitDB = require('orbit-db')
// For js-ipfs >= 0.38
// Create IPFS instance // Create IPFS instance
var ipfs;
const initIPFSInstance = async () => { const initIPFSInstance = async () => {
// For js-ipfs >= 0.38 return await IPFS.create({ repo: "./path-for-js-ipfs-repo" });
ipfs = await IPFS.create({ repo: "./path-for-js-ipfs-repo" }); };
// For js-ipfs < 0.38 initIPFSInstance().then(async ipfs => {
const ipfsOptions = { 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: { EXPERIMENTAL: {
pubsub: true pubsub: true
} }
} };
ipfs = new IPFS(ipfsOptions)
}
initIPFSInstance() ipfs = new IPFS(ipfsOptions);
ipfs.on('error', (e) => console.error(e)) initIPFSInstance().then(ipfs => {
ipfs.on('ready', async () => { ipfs.on("error", e => console.error(e));
const orbitdb = await OrbitDB.createInstance(ipfs) ipfs.on("ready", async () => {
const orbitdb = await OrbitDB.createInstance(ipfs);
// Create / Open a database // Create / Open a database
const db = await orbitdb.log('hello') const db = await orbitdb.log("hello");
await db.load() await db.load();
// Listen for updates from peers // Listen for updates from peers
db.events.on('replicated', (address) => { db.events.on("replicated", address => {
console.log(db.iterator({ limit: -1 }).collect()) console.log(db.iterator({ limit: -1 }).collect());
}) });
// Add an entry // Add an entry
const hash = await db.add('world') const hash = await db.add("world");
console.log(hash) 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 ### Module with IPFS Daemon