Skip to content

Instantly share code, notes, and snippets.

@sergibondarenko
Created September 19, 2017 10:42
Show Gist options
  • Save sergibondarenko/2651ff3d269b68475205b0b6f555b651 to your computer and use it in GitHub Desktop.
Save sergibondarenko/2651ff3d269b68475205b0b6f555b651 to your computer and use it in GitHub Desktop.
const Gun = require('gun');
const gun = Gun('http://localhost:8080/gun');
const alice = gun.get('person/alice').put({name: 'alice', age: 22});
const bob = gun.get('person/bob').put({name: 'bob', age: 24});
const carl = gun.get('person/carl').put({name: 'carl', age: 16});
const dave = gun.get('person/dave').put({name: 'dave', age: 42});
alice.on(function(node){
console.log('Subscribed to Alice!', node);
});
console.log('==============', 'get Bob data');
gun.get('person/bob').val(function(node){
console.log('Bob!', node);
});
const people = gun.get('people');
people.set(alice);
people.set(bob);
people.set(carl);
people.set(dave);
console.log('==============', 'get people data');
people.map().val(function (person) {
console.log('The person is', person);
});
const company = gun.get('startup/hype').put({
name: "hype",
profitable: false,
address: {
street: "123 Hipster Lane",
city: "San Francisco",
state: "CA",
country: "USA"
}
});
console.log('==============');
company.val(function(startup){
console.log("The startup:", startup);
});
company.get('address').get('city').val(function(value, key){
console.log("What is the city?", value);
});
gun.get('startup/hype').put({ // or you could do `company.put({` instead.
funded: true,
address: {
street: "999 Expensive Boulevard"
}
});
const employees = company.get('employees');
employees.set(dave);
employees.set(alice);
employees.set(bob);
alice.get('spouse').put(bob);
bob.get('spouse').put(alice);
alice.get('spouse').get('employer').put(company);
alice.get('employer').put(company);
dave.get('kids').set(carl);
carl.get('dad').put(dave);
carl.get('friends').set(alice);
carl.get('friends').set(bob);
console.log('================', 'read related data');
gun.get('person/alice').get('spouse').get('employer').get('employees').map().get('name').val(function(data, key){
console.log("The employee's", key, data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment