Skip to content

Instantly share code, notes, and snippets.

@sbeleidy
Last active February 14, 2016 09:41
Show Gist options
  • Save sbeleidy/cfa017216f2668090448 to your computer and use it in GitHub Desktop.
Save sbeleidy/cfa017216f2668090448 to your computer and use it in GitHub Desktop.
gun 0.3.4 intro
// View the walkthrough at https://medium.com/@sbeleidy/a-weekend-with-gun-a61fdcb8cc5d
localStorage.clear();
var gun = Gun();
var markInfo = {
name: "Mark",
username: "@amark"
};
var jesseInfo = {
name: "Jesse",
username: "@PsychoLlama"
};
var mark = gun.get('user/'+markInfo.username).put(markInfo);
var jesse = gun.get('user/'+jesseInfo.username).put(jesseInfo);
var lipsum = {
title: "Lorem ipsum dolor",
slug: "lorem-ipsum-dolor",
content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
};
var lorem = {
name: "lorem",
slug: "lorem"
};
var ipsum = {
name: "ipsum",
slug: "ipsum"
};
var dolor = {
name: "dolor",
slug: "dolor"
};
var lipsumPost = gun.get('post/'+lipsum.slug).put(lipsum);
var loremTag = gun.get('tag/'+lorem.slug).put(lorem);
var ipsumTag = gun.get('tag/'+ipsum.slug).put(ipsum);
var dolorTag = gun.get('tag/'+dolor.slug).put(dolor);
lipsumPost.path('author').put(mark).path('posts').set(lipsumPost)
.path('tags').set(loremTag).path('posts').set(lipsumPost)
.path('tags').set(ipsumTag).path('posts').set(lipsumPost)
.path('tags').set(dolorTag).path('posts').set(lipsumPost);
var sit = {
id: 1,
text: "Sit amet, consectetur adipiscing elit."
};
var sitComment = gun.get('comment/'+sit.id).put(sit);
sitComment.path('author').put(jesse).path('comments').set(sitComment)
.path('post').put(lipsumPost).path('comments').set(sitComment);
// Try these out separately so you can see each result
mark.val(function(v){console.log(v);});
mark.path('posts').map().val(function(v){console.log(v);});
jesse.val(function(v){console.log(v);});
jesse.path('comments').map().val(function(v){console.log(v);});
lipsumPost.path('comments').map().val(function(v){console.log(v);});
lipsumPost.path('author').val(function(v){console.log(v);});
lipsumPost.path('tags').map().val(function(v){console.log(v);});
loremTag.path('posts').map().val(function(v){console.log(v);});
sitComment.path('author').val(function(v){console.log(v);});
sitComment.path('post').val(function(v){console.log(v);});
mark.path('posts').map() // All of mark's posts
.path('tags').map() // All of mark's posts' tags
.path('posts').map() // All of the tags' posts
.path('comments').map() // All of the posts' comments
.path('author').val(function(v){console.log(v);}); // All of the comments' authors
// Based on our inserted dataset this would return jesse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment