3. Reading / Writing Data

HAT Data

Response type:

interface HatRecord<T> {
  endpoint: string; // eg. locations
  recordId: string; // A unique record ID of the record.
  data: T; // The Data
}

Write Data

Parameters

Type

namespace

string

endpoint

string

body

object

Response

  • HatRecord\

Example:

const namespace = "testhatapp";
const endpoint = "locations";
const newLocation = {
    latitude: "51.01",
    longitude: "52.12"
};

try {
    const result = await hat.hatData().create(
        namespace, 
        endpoint, 
        newLocation
    );

    if (result.parsedBody) {
        // result.parsedBody contains a HatRecord with the new data.
    }
} catch (error) {
  // Failed to write data...
}

Update Data

Parameters

Type

body

Array>

Response:

  • Array>

Example:

const location = {
    endpoint: "testhatapp/locations",
    recordId: "c14b8f73-157f-40f1-9c77-985d9dea50d2",
    data: {
        latitude: "51.01",
        longitude: "52.12"
    }
};

location.data.latitude = "49.01";

const records = [location]; // Array of records to update. In this case we have only one.

try {
    const result = await hat.hatData().update(records);

    if (result.parsedBody) {
        // result.parsedBody contains an array of HatRecords<T> with the updated location data.
    }
} catch (error) {
  // Failed to update the records...
}

Delete Data

Parameters

Type

recordIds

Array

Response:

  • A successful response message

Example:

const recordIds = ["c14b8f73-157f-40f1-9c77-985d9dea50d2", "d52b8f73-151f-40f1-9c77-932d9dea5daa"];

try {
    const result = await hat.hatData().delete(recordIds);

    if (result.parsedBody) {
        // result.parsedBody contains a successful response message.
        // In this case the result will be {message: "All records deleted"}.
    }
} catch (error) {
  // Failed to delete records...
}

Read Data

The are 2 ways to fetch data:

  1. With default options.

Parameters

Type

namespace

string

endpoint

string

Response

  • Array>

Example:

const namespace = "testhatapp";
const endpoint = "locations";

try{
    const result = await hat.hatData().getAllDefault(namespace, endpoint);

    if (result.parsedBody) {
        // result.parsedBody contains an Array of HatRecord<Location>.
    }
} catch (error) {
    // Failed to fetch the data
}

2. With custom options.

Example:

const options = {
    ordering: "descending",
    orderBy: "lastUpdated"
};
const namespace = "testhatapp";
const endpoint = "locations";

try{
   const result = await hat.hatData().getAll(namespace, endpoint, options);

   if (result.parsedBody) {
       // result.parsedBody contains an Array of HatRecords<Location>, ordered by the last updated date.
   }
} catch (error) {
    // Failed to fetch the data
}

Last updated