Last active
September 15, 2023 21:13
-
-
Save sloev/a29e928f8f7a79014bb3a800ec125408 to your computer and use it in GitHub Desktop.
index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const GUN = require('gun'); | |
const dbName = "database" + new Date() | |
require('gun/lib/path.js') | |
var SEA = GUN.SEA; | |
var gun = GUN() | |
// 1...... | |
// create server certificate for database (allowing server to delete all user objects) | |
var ServerAdmin = await SEA.pair() | |
var certificate = await SEA.certify("*", [{ "#": { "*": dbName } }], ServerAdmin, null) | |
// Authenticate with the ServerAdmin pair | |
gun.user().auth(ServerAdmin, () => { | |
// Put the certificate into the ServerAdmin graph for ease of later use | |
gun.user() | |
.get('certs') | |
.get(dbName) | |
.put(certificate) | |
}) | |
// 2...... | |
// create new user, use cert to store user in database | |
var certificate = await gun.user(ServerAdmin.pub).get('certs').get(dbName).then() | |
var bobSea = await SEA.pair() | |
await gun.user().auth(bobSea) | |
var bobInfo = { | |
name: "bob", | |
username: "bob@bob.dk" | |
}; | |
var bob = gun.user(ServerAdmin.pub).get(dbName).get('users/'+bobInfo.username).put(bobInfo, null, {opt: {cert: certificate }}) | |
var heapInfo = { | |
image: "foo", | |
decription: "", | |
secret_hash_for_delete: "" | |
} | |
var heap = gun.user(ServerAdmin.pub).get(dbName).get('heaps').set(heapInfo,null, {opt: {cert: certificate }}) | |
console.log("heap:", heap) | |
heap.path("author").put(bob, null, {opt: {cert: certificate }}).path("heaps").set(heap, null, {opt: {cert: certificate }}) | |
gun.user(ServerAdmin.pub).get(dbName).get('heaps').map().once(function(data, key){ | |
console.log("Item:", data); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment