Query and Read records on a DWN

Chris Siku - Oct 11 - - Dev Community

With records created, it’s essential to know how to access them. In this chapter, we will explore the methods for querying and reading records from your DWN. You’ll learn how to formulate queries, handle responses, and parse data efficiently using JavaScript.

This chapter will equip you with the techniques needed to retrieve and display information from your Decentralized Web Node, ensuring you can make the most of the data stored within.

Query Records

You can use the query function to obtain records and from a DWN of a specific person

as we still proceeding with our previous code, let query our DWN that contains our mixed record

// index.js
// ....

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);
});

Enter fullscreen mode Exit fullscreen mode

if your run the project at this point you'll get the response of your query what is an object containing all your record on your DWN and you'll realize that there is a new key recordId: record.id which will help us perform operations like read, update and delete a specific record,

Read Record

You can use the read function to obtain records and from a DWN. here is how :

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

console.log(await singleRecord.data.json());
Enter fullscreen mode Exit fullscreen mode

the result in the console should be as follow

{
  image: { currentTarget: { files: [Array] } },
  file: { currentTarget: { files: [Array] } },
  text: 'Mixed data 2'
}
Enter fullscreen mode Exit fullscreen mode

Okay, if you have been following allong, you code should look as the following :

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 2");


// 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());

Enter fullscreen mode Exit fullscreen mode

Good job so far

Next ⏩️ ⏩️ ⏩️ Update and delete records on a DWN

Prev ⏪️ ⏪️ ⏪️ Create a record

. . . . . . .
Terabox Video Player