mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
73 lines
2.3 KiB
JavaScript
73 lines
2.3 KiB
JavaScript
'use strict'
|
|
|
|
const IpfsApi = require('exports?IpfsApi!ipfs-api/dist/index.js')
|
|
const OrbitDB = require('../../src/OrbitDB')
|
|
|
|
const username = 'user1'
|
|
const channel = 'browsertest1'
|
|
const key = 'greeting'
|
|
|
|
try {
|
|
const elm = document.getElementById("result")
|
|
const ipfs = IpfsApi('localhost', '5001')
|
|
const orbit = new OrbitDB(ipfs, username)
|
|
|
|
const db = orbit.kvstore(channel)
|
|
const log = orbit.eventlog(channel + ".log")
|
|
const counter = orbit.counter(channel + ".count")
|
|
|
|
const creatures = ['👻', '🐙', '🐷', '🐬', '🐞', '🐈', '🙉', '🐸', '🐓']
|
|
|
|
let count = 1
|
|
const query = () => {
|
|
const startTime = new Date().getTime()
|
|
const idx = Math.floor(Math.random() * creatures.length)
|
|
|
|
// Set a key-value pair
|
|
db.put(key, "db.put #" + count + " - GrEEtinGs to " + creatures[idx])
|
|
.then((res) => {
|
|
const endTime = new Date().getTime()
|
|
console.log(`db.put (#${count}) took ${(endTime - startTime)} ms\n`)
|
|
count ++
|
|
})
|
|
.then(() => counter.inc()) // Increase the counter by one
|
|
.then(() => log.add(creatures[idx])) // Add an event to 'latest visitors' log
|
|
.then(() => {
|
|
const result = db.get(key)
|
|
const latest = log.iterator({ limit: 5 }).collect()
|
|
const count = counter.value()
|
|
|
|
const output =
|
|
`<b>Key-Value Store</b>
|
|
---------------------------------------------------
|
|
Key | Value
|
|
---------------------------------------------------
|
|
${key} | ${result}
|
|
---------------------------------------------------
|
|
|
|
<b>Eventlog</b>
|
|
---------------------------------------------------
|
|
Latest Visitors
|
|
---------------------------------------------------
|
|
${latest.reverse().map((e) => e.payload.value + " - " + new Date(e.payload.meta.ts).toISOString()).join('\n')}
|
|
|
|
<b>Counter</b>
|
|
---------------------------------------------------
|
|
Visitor Count: ${count}
|
|
---------------------------------------------------
|
|
`
|
|
elm.innerHTML = output.split("\n").join("<br>")
|
|
console.log(output)
|
|
})
|
|
.catch((e) => {
|
|
elm.innerHTML = "<i>" + e.message + "</i><br><br>" + "Make sure you have an IPFS daemon running at localhost:5001"
|
|
console.error(e.stack)
|
|
})
|
|
}
|
|
setInterval(query, 1000)
|
|
|
|
} catch(e) {
|
|
console.error(e.stack)
|
|
elm.innerHTML = e.message
|
|
}
|