Databases
See a walk through of the code
Databases are covered in our data storage tutorial video.
Try it out
This example shows opening a database (in the openDatabase
function), saving data (the addItem
function) and finally how to delete all data (the deleteAll
function).
Live Editor
VeridaDatabaseExample = () => {const [veridaContext, setVeridaContext] = useState(undefined);// tableRows the the records shown from the databaseconst [tableRows, setTableRows] = useState([]);this.CONTEXT_NAME = "Verida Tutorial"React.useEffect(async () => {// See the login example for an explanation of thisif (!veridaContext) {if (hasSession(this.CONTEXT_NAME)) {// we know we have a session already// globalLoginFunction is a globally defined version of the login function// See https://developers.verida.io/docs/tutorial/SSOlet ctx = await globalLoginFunction(this.CONTEXT_NAME);setVeridaContext(ctx);}}}, []);generateTableRows = (items) => {// We render all available fields here// the "myvalue" column is the one we set in the addItem function belowlet rows = items.map((item) => (<tr key={item._id}><td>{item._id}</td><td>{item.myvalue}</td><td>{item.insertedAt}</td><td>{item.modifiedAt}</td><td>{item._rev}</td></tr>))setTableRows(rows)}addItem = async function () {if (Window.db === undefined) {await openDatabase();}// save a record with key "hello", value "world"//// Try editing the value saved hereconst item = await Window.db.save({myvalue: "hello world", // change this, and press the "Add Item" button})// retrieve the items with no filter and no optionsconst items = await Window.db.getMany(null, null);generateTableRows(items)}openDatabase = async function () {// veridaContext will be undefined if the user isn't logged inif (veridaContext !== undefined) {// Open / create a test databaseWindow.db = await veridaContext.openDatabase("test_db");// call getMany with no filter and no optionsconst items = await Window.db.getMany(null, null);generateTableRows(items);return true} else {// This method shouldn't be called if no one is logged inconsole.log("veridaContext is null. This means no one has logged in")return false;}}deleteAll = async function () {if (Window.db === undefined) {await openDatabase();}// delete all items from this databaseawait Window.db.deleteAll();// display the empty tablegenerateTableRows([]);}useEffect(() => {if (Window.db === undefined) {openDatabase();}}, [veridaContext])// if we have a veridaContext we are logged inif (veridaContext) {// user is logged inreturn (<div><h3>Logged in as DID: {veridaContext.account.accountDid}</h3><h4>Current data in database</h4><table><thead><tr><td>_id</td><td>myvalue</td><td>insertedAt</td><td>modifiedAt</td><td>_rev</td></tr></thead><tbody>{tableRows}</tbody></table><h4>Actions</h4><button onClick={this.addItem}>Add Item</button><br /><button onClick={this.deleteAll}>Delete All Items</button></div>)} else {return (<div className="admonition admonition-danger alert alert--danger"><h2>Please login <a href="/docs/tutorial/SSO#runcode">here</a></h2></div>)}}
Result
Loading...
Database documentation
See the Verida Database documentation for complete documentation.