Update a data contract#
It is possible to update existing data contracts in certain backwards-compatible ways. This includes:
Adding new documents
Adding new optional properties to existing documents
Adding new indices only as part of new documents. Indices on existing documents cannot be added, removed, or changed.
In this tutorial we will update an existing data contract.
Prerequisites#
General prerequisites (Node.js / Dash SDK installed)
A platform address with a balance: Tutorial: Create and Fund a Wallet
A configured client: Setup SDK Client
A Dash Platform Identity: Tutorial: Register an Identity
A Dash Platform Contract ID: Tutorial: Register a Data Contract
Code#
The following examples demonstrate updating an existing contract to add a new property to an existing document. The second example shows how to update a contract that has contract history enabled:
import { setupDashClient } from '../setupDashClient.mjs';
const { sdk, keyManager } = await setupDashClient();
const { identityKey, signer } = await keyManager.getAuth();
// Edit these values for your environment
// Your contract ID from the Register a Data Contract tutorial
const DATA_CONTRACT_ID =
process.env.DATA_CONTRACT_ID || 'YOUR_DATA_CONTRACT_ID';
const DOCUMENT_TYPE = 'note';
if (!DATA_CONTRACT_ID || DATA_CONTRACT_ID === 'YOUR_DATA_CONTRACT_ID') {
throw new Error(
'Set DATA_CONTRACT_ID (env var or in code) to your contract ID from the Register a Data Contract tutorial',
);
}
try {
const existingContract = await sdk.contracts.fetch(DATA_CONTRACT_ID);
// Increment the contract version
existingContract.version += 1;
// Clone schemas, then add a new "author" property to the DOCUMENT_TYPE schema
const updatedSchemas = structuredClone(existingContract.schemas);
updatedSchemas[DOCUMENT_TYPE].properties.author = {
type: 'string',
position: 1,
};
// Apply the updated schemas (enable full validation)
existingContract.setSchemas(updatedSchemas, undefined, true, undefined);
// Submit the update
await sdk.contracts.update({
dataContract: existingContract,
identityKey,
signer,
});
console.log('Contract updated:\n', existingContract.toJSON());
} catch (e) {
console.error('Something went wrong:\n', e.message);
}
Note: This example expects a contract registered with keepsHistory: true (see the “Contract with history” example in Register a data contract). Platform stores each update to that contract as a new revision, which you can read with the Retrieve data contract history tutorial.
import { setupDashClient } from '../setupDashClient.mjs';
const { sdk, keyManager } = await setupDashClient();
const { identityKey, signer } = await keyManager.getAuth();
// Edit these values for your environment
// Your contract ID from the Register a Data Contract tutorial
const DATA_CONTRACT_ID =
process.env.DATA_CONTRACT_ID || 'YOUR_DATA_CONTRACT_ID';
const DOCUMENT_TYPE = 'note';
if (!DATA_CONTRACT_ID || DATA_CONTRACT_ID === 'YOUR_DATA_CONTRACT_ID') {
throw new Error(
'Set DATA_CONTRACT_ID (env var or in code) to your contract ID from the Register a Data Contract tutorial',
);
}
try {
const existingContract = await sdk.contracts.fetch(DATA_CONTRACT_ID);
if (!existingContract) {
throw new Error(`Contract ${DATA_CONTRACT_ID} not found`);
}
// Increment the contract version
existingContract.version += 1;
// Clone schemas, then add a new "author" property to the DOCUMENT_TYPE schema
const updatedSchemas = structuredClone(existingContract.schemas);
updatedSchemas[DOCUMENT_TYPE].properties.author = {
type: 'string',
position: 1,
};
// Apply the updated schemas (enable full validation)
existingContract.setSchemas(updatedSchemas, undefined, true, undefined);
// A contract registered with keepsHistory: true (contract-register-history.mjs)
// stores this update as a new revision. Read past revisions with
// contract-retrieve-history.mjs.
// Submit the update
await sdk.contracts.update({
dataContract: existingContract,
identityKey,
signer,
});
console.log('Contract updated:\n', existingContract.toJSON());
} catch (e) {
console.error('Something went wrong:\n', e.message);
}
Note
Please refer to the data contract reference page for more comprehensive details related to contracts and documents.
What’s Happening#
After we initialize the client, we get the auth key signer from the key manager. We then fetch the existing contract and modify the contract’s document schema (adding an author property to the note document schema).
We clone the existing schemas, add the new property, then apply them with setSchemas(). After incrementing the contract version, we call sdk.contracts.update() with the modified contract and signing credentials to submit the update to the network.