fix: fix Module with IPFS Instance
I tried out both versions and added working examples for both versions ranges
This commit is contained in:
parent
2c9dd9480d
commit
cef97b847f
82
README.md
82
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user