Building a tree structure with Gun

189 views Asked by At

I am trying to implement, through Gun, a tree structure that allows to express relationships between companies, subsidiaries of these companies and their products. The structure that I have done is as follows:

  • there is a root node: this is the main seller of some product (for example, sensors) and all its children (depth nodes 1) are its customers;
  • there are children nodes, not leaves, from level depth 1 on: these are the customers of the root knot; they can have different branches; there can be sub-sub-sub-branches of a branch and so on, there are no limits to the number of sub-subbranches;
  • there are leaf knots: these are the products contained in a branch, a sub-branch, a sub-sub-branch...

Here's how I thought of this:

  • create a client set;
  • put clients nodes inside client set;
  • create a branch set;
  • put branches nodes inside branch set;
  • link branches set to client set;
  • create a sensor set;
  • put sensors nodes inside sensor set;
  • link sensors set to branches set.

Somethink like this:

           [clients]
              /\
             /  \
    (MecDoland) (FrankMirros)
         |            |
     [branches]   [branches]
     /   |   \        |
  (NY) (CA)  (DA)    ...
   |     |     |
   | [sensors]...
   |     |
   |    ...
[sensors]
  /|\
......

Here's my implementation

const Gun = require('gun')
const gun = new Gun()

// Just a util function
function printError(ack) {
    if (ack.err) {
        console.log(ack.err)
    }
}

// [clients]
const clients = gun.get('clients')   // group
// (MecDoland)
const mac = clients.get('MecDoland').put({ data: 12 })
// (FrankMirros)
const pm = clients.get('FrankMirros').put({ data: 13 })
console.log('Adding to "clients" set MecDoland...')
// [clients] -> (MecDoland)
clients.set(mac, printError)
console.log('Adding to "clients" set philip morris...')
// [clients] -> (FrankMirros)
clients.set(pm, printError)

// [branches]
const branches = gun.get('branches') // group
// (MecDolandNY)
const macny = gun.get('MacDonaldNY').put({ data: 1 }, printError)
// (MecDolandCA)
const macca = gun.get('MecDolandCA').put({ data: 2 }, printError)
// [branches] -> (MecDolandNY)
branches.set(macny, printError)
// [branches] -> (MecDolandCA)
branches.set(macca, printError)
// [MecDoland] -> [branches]
mac.set(branches, printError)

//clients.map().once(v => console.log('CLIENT:',v))
//branches.map().once(v => console.log('BRANCH:',v._))

const sensors = gun.get('sensorsMecDolandNY') // group
const temp = gun.get('temperatue').put({ measure: 'celsius', value: 37 })
const press = gun.get('pressure').put({ measure: 'Bar', value: 2 })
sensors.set(temp)
sensors.set(press)
macny.set(sensors)
// Show all clients
gun
    .get('clients')
    .map()
    .once(v => console.log(v))

There are two points that I haven't been able to clarify:

1 - I don't know if I am using Gun sets well: I tried to add some relationships but didn't work out; consider the following instructions:

macDoland.get('client').put(rootNode)
frankMirrors.get('client').put(rootNode)

I tried to put a client relationship for both customer nodes, but printing rootNode showed me that rootNode had client relationship only with frankMirrors node! The first relationship with macDoland node disappeared so I started using Gun sets. My code works (more or less) but I don't know if I am using Gun set properly and if this is the right way to do it, given the kind of relationship I want to build between main vendors, customers, customers' branches and sensors in each branch.

2 - I'd like to make this approach more generic and scalable: for now there are clients, branches and sensors; this may change in future and the number of levels between clients and sensors may increase. This means that there may be an arbitrary number of levels between client nodes (depth 1) and sensors (max depth): for example, a very small client may be without branches and thus its client node smallCompany would directly be linked to its sensors; for another example, a gigantic client may have (say) five level of depths and I don't want to hard-code Gun set names such as macDolandNySubSubSubSubBranch. How could I provide scalability for an arbitrary number of sub-branches?

0

There are 0 answers