-
Notifications
You must be signed in to change notification settings - Fork 1
/
up.js
61 lines (50 loc) · 1.77 KB
/
up.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')
// Pubsub option must be passed to all components
const ipfsOptions = {
start: true,
EXPERIMENTAL: {
pubsub: true,
},
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
]
},
}
}
// Create IPFS instance and pass the pubsub option in
const ipfs = new IPFS(ipfsOptions)
console.log('IPFS ✅')
// Handle errors
ipfs.on('error', (e) => console.error(e))
// Start working with OrbitDB once the IPFS instance is ready
ipfs.on('ready', async () => {
// Create OrbitDB instance on the IPFS instance
const orbitdb = new OrbitDB(ipfs)
console.log('OrbitDB ✅')
// Create our docs type database using index-by-name option
const db = await orbitdb.docs('fav_emoji', {
indexBy: 'name',
write: ['*'] // Allow anyone write access
})
// Get our OrbitDB address so others can access it
// Write to file / send by HTTP for access in other components
const address = db.address.toString()
console.log('Address: ' + address)
// Add three documents, e.g. representing people
await db.put({ _id: 'ID1', name: 'Alice', age: 17, emoji: '🐳' })
await db.put({ _id: 'ID2', name: 'Bob', age: 18, emoji: '💰' })
await db.put({ _id: 'ID3', name: 'Carol', age: 19, emoji: '😂' })
console.log('3 users have been added to the database.')
// Get a doc
console.log("Alice's file looks like this:")
const profile = db.get('Alice')
console.log(profile)
// COMMENT THIS BLOCK OUT TO REMAIN A PEER (NODE/HOST) SO OTHERS CAN REPLICATE
// Disconnect
await orbitdb.disconnect()
ipfs.stop(() => {})
console.log('IPFS connection closed ❌')
})