Guides
Dash CoreDash PlatformDash.orgDash User DocsLog In
Guides
These docs are for v0.16.0. Click to read the latest docs for v0.25-redirect.

Update Documents

Update data stored on Dash Platform

Overview

In this tutorial we will update existing data on Dash Platform. Data is stored in the form of documents which are encapsulated in a state transition before being submitted to DAPI.

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 SDK documentation for details.

const Dash = require('dash');

const clientOpts = {
  network: 'evonet',
  wallet: {
    mnemonic: 'a Dash wallet mnemonic with evonet funds goes here',
  },
  apps: {
    tutorialContract: {
      contractId: 'Q894cs83D8REQNo7mAetj1wPJK2W3svrwqaN61aP25W',
    },
  },
};
const client = new Dash.Client(clientOpts);

const updateNoteDocument = async () => {
  const platform = client.platform;
  const identity = await platform.identities.get('an identity ID goes here');
  const documentId = 'an existing document ID goes here';

  // Retrieve the existing document
  const [document] = await client.platform.documents.get(
    'tutorialContract.note',
    { where: [['$id', '==', documentId]] },
  );

  // Update document
  document.set('message', 'Updated document @ ' + new Date().toUTCString());

  // Sign and submit the document replace transition
  return platform.documents.broadcast({ replace: [document] }, identity);
};

updateNoteDocument()
  .then((d) => console.log('Document updated:\n', d))
  .catch((e) => console.error('Something went wrong:\n', e))
  .finally(() => client.disconnect());

What's happening

After we initialize the Client, we retrieve the document to be updated via platform.documents.get using its id. Once the document has been retrieved, we must submit it to DAPI with the desired data updates. Documents are submitted in batches that may contain multiple documents to be created, replaced, or deleted. In this example, a single document is being updated.

The platform.documents.broadcast method then takes the document batch (e.g. {replace: [noteDocument]}) and an identity parameter. Internally, it creates a State Transition containing the previously created document, signs the state transition, and submits the signed state transition to DAPI.