mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00

Use latest store modules from npm Update README Update docs Update examples Update benchmarks Update dependencies Add Getting Started guide Add new a screenshot Add a new live demo Add persistency tests for snapshot saving/loading and events Add network stress tests (but skip them by default as they're very heavy and lengthy) Add browser benchmarks Add log() alias for eventlog() database Add possibility to create database if it doesn't exist yet Add support for orbitdb addresses Add a test for starting replication when peers connect Add debug build Use IPFS nodeID as default user id Use ipfs-pubsub-room Handle closing of databases properly Handle cache errors Clean up tests, re-organize code files Clean up code style Support for CLI Remove obsolete scripts
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
'use strict'
|
|
|
|
const IPFS = require('ipfs')
|
|
const OrbitDB = require('../src/OrbitDB')
|
|
|
|
const userId = 1
|
|
const creatures = ['🐙', '🐬', '🐋', '🐠', '🐡', '🦀', '🐢', '🐟', '🐳']
|
|
|
|
const output = (user) => {
|
|
let output = ``
|
|
output += `----------------------\n`
|
|
output += `User\n`
|
|
output += `----------------------\n`
|
|
output += `Id: ${userId}\n`
|
|
output += `Avatar: ${user.avatar}\n`
|
|
output += `Updated: ${user.updated}\n`
|
|
output += `----------------------\n`
|
|
console.log(output)
|
|
}
|
|
|
|
console.log("Starting...")
|
|
|
|
const ipfs = new IPFS({
|
|
repo: './orbitdb/examples/ipfs',
|
|
start: true,
|
|
EXPERIMENTAL: {
|
|
pubsub: true,
|
|
},
|
|
})
|
|
|
|
ipfs.on('error', (err) => console.error(err))
|
|
|
|
ipfs.on('ready', async () => {
|
|
let db
|
|
try {
|
|
const orbitdb = new OrbitDB(ipfs, './orbitdb/examples/eventlog')
|
|
db = await orbitdb.kvstore('example', { overwrite: true })
|
|
await db.load()
|
|
// Query immediately after loading
|
|
const user = db.get(userId)
|
|
output(user)
|
|
} catch (e) {
|
|
console.error(e)
|
|
process.exit(1)
|
|
}
|
|
|
|
const query = async () => {
|
|
// Randomly select an avatar
|
|
const index = Math.floor(Math.random() * creatures.length)
|
|
|
|
// Set the key to the newly selected avatar and update the timestamp
|
|
await db.put(userId, { avatar: creatures[index], updated: new Date().getTime() })
|
|
|
|
// Get the value of the key
|
|
const user = db.get(userId)
|
|
|
|
// Display the value
|
|
output(user)
|
|
}
|
|
|
|
console.log("Starting update loop...")
|
|
setInterval(query, 1000)
|
|
})
|