Confused about keys in gun DB

478 Views Asked by At
var stallone = {stallone:{first:'Sylvester',last:'Stallone',gender:'male'}};
var gibson = {gibson:{first:'Mel',last:'Gibson',gender:'male'}};
var movies = gun.get('movies')
movies.put(stallone).key('movies/action').key('movies/actors').key('movies/action/rambo')   movies.put(gibson).key('movies/action').key('movies/actors').key('movies/action/roadwarrior').key('movies/comedy');

movies.get('movies/action').val();
returns {_: Object, stallone: Object, gibson: Object} Nice.

movies.get('movies/comedy').val();
returns {_: Object, stallone: Object, gibson: Object} Erm..What is Sly doing here? Not Nice!!

gun.get('movies/comedy').val();
returns {_: Object, stallone: Object, gibson: Object} same thing!!

This behaviour leads to a couple of questions:
1) why bother creating movies ? I'm working with var movies = gun.get('movies') so why do i have to create the key with 'movies' in it again? 'movies' should be prefixed automaticly.
2) Even if the multiple keys would work it's not very intuitive. It would be nice if we could just do movies.put(gibson).keys(['actors','comedy','action']).

Note: i would be happy if could be done tru a loop. but that doesn't work eather

var gibsonKeys = ['actors','action','comedy','dieHard']
gibsonKeys.forEach(function(key){
    movies.put(gibson).key('movies/'+key);
  // could be gun.put(gibson).key('movies/'+ key) as well
});

As a sidenote...I know that the keys are just strings and not real paths to the data ;)

1

There are 1 best solutions below

0
On

Answered by Mark Nadal

A couple things to note:

movies.put(data).key('foo/bar')

is putting data on movies and keying movies with 'foo/bar'. It is an update operation, not an insert operation. So what is returned from the put is the same context (movies), not some sub-document (you could access that sub document with movies.put(data).path('stallone') for example). If you are wanting to insert a record, kinda like having a table, try using .set - check out this article: https://github.com/amark/gun/wiki/graphs which goes over some examples of various data types.
Actually, for .set this is probably better: https://medium.com/@sbeleidy/a-weekend-with-gun-a61fdcb8cc5d#.49nuy86gs
Keys are different than tags, it also looks like you are probably wanting something like this: https://github.com/PsychoLlama/labelmaker.
Keys are as in key/value you can have multiple keys on something but they all point to the same thing. The above module gives you tags, which allows you to take multiple different things and tag them all with the same tag. Underneath the hood the way this is done is that it creates a set (see above, think of it as an unordered list) which the the tag is keyed to, and then you're able to iterate over all the multiple different items that are in that list. Does that make sense? Allowing key to accept multiple keys though is probably still a good idea though

However, the behavior above is correct for key, but it seems like what you want to use is a tag like system which you can add to GUN with the above module.