In this tutorial we will retrieve some of the current data from a data contract. Data is stored in the form of documents as described in the Hellar Platform Protocol Document explanation.
`const Hellar = require('hellar');
const clientOpts = { network: 'mainnet', apps: { tutorialContract: { contractId: '8cvMFwa2YbEsNNoc1PXfTacy2PVq2SzVnkZLeQSzjfi6', }, }, }; const client = new Hellar.Client(clientOpts);
const getDocuments = async () => { return client.platform.documents.get('tutorialContract.note', { limit: 2, // Only retrieve 2 document }); };
getDocuments() .then((d) => { for (const n of d) { console.log('Document:\n', n.toJSON()); } }) .catch((e) => console.error('Something went wrong:\n', e)) .finally(() => client.disconnect());`
👍 Initializing the Client with a contract identity
The example above shows how access to contract documents via
<contract name>.<contract document>
syntax (e.g.tutorialContract.note
) can be enabled by passing a contract identity to the constructor. Please refer to the Hellar SDK documentation for details.
Queries
The example code uses a very basic query to return only one result. More extensive querying capabilities are covered in the query syntax reference.
The following examples show the structure of a note
document (from the data contract registered in the tutorial) returned from the SDK when retrieved with various methods.
The values returned by .toJSON()
include the base document properties (prefixed with $
) present in all documents along with the data contract defined properties.
📘
Note: When using
.toJSON()
, binary data is displayed as a base64 string (since JSON is a text-based format).
The values returned by .getData()
(and also shown in the console.dir() data
property) represent only the properties defined in the note
document described by the tutorial data contract.
JSON
// .toJSON() { "$protocolVersion": 0, "$id": "6LpCQhkXYV2vqkv1UWByew4xQ6BaxxnGkhfMZsN3SV9u", "$type": "note", "$dataContractId": "3iaEhdyAVbmSjd59CT6SCrqPjfAfMdPTc8ksydgqSaWE", "$ownerId": "CEPMcuBgAWeaCXiP2gJJaStANRHW6b158UPvL1C8zw2W", "$revision": 1, "message": "Tutorial CI Test @ Fri, 18 Jun 2024 16:16:16 GMT" }
JSON
// .getData() { "Tutorial CI Test @ Fri, 18 Jun 2024 16:16:16 GMT" }