A Weekend With GUN

Graph storage using gunDB

Saad Elbeleidy
A Weekend With
6 min readFeb 8, 2016

--

Looking for alternatives to Firebase, I stumbled upon gunDB (check it out at http://gun.js.org/). It does all sorts of cool stuff but to sum it up:

  • Key/Value, Document, Relational or Graph structure
  • Realtime
  • Decentralized
  • Offline first

To start messing around with gunDB you can use this jsbin as your gunDB playground. You can follow along by typing or copying each snippet into the JavaScript section and then running it to see your results in the console.

This post refers to gunDB version 0.3.4 which is the latest release at the time of this writing.

How it works

I like the graph aspect of gunDB so I’ll be covering how that works specifically but the same functions work regardless of what structure you go with.

After importing/requiring the gun library you do:

You’re good to go to test it out. Your data won’t actually be stored anywhere other than local storage in the browser right now, but check out how to set up peers, store to a file and connect to S3 here. I will just be covering how to interact with gun.

Here are the three key functions you’ll use when working with gunDB:

At the end of each call you end up in a context where you can make more calls. In the above example we got the object stored at ‘path/to/object’ then we went to its property ‘property’ and put an object there. This makes ‘path/to/object’ ‘s property ‘property’ equal to object.

Here’s an actual example:

“put” updates or adds properties to your specified context so this would result in the same outcome:

To see what’s in your current context you can use “val”

Now let’s create a relationship. I’m currently learning gunDB so let’s add gunDB into the DB and then connect it to me.

Now if I want to see what I’m learning I can just do:

Two issues arise here:

  1. What if I’m learning multiple things?
  2. What if I want to get to var saad from var gunDB?

Answering the first question: One To Many relationships

“put” creates a One to One, one way referencing relationship. This means that the properties of the object I’m putting go straight into the path that I selected (One to One) and that the object doesn’t have a way to get to the parent (one way referencing).

To get a One To Many relationship we use “set”:

“set” creates a One to Many, one way referencing relationship. This means that the properties of the object I’m putting go into the path that I selected as one element of a list(One to Many) and that the object doesn’t have a way to get to the parent (one way referencing).

Now that we have the relationship connected, you’re probably thinking, how do I check the value of each object in the entire set. Well instead of just using “val” we can use it in combination with “map”:

Answering the second question: Two way referencing relationships

A two way referencing relationship is just two one way referencing relationships. So, right now that’s just what we do. If we want to reference the parent we just go to the property we want to use in the child and add the parent reference.

So here are the 4 cases:

I can learn one project and the project can only be learned by one person (One to one)

I can learn many projects and each project can only be learned by one person (One to many)

I can learn one project and the project can be learned by multiple people (Many to one)

I can learn many projects and each project can be learned by multiple people (Many to many)

To summarize:

Now that we see the syntax for connecting things it makes sense to do a full example. Unfortunately this example doesn’t have any one to one relationships but it should still be beneficial to go through:

Blog

Here’s an example on how to structure a blog’s data structure using gunDB. We will start with 4 object types:

  • User
  • Post
  • Tag
  • Comment

Here’s how they are all connected:

  • User to Post — One To Many
    A user can have many posts but a post belongs to only one user.
  • User to Comment — One To Many
    A user can have many comments but a comment belongs to only one user.
  • Post to Tag — Many To Many
    A post can have many tags and a tag can be applied to many posts.
  • Post to Comment — One To Many
    A post can have many comments but a comment can only apply to one post.

Creating our users

Creating a post

The Post object:

Some tags

Lets store them!

Let’s connect them!
First, we connect the post to its author, then we connect all the tags:

Adding a comment

The comment & its connections

Checking it all works

If you want to check that everything worked fine, you can log some of the references’ values to check.

Having some fun

If you want to test this out yourself and maybe change a thing or two, check out the code here.

And there you go, a short intro to GUN’s gunDB. If you got this far, you probably want to start working with gun. You can do that on your project by simply running:

Enjoy!

P.S. Huge thanks to the awesome Mark Nadal and the folks over in the GUN gitter chat for being so helpful and putting up with all of my questions. Get in touch with them for any questions on working with gunDB.

I hope you enjoyed that and would appreciate your feedback. Let me know what you think!

--

--

Saad Elbeleidy
A Weekend With

Robot Teleoperation Interface Researcher interested in Machine Learning, Data Visualization, Algorithmic Bias, and Food