Skip to main content

Custom Data

Custom data management in the @espressif/rainmaker-base-sdk allows you to store, retrieve, and manage custom user data with fine-grained permissions. You can also manage geographical coordinates and time zone information as part of custom data.

note

The userInstance mentioned below refers to the ESPRMUser instance obtained during User Sign in.

Custom Data Operations

Fetch Custom Data

Retrieve custom data using the getCustomData method.

try {
const customData = await userInstance.getCustomData();
console.log("Custom data fetched successfully:", customData);
} catch (error) {
console.error("Error fetching custom data:", error);
}

Set Custom Data

Set custom data using the setCustomData method.

const customDataRequest = {
name: {
value: "jack",
perms: [{ read: ["user", "admin"], write: ["user"] }],
},
};

try {
const response = await userInstance.setCustomData(customDataRequest);
console.log("Custom data set successfully:", response);
} catch (error) {
console.error("Error setting custom data:", error);
}

Delete Custom Data

Delete Value

Remove the value of a specific key using deleteCustomData.

try {
const response = await userInstance.deleteCustomData("exampleKey");
console.log("Custom data value deleted successfully:", response);
} catch (error) {
console.error("Error deleting custom data value:", error);
}

Delete Key

Delete an entire custom data entry using deleteCustomDataKey.

try {
const response = await userInstance.deleteCustomDataKey("exampleKey");
console.log("Custom data key deleted successfully:", response);
} catch (error) {
console.error("Error deleting custom data key:", error);
}

Delete Permissions

Remove permissions for a specific key using deleteCustomDataPermissions.

try {
const response = await userInstance.deleteCustomDataPermissions("exampleKey");
console.log("Custom data permissions deleted successfully:", response);
} catch (error) {
console.error("Error deleting custom data permissions:", error);
}

Geo Coordinates Management

Set Geo Coordinates

Store the user's geographical coordinates in the custom data using the setGeoCoordinates method.

/*
- geoCoordinates: An object containing the latitude and longitude to be stored.
*/

try {
const geoCoordinates = {
latitude: 28.6139,
longitude: 77.2090,
};
const response = await userInstance.setGeoCoordinates(geoCoordinates);
console.log("Geo coordinates set successfully:", response);
} catch (error) {
console.error("Error setting geo coordinates:", error);
}

Get Geo Coordinates

Retrieve the user's geographical coordinates from the custom data using the getGeoCoordinates method.

try {
const geoCoordinates = await userInstance.getGeoCoordinates();
console.log("Geo coordinates fetched successfully:", geoCoordinates);
} catch (error) {
console.error("Error fetching geo coordinates:", error);
}

Time Zone Management

The time zone information is stored in the user's custom data. If a time zone is set, it will be automatically used during the device provisioning process to configure the device with the same time zone.

Set Time Zone

Set the user's time zone using the setTimeZone method. This time zone is stored in the user's custom data.

try {
const response = await userInstance.setTimeZone("Asia/Kolkata");
console.log("Time zone set successfully:", response);
} catch (error) {
console.error("Error setting time zone:", error);
}
note

This time zone information is stored in the user's custom data. When provisioning devices in the future, if this time zone information is already set, the provisioned devices will automatically be updated to use this time zone.

On this page