Last active
April 26, 2023 20:41
-
-
Save frankhinek/1c2b4fb503c838de23557055061115d9 to your computer and use it in GitHub Desktop.
Web5 CRUD Example
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Web5 CRUD Example</title> | |
<script src="https://unpkg.com/@tbd54566975/web5"></script> | |
</head> | |
<body> | |
nothing to see here | |
</body> | |
<script type="module"> | |
import { Web5 } from 'https://unpkg.com/@tbd54566975/web5@0.6.0/dist/browser.mjs'; | |
let web5 = new Web5; | |
let alice = await web5.did.create('ion'); | |
await web5.did.manager.set(alice.id, { | |
connected: true, | |
endpoint: 'app://dwn', | |
keys: { | |
['#dwn']: { | |
keyPair: alice.keys.find(key => key.id === 'dwn').keyPair, | |
}, | |
}, | |
}); | |
/**********************************************************************************************************************/ | |
let { record: recordJson, status: statusJson } = await web5.dwn.records.create(alice.id, { | |
author: alice.id, | |
data: { hello: 'world' }, | |
message: { | |
dataFormat: 'application/json', | |
}, | |
}); | |
console.log(status); | |
console.log(recordJson.toJSON()); | |
console.log('Record data in JSON format:', await recordJson.data.json()) | |
/**********************************************************************************************************************/ | |
let { record: recordText, status: statusText } = await web5.dwn.records.createFrom(alice.id, { | |
author: alice.id, | |
data: 'Hello, world', | |
message: { | |
dataFormat: 'text/plain', | |
}, | |
record: recordJson, | |
}); | |
console.log('Record data in text format:', await recordText.data.text()) | |
/**********************************************************************************************************************/ | |
console.log('Before:', recordJson.published); | |
await recordJson.update({ published: true }); | |
console.log('After:', recordJson.published); | |
/**********************************************************************************************************************/ | |
let response = await recordJson.delete(); | |
console.log(response.status); | |
/**********************************************************************************************************************/ | |
// Attempt to update the record to show what happens | |
await recordJson.update({ published: true }) | |
.then(response => response) | |
.catch(error => alert(error.message)); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment