Skip to content

Instantly share code, notes, and snippets.

@jussiry
Created March 23, 2020 12:40
Show Gist options
  • Save jussiry/eb33c7542ca3383b68b6885383f8ede5 to your computer and use it in GitHub Desktop.
Save jussiry/eb33c7542ca3383b68b6885383f8ede5 to your computer and use it in GitHub Desktop.
Examples on using GUN js
var Gun = require('gun');
var gun = Gun('https://gun-eu.herokuapp.com/gun'); // connect to gun-eu peer (UI prototype also connects to this peer)
// Photos are stored in root -> starling -> photos -path
var photosPath = gun.get('starling').get('photos');
// UNCOMMENT next line to see results, explanation below:
//photosPath.once((res) => console.log(res))
/*
.once retrieves data from local db or or retieves it once from remote db.
Object in photosPath has:
- Undersoce value (_) in photos path includes metadata (vector clocks etc) and is handled by GUN
- Lots of null's:
In GUN data can't be totally deleted, since other peers might still retain it.
By setting the reference null we still remeber it has been "deleted" and won't get accidentally repopulated when connecting to peers that haven't removed the reference
- Photo-references (#: "some id, a.k.a 'soul'"
*/
// To see contents of each photo, we need retrieve the data that the references point to:
// UNCOMMENT:
// photosPath
// .map() // map iterates through every child
// .on(photo => { // .on runs callback on every change or addition to the node
// photo && console.log(photo)
// })
// Uncomment to add a photo using .set
// .set creates a new item and sets a "soul" (GUN lingo for id) reference from photosPath to that item
// photosPath.set({
// title: "A boy with a bicycle",
// url: "https://s3-ap-southeast-1.amazonaws.com/seeoutlook/wp-content/uploads/2018/01/22080922/City-View.jpg",
// nested: { objects: { automatically: 'creates another nodes and adds a references to them' } }
// });
// Alternative .put can be used to mofify a property in a node or if the user wants to set reference id's manually
// See Gun docs if you need to use .put https://gun.eco/docs/API#put
// Path is traced by chaining get's and .map splits path gets to all of the childs
// gun.get('starling').get('photos').map().get('nested').get('objects').once(res => console.log('nested object', res))
// Nodes can also be loaded from root by using their souls:
// gun.get('k84fmc23xaOrxdI2fiHr').once(res => console.log('RES', res))
// Useful method for resetting (nullifying) data that don't match :
function removeIllegalPhotos(requiredProps=['url']) {
photosPath.map().once((photo) => { // map iterates through children of photosPath; once executes callback only once per child
var soul = Gun.node.soul(photo) // "soul" == id of photo
if (!soul) return // photosPath may have also nulls or non-reference values (strings etc.)
requiredProps.forEach(propName => {
if (typeof photo[propName] === 'undefined') {
photosPath.get(soul).put(null)
return
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment