Skip to content

Setting Data (v0.1.x)

Jesse Gibson edited this page Jan 26, 2016 · 5 revisions

v0.1.0 has been deprecated.


Setting Data

It's easiest to initially think about gun as a field/value store, where JavaScript objects can be stored and retrieved based on a particular key that identifies it. This key can be literally any string; they don't have to follow any particular pattern, although that may be useful for your own organization.

In this example, we'll look at the following objects:

// Cecil (Human)
{
    "name": "Cecil",
    "species": "human",
    "location": "Night Vale"
}

// Khoshekh (Cat)
{
    "name": "Khoshekh",
    "species": "cat"
}

Note: This tutorial assumes that you've already set up a connection to gun, and that that connection is represented by the variable gun. You can read more about getting started here.

To start off, we'll save Cecil to the key person/cecil

gun.set({
    "name": "Cecil",
    "species": "human",
    "location": "Night Vale"
}).key('person/cecil');

Thanks to the chaining that is built into GUN, we can really easily assign multiple keys to the same object, too. For example, if we wanted to access Cecil at both person/cecil and human/1, we could do that as follows

gun.set({
    "name": "Cecil",
    "species": "human",
    "location": "Night Vale"
}).key('person/cecil').key('human/1');

Now, both person/cecil and human/1 can be used to look up the same object.

Next, we want to establish that Khoshekh belongs to Cecil. We can do that by using set again to set a particular value of Cecil, which we will refer to as cat.

gun.load('person/cecil').path('cat').set({
    "name": "Khoshekh",
    "species": "cat"
});

In the above example, we tell gun to load the object at the person/cecil key (which we just set up earlier), then step into the object to the cat field, which we set to Khoshekh's object. We can also rely on gun's chaining once again to set a separate key that Khoshekh can be accessed at as well.

gun.load('person/cecil').path('cat').set({
    "name": "Khoshekh",
    "species": "cat"
}).key('cat/khoshekh');

For more information on getting data from GUN, check out Getting Data on the 0.1.x Table of Contents.

This wiki is where all the GUN website documentation comes from.

You can read it here or on the website, but the website has some special features like rendering some markdown extensions to create interactive coding tutorials.

Please feel free to improve the docs itself, we need contributions!

Clone this wiki locally