Hey! These docs are for version 0.19.0, which is no longer officially supported. Click here for the latest version, 0.21.0!

Retrieve Documents

Retrieve data from Dash Platform

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

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 = {
  apps: {
    tutorialContract: {
      contractId: 'C96rCVpck4RdBQXG3zzP5KH4RKzfKVTsmTauu8FQenJi',
    },
  },
};
const client = new Dash.Client(clientOpts);

const getDocuments = async () => {
  return client.platform.documents.get(
    'tutorialContract.note',
    {
      limit: 2, // Only retrieve 1 document
    },
  );
};

getDocuments()
  .then((d) => {
    for (const n of d) {
      console.log('Document(s):\n', n.toJSON());
    }
  })
  .catch((e) => console.error('Something went wrong:\n', e))
  .finally(() => client.disconnect());

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.

📘

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.

{
  '$protocolVersion': 0,
  '$id': 'H1WzTmPnHnM8oitLVXa7oZG7ZrrunkxChGqHXdh1U8QC',
  '$type': 'note',
  '$dataContractId': 'C96rCVpck4RdBQXG3zzP5KH4RKzfKVTsmTauu8FQenJi',
  '$ownerId': 'FgPx8YHbWv4rMeiP52DfmnAXGtjiCiSaNKCbgT7eCRxh',
  '$revision': 1,
  message: 'Tutorial CI Test @ Mon, 08 Mar 2021 14:34:45 GMT'
}
{
  message: 'Tutorial CI Test @ Mon, 08 Mar 2021 14:34:45 GMT'
}
Tutorial CI Test @ Mon, 08 Mar 2021 14:34:45 GMT
Document {
  dataContract: DataContract {
    protocolVersion: 0,
    id: Identifier(32) [Uint8Array] [
      165, 131, 208, 183,  34, 175, 156, 255,
      138,  15, 177, 121, 155, 214,  14, 208,
      114, 110, 251, 113, 225,  37, 161, 163,
      232,   3,  56,  20,  98,  91, 250,  15
    ],
    ownerId: Identifier(32) [Uint8Array] [
      218,  27, 56,   0,  87, 229, 149,  21,
      146, 180, 72, 240, 101,  46, 163, 209,
        9, 132, 87,   7,  55,  33, 235,  71,
      209, 147, 84, 126, 169,  89,   8, 198
    ],
    schema: 'https://schema.dash.org/dpp-0-4-0/meta/data-contract',
    documents: { note: [Object] },
    definitions: undefined,
    binaryProperties: { note: {} }
  },
  entropy: undefined,
  protocolVersion: 0,
  id: Identifier(32) [Uint8Array] [
    237, 220, 230, 196,  50, 167, 132,
      8, 188, 140,  16,  42, 131,  66,
     10,  36, 145, 124, 245,  37, 243,
    139, 252, 222, 119, 221,  98, 224,
    110, 246, 156, 117
  ],
  type: 'note',
  dataContractId: Identifier(32) [Uint8Array] [
    165, 131, 208, 183,  34, 175, 156, 255,
    138,  15, 177, 121, 155, 214,  14, 208,
    114, 110, 251, 113, 225,  37, 161, 163,
    232,   3,  56,  20,  98,  91, 250,  15
  ],
  ownerId: Identifier(32) [Uint8Array] [
    218,  27, 56,   0,  87, 229, 149,  21,
    146, 180, 72, 240, 101,  46, 163, 209,
      9, 132, 87,   7,  55,  33, 235,  71,
    209, 147, 84, 126, 169,  89,   8, 198
  ],
  revision: 1,
  data: { message: 'Tutorial CI Test @ Mon, 08 Mar 2021 14:34:45 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.


Did this page help you?