Skip to content

Instantly share code, notes, and snippets.

@sloev
Last active September 15, 2023 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloev/a29e928f8f7a79014bb3a800ec125408 to your computer and use it in GitHub Desktop.
Save sloev/a29e928f8f7a79014bb3a800ec125408 to your computer and use it in GitHub Desktop.
index.js
(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