mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-28 00:42:29 +00:00
Update an eventlog example and add start-daemon example
This commit is contained in:
parent
fb63cfc346
commit
65a1061d63
@ -1,30 +1,42 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const IpfsApi = require('ipfs-api')
|
const IpfsDaemon = require('ipfs-daemon')
|
||||||
const OrbitDB = require('../src/OrbitDB')
|
const OrbitDB = require('../src/OrbitDB')
|
||||||
|
|
||||||
const ipfs = IpfsApi('localhost', '5001')
|
const userId = Math.floor(Math.random() * 100)
|
||||||
const orbitdb = new OrbitDB(ipfs)
|
const conf = {
|
||||||
const db = orbitdb.eventlog("/orbit-db/examples/eventlog-example")
|
IpfsDataDir: '/tmp/' + userId,
|
||||||
|
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'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
const creatures = ['🐙', '🐷', '🐬', '🐞', '🐈', '🙉', '🐸', '🐓']
|
IpfsDaemon(conf)
|
||||||
|
.then((res) => {
|
||||||
|
const orbitdb = new OrbitDB(res.ipfs)
|
||||||
|
const db = orbitdb.eventlog("|orbit-db|examples|eventlog-example")
|
||||||
|
|
||||||
const query = () => {
|
const creatures = ['🐙', '🐷', '🐬', '🐞', '🐈', '🙉', '🐸', '🐓']
|
||||||
|
|
||||||
|
const query = () => {
|
||||||
const index = Math.floor(Math.random() * creatures.length)
|
const index = Math.floor(Math.random() * creatures.length)
|
||||||
db.add(creatures[index])
|
db.add({ avatar: creatures[index], userId: userId })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const latest = db.iterator({ limit: 5 }).collect()
|
const latest = db.iterator({ limit: 5 }).collect()
|
||||||
let output = ``
|
let output = ``
|
||||||
output += `---------------------------------------------------\n`
|
output += `--------------------\n`
|
||||||
output += `Latest Visitors\n`
|
output += `Latest Visitors\n`
|
||||||
output += `---------------------------------------------------\n`
|
output += `--------------------\n`
|
||||||
output += latest.reverse().map((e) => e.payload.value).join('\n') + `\n`
|
output += latest.reverse().map((e) => e.payload.value.avatar + " (userId: " + e.payload.value.userId + ")").join('\n') + `\n`
|
||||||
console.log(output)
|
console.log(output)
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
console.error(e.stack)
|
console.error(e.stack)
|
||||||
console.log("Make sure you have an IPFS daemon running at localhost:5001")
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(query, 1000)
|
setInterval(query, 1000)
|
||||||
|
})
|
||||||
|
.catch((err) => console.error(err))
|
||||||
|
13
examples/start-daemon.js
Normal file
13
examples/start-daemon.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
const IpfsDaemon = require('ipfs-daemon')
|
||||||
|
module.exports = IpfsDaemon({
|
||||||
|
IpfsDataDir: './tmp',
|
||||||
|
API: {
|
||||||
|
HTTPHeaders: {
|
||||||
|
"Access-Control-Allow-Origin": ['*'],
|
||||||
|
"Access-Control-Allow-Methods": ["PUT", "GET", "POST"],
|
||||||
|
"Access-Control-Allow-Credentials": ["true"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((res) => console.log("started"))
|
||||||
|
.catch((err) => console.error(err))
|
Loading…
x
Reference in New Issue
Block a user