Using the Datum SDK
Make sure you did all the steps in Getting Started before you proceed with this page.
Data related operations
Store Data
To store data in the Datum Network use the set method, either with or without specifying a key name. Key names make it easier to retrieve data again because you do not need to save the hash id that is used to reference the data.
Set
//sample data to store
var data = {
"Position": {
"Longitude": 9.93368,
"Latitude": 53.55608
}
}
datum.set(data)
.then(itemId=>{
console.log(itemId); //0x6e2f6b88a8dd00616072d4fd99bcb4c59d69cd39a6ac707156b393f5cd9a61ba
})
Set with key
You can also store data with key to help you retrieve it later
datum.set(data,'PIN_SEEDS');
Retrieve Data
Using hash Id
//sample data hash. replace with your hash from "set"
var id = "0x6e2f6b88a8dd00616072d4fd99bcb4c59d69cd39a6ac707156b393f5cd9a61ba";
//retrieve data with given ide
datum.get(id)
.then(result => {
console.log(result);
//"{"Position":{"Longitude":9.93368,"Latitude":53.55608}}"
})
Using key
datum.getWithKey("PIN_SEEDS")
.then(result => {
console.log(result);
//"{"Position":{"Longitude":9.93368,"Latitude":53.55608}}"
})
Delete Data
Use the remove and removebykey methods to delete data from Storage Nodes, the hash of the data will remain on the blockchain layer which cannot be deleted (immutable).
Remove single data hash/id
//store data and remove it again
datum.remove('0x6e2f6b88a8dd00616072d4fd99bcb4c59d69cd39a6ac707156b393f5cd9a61ba');
Remove by key
//store data and remove it again
datum.removeByKey("PIN_SEEDS");
Events
All blockchain related methods return a promise with additional events fired to monitor progress.
Example
datum.deposit(1)
.once('transactionRaw', function(tx){
console.log(`Raw Transaction: ${tx}`);
})
.once('transactionHash', function(hash){
console.log(`the transaction hash after broadcasted to network: ${hash}`);
})
.then(mined => {
console.log(`transaction was mined in last block: ${mined}`);
});
Cost
Storage Costs
Use the method getStorageCosts to obtain the cost associated with uploading data on the Datum Blockchain based on size of data in bytes and duration of storage in days. The example is for 1MB of storage for 365 days.
Datum.getStorageCosts(1024 * 1024, 365)
.then(costs => {
console.log(costs);
})
Traffic Costs
Use the method getTrafficCostsGB to obtain the cost based on excepted traffic volume in GB. The example is for 10GB of traffic volume.
Datum.getTrafficCostsGB(10)
.then(costs => {
console.log(costs);
})