I've been a DevOps engineer my whole career and I decided to try programming on Hive as it is great motivation to support a blockchain I'm invested in, like contributing writing to, and helping others build reputation on the internet for Hive. All that makes it fun.
So it's quite easy to program in Typescript/Express/Node and it took me only 2 days to refresh myself to be able to submit block IDs to a MongoDB database.
I hope this code helps anyone who is curious for developing apps on Hive. This was my first attempt at executing an Obituary frontend and CUSTOM_JSON interface which seems to be quite feasible with dhive.
I recommend the following post by @gtg for exploring what would work best for your app. The dhive library is fantastic. Their docs are linked below:
https://hive.blog/hive-102930/@gtg/a-curated-list-of-awesome-hive-resources
https://openhive-network.github.io/dhive/
Here is a working example of submitting every block to a MongoDB database. I am a novice, so any critiques would be great.
import { Client } from "@hiveio/dhive";
const client = new Client(["https://api.pharesim.me", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);
// Use connect method to connect to the server
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://27017';
// Database Name
const dbName = 'hive';
async function getHiveBlocks() {
for await (const block of client.blockchain.getBlocks()) {
return "id:" + `${block.block_id}`;
}
}
function MongoConnection(){
MongoClient.connect(url, function(err, MongoBomb) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = MongoBomb.db(dbName);
function findDocuments(db){
// Get the documents collection
const collection = db.collection('blocks');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs)
MongoBomb.close();
});
}
function insertHiveBlocks(db) {
console.log("test");
// Get the documents collection
const collection = db.collection('blocks');
// Insert some document
collection.insertOne(getHiveBlocks()), function(err, result) {
try{
console.log("MADE IT")
assert.equal(err, null);
assert.equal(1, result.result.n);
assert.equal(1, result.ops.length);
console.log("Inserted 1 documents into the collection");
MongoBomb.close();
} catch {
console.log("POOP");
}
}
}
insertHiveBlocks(db);
findDocuments(db);
MongoBomb.close();
})
}
MongoConnection();
HIVE.D!
For the sake of @patrickulrich @customcog @frankscoms @pennywisedawise and of course @fulltimegeek
I love MongoDB.
Does that return an array of blocks? If so, what's the size of it.
So then, I guess that it's an array of size 1? (probably most recent block)
It then returns the id of that block. My guess is that the Javascript Devs made the getBlocks() function return an array for future flexibility. I'm still a novice so I might be reading the javascript wrong ...
Thank You SOOO Much : )
That's pretty cool to know.
This is for a non dev like me abracadabra
Congratulations @utopiaeducators! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :
You can view your badges on your board And compare to others on the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Do not miss the last post from @hivebuzz:
Upvoted by GITPLAIT!
We have a curation trial on Hive.vote. you can earn a passive income by delegating to @gitplait
We share 80 % of the curation rewards with the delegators.
To delegate, use the links or adjust 10HIVE, 20HIVE, 50HIVE, 100HIVE, 200HIVE, 500HIVE, 1,000HIVE, 10,000HIVE, 100,000HIVE
Join the Community and chat with us on Discord let’s solve problems & build together.
Warning! This user is on our black list, likely as a known plagiarist, spammer or ID thief. Please be cautious with this post!
If you believe this is an error, please chat with us in the #appeals channel in our discord.
1st order of business is to remove this operator!
Phishing scam to Discord. The RCMP have been notified.