mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-06 14:16:37 +00:00
Merge pull request #1085 from orbitdb/fix/remove-examples
Remove obsolete example code
This commit is contained in:
commit
53e8b35fc9
5
.gitignore
vendored
5
.gitignore
vendored
@ -9,11 +9,6 @@ test/ipfs/
|
||||
test/browser/ipfs/
|
||||
test/browser/bundle.js*
|
||||
|
||||
# Don't track examples' dependencies (libs) in git
|
||||
examples/browser/browser-webpack-example/bundle.js
|
||||
examples/browser/browser-webpack-example/*.map
|
||||
examples/browser/lib
|
||||
|
||||
# Don't track builds in git
|
||||
# Distribution builds are available via npm
|
||||
dist/
|
||||
|
@ -4,10 +4,6 @@ orbitdb/
|
||||
# Don't distribute the dependencies
|
||||
node_modules/
|
||||
|
||||
# Don't distribute examples with the module
|
||||
# See examples at https://github.com/orbitdb/orbit-db
|
||||
examples/
|
||||
|
||||
# Don't distribute source maps
|
||||
dist/*.map
|
||||
|
||||
|
6
Makefile
6
Makefile
@ -9,13 +9,7 @@ test: deps
|
||||
build: test
|
||||
mkdir -p examples/browser/lib/
|
||||
npm run build
|
||||
cp dist/orbitdb.min.js examples/browser/lib/orbitdb.min.js
|
||||
cp dist/orbitdb.js examples/browser/lib/orbitdb.js
|
||||
cp dist/orbitdb.js.map examples/browser/lib/orbitdb.js.map
|
||||
cp node_modules/ipfs/dist/index.min.js examples/browser/lib/ipfs.min.js
|
||||
cp node_modules/ipfs/dist/index.min.js examples/browser/lib/ipfs.js
|
||||
@echo "Build success!"
|
||||
@echo "Output: 'dist/', 'examples/browser/'"
|
||||
|
||||
clean:
|
||||
rm -rf node_modules/
|
||||
|
@ -1,41 +0,0 @@
|
||||
body {
|
||||
font-family: 'Abel', sans-serif;
|
||||
font-size: 0.8em;
|
||||
}
|
||||
|
||||
#logo {
|
||||
border-top: 1px dotted black;
|
||||
border-bottom: 1px dotted black;
|
||||
}
|
||||
|
||||
#status {
|
||||
border-top: 1px dotted black;
|
||||
border-bottom: 1px dotted black;
|
||||
padding: 0.5em 0em;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#results {
|
||||
border: 1px dotted black;
|
||||
padding: 0.5em;
|
||||
}
|
||||
|
||||
#writerText {
|
||||
padding-top: 0.5em;
|
||||
}
|
||||
|
||||
pre {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
input {
|
||||
padding: 0.5em;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin-bottom: 0.2em;
|
||||
}
|
||||
h3 {
|
||||
margin-top: 0.8em;
|
||||
margin-bottom: 0.2em;
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<link href="browser.css" rel="stylesheet">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<a href="https://github.com/orbitdb/orbit-db" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
|
||||
|
||||
<div id="logo">
|
||||
<pre>
|
||||
_ _ _ _ _
|
||||
| | (_) | | | |
|
||||
___ _ __| |__ _| |_ __| | |__
|
||||
/ _ \| '__| '_ \| | __| / _\` | '_\
|
||||
| (_) | | | |_) | | |_ | (_| | |_) |
|
||||
\___/|_| |_.__/|_|\__| \__,_|_.__/
|
||||
|
||||
Peer-to-Peer Database for the Decentralized Web
|
||||
<a href="https://github.com/orbitdb/orbit-db" target="_blank">https://github.com/orbitdb/orbit-db</a>
|
||||
</pre>
|
||||
</div>
|
||||
<h2>Open or Create Local Database</h2>
|
||||
<i>Open a database locally and create it if the database doesn't exist.</i>
|
||||
<br><br>
|
||||
<input id="dbname" type="text" placeholder="Database name"/>
|
||||
<button id="create" type="button" disabled>Open</button>
|
||||
<select id="type">
|
||||
<option value="eventlog">Eventlog</option>
|
||||
<option value="feed">Feed</option>
|
||||
<option value="keyvalue">Key-Value</option>
|
||||
<option value="docstore">DocumentDB</option>
|
||||
<option value="counter">Counter</option>
|
||||
</select>
|
||||
<input id="public" type="checkbox" checked> Public
|
||||
|
||||
<h2>Open Remote Database</h2>
|
||||
<i>Open a database from an OrbitDB address, eg. /orbitdb/QmfY3udPcWUD5NREjrUV351Cia7q4DXNcfyRLJzUPL3wPD/hello</i>
|
||||
<br>
|
||||
<i><b>Note!</b> Open the remote database in an Incognito Window or in a different browser. It won't work if you don't.</i>
|
||||
<br><br>
|
||||
<input id="dbaddress" type="text" placeholder="Address"/>
|
||||
<button id="open" type="button" disabled>Open</button>
|
||||
<input id="readonly" type="checkbox" checked> Read-only
|
||||
<br><br>
|
||||
<div id="status">Init</div>
|
||||
<div>
|
||||
<header id="output-header"></header>
|
||||
<div id="output"></div>
|
||||
</div>
|
||||
<div id="writerText"></div>
|
||||
|
||||
<script type="text/javascript" src="bundle.js" charset="utf-8"></script>
|
||||
<link href="https://fonts.googleapis.com/css?family=Abel" rel="stylesheet">
|
||||
</body>
|
||||
</html>
|
@ -1,27 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
/*
|
||||
This is the entry point for Webpack to build the bundle from.
|
||||
We use the same example code as the html browser example,
|
||||
but we inject the Node.js modules of OrbitDB and IPFS into
|
||||
the example.
|
||||
|
||||
In the html example, IPFS and OrbitDB are loaded by ../browser.html from the
|
||||
minified distribution builds
|
||||
*/
|
||||
|
||||
// Import IPFS module
|
||||
import IPFS from 'ipfs'
|
||||
|
||||
// Import OrbitDB module from 'orbit-db', eg. directory to its package.json
|
||||
import OrbitDB from '../../..'
|
||||
|
||||
// When 'orbit-db' was installed from npm, use with:
|
||||
// import OrbitDB from 'orbit-db'
|
||||
|
||||
// Example main code
|
||||
const example = require('../example')
|
||||
|
||||
// Call the start function and pass in the
|
||||
// IPFS and OrbitDB modules
|
||||
example(IPFS, OrbitDB)
|
@ -1,50 +0,0 @@
|
||||
body {
|
||||
font-family: 'Abel', sans-serif;
|
||||
font-size: 0.8em;
|
||||
}
|
||||
|
||||
#logo {
|
||||
border-top: 1px dotted black;
|
||||
border-bottom: 1px dotted black;
|
||||
}
|
||||
|
||||
#status {
|
||||
border-top: 1px dotted black;
|
||||
border-bottom: 1px dotted black;
|
||||
padding: 0.5em 0em;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#results {
|
||||
border: 1px dotted black;
|
||||
padding: 0.5em;
|
||||
}
|
||||
|
||||
#output-header > p {
|
||||
margin: 0;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
#output {
|
||||
padding-top: 1em;
|
||||
}
|
||||
|
||||
#writerText {
|
||||
padding-top: 0.5em;
|
||||
}
|
||||
|
||||
pre {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
input {
|
||||
padding: 0.5em;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin-bottom: 0.2em;
|
||||
}
|
||||
h3 {
|
||||
margin-top: 0.8em;
|
||||
margin-bottom: 0.2em;
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<link href="browser.css" rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/css?family=Abel" rel="stylesheet">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<a href="https://github.com/orbitdb/orbit-db" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
|
||||
|
||||
<div id="logo">
|
||||
<pre>
|
||||
_ _ _ _ _
|
||||
| | (_) | | | |
|
||||
___ _ __| |__ _| |_ __| | |__
|
||||
/ _ \| '__| '_ \| | __| / _\` | '_\
|
||||
| (_) | | | |_) | | |_ | (_| | |_) |
|
||||
\___/|_| |_.__/|_|\__| \__,_|_.__/
|
||||
|
||||
Peer-to-Peer Database for the Decentralized Web
|
||||
<a href="https://github.com/orbitdb/orbit-db" target="_blank">https://github.com/orbitdb/orbit-db</a>
|
||||
</pre>
|
||||
</div>
|
||||
<h2>Open or Create Local Database</h2>
|
||||
<i>Open a database locally and create it if the database doesn't exist.</i>
|
||||
<br><br>
|
||||
<input id="dbname" type="text" placeholder="Database name"/>
|
||||
<button id="create" type="button" disabled>Open</button>
|
||||
<select id="type">
|
||||
<option value="eventlog">Eventlog</option>
|
||||
<option value="feed">Feed</option>
|
||||
<option value="keyvalue">Key-Value</option>
|
||||
<option value="docstore">DocumentDB</option>
|
||||
<option value="counter">Counter</option>
|
||||
</select>
|
||||
<input id="public" type="checkbox" checked> Public
|
||||
|
||||
<h2>Open Remote Database</h2>
|
||||
<i>Open a database from an OrbitDB address, eg. /orbitdb/QmfY3udPcWUD5NREjrUV351Cia7q4DXNcfyRLJzUPL3wPD/hello</i>
|
||||
<br>
|
||||
<i><b>Note!</b> Open the remote database in an Incognito Window or in a different browser. It won't work if you don't.</i>
|
||||
<br><br>
|
||||
<input id="dbaddress" type="text" placeholder="Address"/>
|
||||
<button id="open" type="button" disabled>Open</button>
|
||||
<input id="readonly" type="checkbox" checked> Read-only
|
||||
<br><br>
|
||||
<div id="status">Init</div>
|
||||
<div>
|
||||
<header id="output-header"></header>
|
||||
<div id="output"></div>
|
||||
</div>
|
||||
<div id="writerText"></div>
|
||||
|
||||
<script type="text/javascript" src="lib/orbitdb.js" charset="utf-8"></script>
|
||||
<script type="text/javascript" src="lib/ipfs.js" charset="utf-8"></script>
|
||||
<script type="text/javascript" src="example.js" charset="utf-8"></script>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
// Start the example
|
||||
main()
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -1,282 +0,0 @@
|
||||
const creatures = [
|
||||
'🐙', '🐷', '🐬', '🐞',
|
||||
'🐈', '🙉', '🐸', '🐓',
|
||||
'🐊', '🕷', '🐠', '🐘',
|
||||
'🐼', '🐰', '🐶', '🐥'
|
||||
]
|
||||
|
||||
const outputHeaderElm = document.getElementById("output-header")
|
||||
const outputElm = document.getElementById("output")
|
||||
const statusElm = document.getElementById("status")
|
||||
const dbnameField = document.getElementById("dbname")
|
||||
const dbAddressField = document.getElementById("dbaddress")
|
||||
const createButton = document.getElementById("create")
|
||||
const openButton = document.getElementById("open")
|
||||
const createType = document.getElementById("type")
|
||||
const writerText = document.getElementById("writerText")
|
||||
const publicCheckbox = document.getElementById("public")
|
||||
const readonlyCheckbox = document.getElementById("readonly")
|
||||
|
||||
function handleError(e) {
|
||||
console.error(e.stack)
|
||||
statusElm.innerHTML = e.message
|
||||
}
|
||||
|
||||
const main = async (IPFS, ORBITDB) => {
|
||||
let orbitdb, db
|
||||
let count = 0
|
||||
let interval = Math.floor((Math.random() * 300) + (Math.random() * 2000))
|
||||
let updateInterval
|
||||
let dbType, dbAddress
|
||||
|
||||
// If we're building with Webpack, use the injected IPFS module.
|
||||
// Otherwise use 'Ipfs' which is exposed by ipfs.min.js
|
||||
if (IPFS)
|
||||
Ipfs = IPFS
|
||||
|
||||
// If we're building with Webpack, use the injected OrbitDB module.
|
||||
// Otherwise use 'OrbitDB' which is exposed by orbitdb.min.js
|
||||
if (ORBITDB)
|
||||
OrbitDB = ORBITDB
|
||||
|
||||
// Init UI
|
||||
openButton.disabled = true
|
||||
createButton.disabled = true
|
||||
statusElm.innerHTML = "Starting IPFS..."
|
||||
|
||||
// Create IPFS instance
|
||||
const ipfs = await Ipfs.create({
|
||||
repo: '/orbitdb/examples/browser/new/ipfs/0.33.1',
|
||||
start: true,
|
||||
preload: {
|
||||
enabled: false
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: true,
|
||||
},
|
||||
config: {
|
||||
Addresses: {
|
||||
Swarm: [
|
||||
// Use IPFS dev signal server
|
||||
// '/dns4/star-signal.cloud.ipfs.team/wss/p2p-webrtc-star',
|
||||
// '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
|
||||
// Use IPFS dev webrtc signal server
|
||||
'/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star/',
|
||||
'/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star/',
|
||||
'/dns4/webrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/',
|
||||
// Use local signal server
|
||||
// '/ip4/0.0.0.0/tcp/9090/wss/p2p-webrtc-star',
|
||||
]
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
openButton.disabled = false
|
||||
createButton.disabled = false
|
||||
statusElm.innerHTML = "IPFS Started"
|
||||
orbitdb = await OrbitDB.createInstance(ipfs)
|
||||
|
||||
const load = async (db, statusText) => {
|
||||
// Set the status text
|
||||
statusElm.innerHTML = statusText
|
||||
|
||||
// When the database is ready (ie. loaded), display results
|
||||
db.events.on('ready', () => queryAndRender(db))
|
||||
// When database gets replicated with a peer, display results
|
||||
db.events.on('replicated', () => queryAndRender(db))
|
||||
// When we update the database, display result
|
||||
db.events.on('write', () => queryAndRender(db))
|
||||
|
||||
db.events.on('replicate.progress', () => queryAndRender(db))
|
||||
|
||||
// Hook up to the load progress event and render the progress
|
||||
let maxTotal = 0, loaded = 0
|
||||
db.events.on('load.progress', (address, hash, entry, progress, total) => {
|
||||
loaded ++
|
||||
maxTotal = Math.max.apply(null, [maxTotal, progress, 0])
|
||||
total = Math.max.apply(null, [progress, maxTotal, total, entry.clock.time, 0])
|
||||
statusElm.innerHTML = `Loading database... ${maxTotal} / ${total}`
|
||||
})
|
||||
|
||||
db.events.on('ready', () => {
|
||||
// Set the status text
|
||||
setTimeout(() => {
|
||||
statusElm.innerHTML = 'Database is ready'
|
||||
}, 1000)
|
||||
})
|
||||
|
||||
// Load locally persisted database
|
||||
await db.load()
|
||||
}
|
||||
|
||||
const startWriter = async (db, interval) => {
|
||||
// Set the status text
|
||||
writerText.innerHTML = `Writing to database every ${interval} milliseconds...`
|
||||
|
||||
// Start update/insert loop
|
||||
updateInterval = setInterval(async () => {
|
||||
try {
|
||||
await update(db)
|
||||
} catch (e) {
|
||||
console.error(e.toString())
|
||||
writerText.innerHTML = '<span style="color: red">' + e.toString() + '</span>'
|
||||
clearInterval(updateInterval)
|
||||
}
|
||||
}, interval)
|
||||
}
|
||||
|
||||
const resetDatabase = async (db) => {
|
||||
writerText.innerHTML = ""
|
||||
outputElm.innerHTML = ""
|
||||
outputHeaderElm.innerHTML = ""
|
||||
|
||||
clearInterval(updateInterval)
|
||||
|
||||
if (db) {
|
||||
await db.close()
|
||||
}
|
||||
|
||||
interval = Math.floor((Math.random() * 300) + (Math.random() * 2000))
|
||||
}
|
||||
|
||||
const createDatabase = async () => {
|
||||
await resetDatabase(db)
|
||||
|
||||
openButton.disabled = true
|
||||
createButton.disabled = true
|
||||
|
||||
try {
|
||||
const name = dbnameField.value
|
||||
const type = createType.value
|
||||
const publicAccess = publicCheckbox.checked
|
||||
|
||||
db = await orbitdb.open(name, {
|
||||
// If database doesn't exist, create it
|
||||
create: true,
|
||||
overwrite: true,
|
||||
// Load only the local version of the database,
|
||||
// don't load the latest from the network yet
|
||||
localOnly: false,
|
||||
type: type,
|
||||
// If "Public" flag is set, allow anyone to write to the database,
|
||||
// otherwise only the creator of the database can write
|
||||
accessController: {
|
||||
write: publicAccess ? ['*'] : [orbitdb.identity.id],
|
||||
}
|
||||
})
|
||||
|
||||
await load(db, 'Creating database...')
|
||||
startWriter(db, interval)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
openButton.disabled = false
|
||||
createButton.disabled = false
|
||||
}
|
||||
|
||||
const openDatabase = async () => {
|
||||
const address = dbAddressField.value
|
||||
|
||||
await resetDatabase(db)
|
||||
|
||||
openButton.disabled = true
|
||||
createButton.disabled = true
|
||||
|
||||
try {
|
||||
statusElm.innerHTML = "Connecting to peers..."
|
||||
db = await orbitdb.open(address, { sync: true })
|
||||
await load(db, 'Loading database...')
|
||||
|
||||
if (!readonlyCheckbox.checked) {
|
||||
startWriter(db, interval)
|
||||
} else {
|
||||
writerText.innerHTML = `Listening for updates to the database...`
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
openButton.disabled = false
|
||||
createButton.disabled = false
|
||||
}
|
||||
|
||||
const update = async (db) => {
|
||||
count ++
|
||||
|
||||
const time = new Date().toISOString()
|
||||
const idx = Math.floor(Math.random() * creatures.length)
|
||||
const creature = creatures[idx]
|
||||
|
||||
if (db.type === 'eventlog') {
|
||||
const value = "GrEEtinGs from " + orbitdb.id + " " + creature + ": Hello #" + count + " (" + time + ")"
|
||||
await db.add(value)
|
||||
} else if (db.type === 'feed') {
|
||||
const value = "GrEEtinGs from " + orbitdb.id + " " + creature + ": Hello #" + count + " (" + time + ")"
|
||||
await db.add(value)
|
||||
} else if (db.type === 'docstore') {
|
||||
const value = { _id: 'peer1', avatar: creature, updated: time }
|
||||
await db.put(value)
|
||||
} else if (db.type === 'keyvalue') {
|
||||
await db.set('mykey', creature)
|
||||
} else if (db.type === 'counter') {
|
||||
await db.inc(1)
|
||||
} else {
|
||||
throw new Error("Unknown datatbase type: ", db.type)
|
||||
}
|
||||
}
|
||||
|
||||
const query = (db) => {
|
||||
if (db.type === 'eventlog')
|
||||
return db.iterator({ limit: 5 }).collect()
|
||||
else if (db.type === 'feed')
|
||||
return db.iterator({ limit: 5 }).collect()
|
||||
else if (db.type === 'docstore')
|
||||
return db.get('peer1')
|
||||
else if (db.type === 'keyvalue')
|
||||
return db.get('mykey')
|
||||
else if (db.type === 'counter')
|
||||
return db.value
|
||||
else
|
||||
throw new Error("Unknown datatbase type: ", db.type)
|
||||
}
|
||||
|
||||
const queryAndRender = async (db) => {
|
||||
const networkPeers = await ipfs.swarm.peers()
|
||||
const databasePeers = await ipfs.pubsub.peers(db.address.toString())
|
||||
|
||||
const result = query(db)
|
||||
|
||||
if (dbType !== db.type || dbAddress !== db.address) {
|
||||
dbType = db.type;
|
||||
dbAddress = db.address;
|
||||
|
||||
outputHeaderElm.innerHTML = `
|
||||
<h2>${dbType.toUpperCase()}</h2>
|
||||
<h3 id="remoteAddress">${dbAddress}</h3>
|
||||
<p>Copy this address and use the 'Open Remote Database' in another browser to replicate this database between peers.</p>
|
||||
`
|
||||
}
|
||||
|
||||
outputElm.innerHTML = `
|
||||
<div><b>Peer ID:</b> ${orbitdb.id}</div>
|
||||
<div><b>Peers (database/network):</b> ${databasePeers.length} / ${networkPeers.length}</div>
|
||||
<div><b>Oplog Size:</b> ${Math.max(db._replicationStatus.progress, db._oplog.length)} / ${db._replicationStatus.max}</div>
|
||||
<h2>Results</h2>
|
||||
<div id="results">
|
||||
<div>
|
||||
${result && Array.isArray(result) && result.length > 0 && db.type !== 'docstore' && db.type !== 'keyvalue'
|
||||
? result.slice().reverse().map((e) => e.payload.value).join('<br>\n')
|
||||
: db.type === 'docstore'
|
||||
? JSON.stringify(result, null, 2)
|
||||
: result ? result.toString().replace('"', '').replace('"', '') : result
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
`
|
||||
}
|
||||
|
||||
openButton.addEventListener('click', openDatabase)
|
||||
createButton.addEventListener('click', createDatabase)
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
|
||||
module.exports = main
|
@ -1,48 +0,0 @@
|
||||
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()
|
@ -1,67 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const IPFS = require('ipfs')
|
||||
const OrbitDB = require('../src/OrbitDB')
|
||||
|
||||
const userId = 1
|
||||
const creatures = ['🐙', '🐬', '🐋', '🐠', '🐡', '🦀', '🐢', '🐟', '🐳']
|
||||
|
||||
const output = (user) => {
|
||||
if (!user)
|
||||
return
|
||||
|
||||
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...")
|
||||
|
||||
async function main () {
|
||||
let db
|
||||
|
||||
try {
|
||||
const ipfs = await IPFS.create({
|
||||
repo: './orbitdb/examples/ipfs',
|
||||
start: true,
|
||||
EXPERIMENTAL: {
|
||||
pubsub: true,
|
||||
},
|
||||
})
|
||||
const orbitdb = await OrbitDB.createInstance(ipfs, {
|
||||
directory: './orbitdb/examples/keyvalue'
|
||||
})
|
||||
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)
|
||||
}
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user