Skip to content

Instantly share code, notes, and snippets.

@brishtiteveja
Created February 18, 2021 15:10
Show Gist options
  • Save brishtiteveja/68612dc45b18658a9143c4c586354593 to your computer and use it in GitHub Desktop.
Save brishtiteveja/68612dc45b18658a9143c4c586354593 to your computer and use it in GitHub Desktop.
orbit db example keyvalue for instaperience instalon
'use strict'
const IPFS = require('ipfs')
const OrbitDB = require('../src/OrbitDB')
const postAttrs = ["postId", "description", "likes", "location", "mediaUrl", "mediaIPFSHash", "ownerId", "timestamp", "username"]
const postDump = {}
postDump["postId"] = ['1jdyU1aFlaD90Er1h0KL' , '27nyFXnnK02P4I3GYdjK', '']
postDump["description"] = ['View from Saxony Lake Beach.' , 'hi']
postDump["likes"] = [100, 10]
postDump["location"] = ["Indianapolis", ""]
postDump["mediaUrl"] = ["https://firebasestorage.googleapis.com/v0/b/fluttergram-firebase-functions.appspot.com/o/post_17150d30-e0a6-11ea-ff42-dbb35afd88e3.jpg?alt=media&token=c96bfabe-de67-4e11-b942-1f05024b7814", "https://firebasestorage.googleapis.com/v0/b/fluttergram-firebase-functions.appspot.com/o/post_2c7107b0-fb72-11ea-b202-0b1fc1702b01.jpg?alt=media&token=c55a370f-194c-4fe2-ad98-5aaf197ee489"]
postDump["mediaIPFSHash"] = ["", ""]
postDump["ownerId"] = ["110942459497126507979", "105243852936973320992"]
postDump["timestamp"] = ["", ""]
postDump["username"] = ["brishtiteveja", "data_tells_me_a_story"]
const output = (post) => {
if (!post)
return
let output = ``
output += `----------------------\n`
output += `Post\n`
output += `----------------------\n`
for(var a = 0; a < postAttrs.length; a++) {
const k = postAttrs[a]
output += k + `: ${post[k]}\n`
}
output += `Updated: ${post.updated}\n`
output += `----------------------\n`
console.log(output)
}
console.log("Starting...")
async function main () {
let db
try {
const ipfs = await IPFS.create({
repo: './orbitdb/instaperience/ipfs',
start: true,
EXPERIMENTAL: {
pubsub: true,
},
})
const orbitdb = await OrbitDB.createInstance(ipfs, {
directory: './orbitdb/instaperience/keyvalue'
})
db = await orbitdb.kvstore('insta_posts', { overwrite: true })
await db.load
// Query immediately after loading
//const post = db.get(postDump["postId"][0])
//output(post)
} catch (e) {
console.log("here")
console.error(e)
process.exit(1)
}
const createPost = (index) => {
var post = {}
for(var a = 0; a < postAttrs.length; a++) {
var k = postAttrs[a]
if(k) {
console.log(k)
post[k] = postDump[k][index]
post["updated"] = new Date().getTime()
}
}
return post
}
const query = async () => {
// Randomly select an avatar
const index = Math.floor(Math.random() * postDump["postId"].length)
console.log("index = " + index)
// Set the key to the newly selected avatar and update the timestamp
const post = createPost(index)
await db.put(postDump["postId"][index], post)
// Get the value of the key
const cpost = db.get(postDump["postId"][index])
// Display the value
output(cpost)
}
console.log("Starting update loop...")
setInterval(query, 1000)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment