Decentralized Database Mastery: A Unified Guide from Basics to Advanced with gunDB

Gustav Corpas
21 min readJan 7, 2024

TLDR;

This introduces the reader to gun by starting with a super basic example and then making that same example increasingly more complex. We’ll cover certificates, database design principles, iteration, awaiting values and much more.

// We start here
const alice = gun.get("members").get("alice");
alice.get("name").put("alice");

// We end here
const user_name = await User.name.chain;
Group.members.iterate(member => {
member.name.chain.put(user_name, certificate);
}

TIP: If you have read this article before or are familiar with gun, there is a section titled Putting it all together at the end that encapsulates all the concepts of the article…

Introduction

2024 just started (well.. when I began this article it did :), and that made me reflect a bit on what I did in 2023 as a developer. The most impactful thing for me, was learning about gunDB. It is a local, offline-first database that makes it so amazingly easy to create peer to peer applications.

For me, working with and setting up databases has always been this somewhat unpleasant development experience, especially if you just need something that’s quick and dirty — for a small side project.

If you find that setting up an entire backend system just to handle a few database calls is cumbersome, or perhaps you are just starting out with programming and this seems daunting!

Well, if I may, I would like to get you excited about gun in one short code snippet…

The following is a fully working database, that will store a value locally on the device. It will then synchronize that value with all other devices that are listening to it.

 <html>
<head>
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
</head>

<body>
<button onclick="say('hi')">say hi</button>
<button onclick="say('bye')">say bye</button>
</body>

<script>
const gun = Gun(["https://gun-manhattan.herokuapp.com/gun"]);

const items = gun.get("test").get("items");

function say(input){…

--

--

Gustav Corpas

I write about technology and society. Or anything that is interesting to me.