orbitdb/examples/events.js
Hayden Young 8a97a39047
Docs (#81)
* docs: Update README to match new version.

* docs: Update events example to use new API.

* docs: Correctly print out db query results.

* docs: Describe keyvalue-indexed db.

* docs: Notes about Helia migration.

* docs: Simplify update docs.

* docs: Using the iterator to display results.

* docs: Remove packages.

* chore: Shorten path.

* docs: Show db address for using with replicated peers or re-opening at a later time.

* docs: Import correct ipfs package.
2023-06-13 18:49:01 +08:00

49 lines
1.2 KiB
JavaScript

import { create } from 'ipfs-core'
import OrbitDB from '../src/orbitdb.js'
const creatures = ['🐙', '🐷', '🐬', '🐞', '🐈', '🙉', '🐸', '🐓']
console.log("Starting...")
async function main () {
let db
try {
const ipfs = await create({
repo: './orbitdb/examples/ipfs',
start: true,
EXPERIMENTAL: {
pubsub: true,
},
})
const orbitdb = await OrbitDB({ ipfs, directory: './orbitdb/examples' })
db = await orbitdb.open('example')
} catch (e) {
console.error(e)
process.exit(1)
}
const query = async () => {
const index = Math.floor(Math.random() * creatures.length)
const userId = Math.floor(Math.random() * 900 + 100)
try {
await db.add({ avatar: creatures[index], userId: userId })
let latest = await db.all()
let output = ``
output += `[Latest Visitors]\n`
output += `--------------------\n`
output += `ID | Visitor\n`
output += `--------------------\n`
output += latest.reverse().map((e) => e.value.userId + ' | ' + e.value.avatar).join('\n') + `\n`
console.log(output)
} catch (e) {
console.error(e)
process.exit(1)
}
}
setInterval(query, 1000)
}
main()