Retrieve Documents
Overview
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 Dash Platform Protocol Document explanation.
Prerequisites
- node.js
- Basic familiarity with JavaScript asychronous functions using async/await
- The Dash JavaScript SDK is initialized (covered in Connecting to EvoNet)
Code
Initializing the Client with a contract identity
The example below 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 Dash JavaScript SDK documentation for details.
const Dash = require('dash');
const clientOpts = {
network: 'evonet',
apps: {
tutorialContract: {
contractId: '4xGhiiDR9o7VhpnW7YtLZvpWXdKByBgChZvUbri8ETJP'
}
}
};
const client = new Dash.Client(clientOpts);
const getDocuments = async function () {
try {
const queryOpts = {
limit: 1 // Only retrieve 1 document
};
const documents = await client.platform.documents.get(
'tutorialContract.note',
queryOpts
);
console.log(documents);
} catch (e) {
console.error('Something went wrong:', e);
} finally {
client.disconnect();
}
};
getDocuments();
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.
Example Document
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.
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.
{
'$id': 'GzbYhwkaomtxgkRqJYJ2QBWHFvorEza1P62zRwMmy5uY',
'$type': 'note',
'$dataContractId': '4xGhiiDR9o7VhpnW7YtLZvpWXdKByBgChZvUbri8ETJP',
'$ownerId': '58keGTkwoDycwWkRMmPYQMxVSEc6gy3fSTg59pfHAtdy',
'$revision': 1,
message: 'Tutorial Test @ Wed, 22 Jul 2020 19:53:26 GMT'
}
{
message: 'Tutorial Test @ Wed, 22 Jul 2020 19:53:26 GMT'
}
Tutorial Test @ Wed, 22 Jul 2020 19:53:26 GMT
Document {
entropy: undefined,
id: 'GzbYhwkaomtxgkRqJYJ2QBWHFvorEza1P62zRwMmy5uY',
type: 'note',
dataContractId: '4xGhiiDR9o7VhpnW7YtLZvpWXdKByBgChZvUbri8ETJP',
ownerId: '58keGTkwoDycwWkRMmPYQMxVSEc6gy3fSTg59pfHAtdy',
revision: 1,
data: { message: 'Tutorial Test @ Wed, 22 Jul 2020 19:53:26 GMT' }
}
What's happening
After we initialize the Client, we request some documents. The client.platform.documents.get
method takes two arguments: a record locator and a query object. The records locator consists of an app name (e.g. tutorialContract
) and the top-level document type requested, (e.g. note
).
DPNS Contract
Note: Access to the DPNS contract is built into the Dash SDK. DPNS documents may be accessed via the
dpns
app name (e.g.dpns.domain
).
If you need more than the first 100 documents, you'll have to make additional requests with startAt
incremented by 100 each time. In the future, the Dash SDK may return documents with paging information to make this easier and reveal how many documents are returned in total.
Updated about 4 years ago