DEV Community

Cover image for Update and delete records on a DWN
Chris Siku
Chris Siku

Posted on

Update and delete records on a DWN

Data management isn't just about creating records; it also involves updating and deleting them as needed.

In this chapter, we’ll cover how to update existing records and delete unnecessary ones on your Decentralized Web Node (DWN). You'll learn the appropriate methods and commands to execute these actions safely and effectively, along with best practices for maintaining data consistency and integrity.

By the end of this chapter, you'll be able to manage your DWN's data lifecycle seamlessly.

Updating a Record

In previous examples, we’ve covered how to create records on a DWN and query them to read one by one. Now, to update a record, you can use the update function.

Here’s how to update a record:

// index.js
// ...

// Read a single record
const { record: singleRecord } = await web5.dwn.records.read({
  message: {
    filter: {
      recordId: "bafyreiflqo2weyflwxn3lzkihopjtaeshbf4drmuf4yhmx6q3zeofv5bh4",
    },
  },
});

// Update the record
const { status } = await singleRecord.update({
  data: {
    file: mockTxt,   // Example text file
    image: mockImage, // Example image file
    text: "Updated mixed data", // New data content
  },
});

console.log(await singleRecord.data.json());  // Print updated record data
Enter fullscreen mode Exit fullscreen mode

Deleting a Record

To delete a record from your DWN, use the delete function:

// index.js
// ...

// Delete the record
const { status: deleteStatus } = await singleRecord.delete();
Enter fullscreen mode Exit fullscreen mode

And that's it! You’ve successfully performed all the CRUD (Create, Read, Update, Delete) operations on your DWN.

Complete CRUD Example Code

Here’s a complete example showing how to connect to a DWN, create a mixed data record, query it, update, and delete it:

import { Web5 } from "@web5/api";

const { web5, did } = await Web5.connect({
  didCreateOptions: {
    dwnEndpoints: ["https://dwn.gcda.xyz"], // Community DWN instance on Google Cloud
  },
  registration: {
    onSuccess: () => {
      console.log("\n\nConnected successfully");
    },
    onFailure: (error) => {
      console.error("Connection failed:", error);
    },
  },
});

console.log("\nConnected did:", did);

// Image file
const imageFile = new File(["test image content"], "web5.png", {
  type: "image/png",
});
const mockImage = {
  currentTarget: {
    files: [imageFile], // Simulate a file input
  },
};

// txt file
const txtFile = new File(["dummy content"], "web5.txt", {
  type: "text/plain",
});

const mockTxt = {
  currentTarget: {
    files: [txtFile], // Mimic the file input's files array
  },
};

// Create Mixed record

const createMixedRecord = async (file, image, text) => {
  const { status, record } = await web5.dwn.records.create({
    data: {
      image,
      file,
      text,
    },
    message: {
      schema: "https://test.org/schema/mixed-data",
      dataFormat: "application/json",
    },
  });

  return record;
};

createMixedRecord(mockImage, mockTxt, "Mixed data");

// Query records
const response = await web5.dwn.records.query({
  form: did,
  message: {
    filter: {
      schema: "https://test.org/schema/mixed-data",
      dataFormat: "application/json",
    },
  },
});

response.records.forEach(async (record) => {
  const result = await record.data.json();
  const allRecords = { recordId: record.id, data: result };
  console.log(allRecords);
});

// read record
const { record: singleRecord } = await web5.dwn.records.read({
  message: {
    filter: {
      recordId: "bafyreig4vibz5lg4otmftd56qzqisgga7cqw5yeob336z7nn2x3hyv55ha",
    },
  },
});

console.log(await singleRecord.data.json());

// update a record

const { status } = await singleRecord.update({
  data: {
    file: mockTxt,
    image: mockImage,
    text: "Updated mixed data",
  },
});

console.log(await singleRecord.data.json());

// delete Record
const { status: deleteStatus } = await singleRecord.delete();

Enter fullscreen mode Exit fullscreen mode

Protocols: The Data Exchange Contracts

Finally, remember that Protocols define the data schema and serve as the contract by which two Decentralized Web Nodes (DWNs) agree to communicate and share data.

Image description

Stay tuned for our upcoming post on protocols and how to synchronize data between two DWNs!

Prev ⏪️ ⏪️ ⏪️ Query and Read record

Top comments (0)